json editor update

pull/21369/head
jZonG 1 year ago
parent fe27604bd3
commit 4028eb9519

@ -1,8 +1,33 @@
import { z } from 'zod' import { z } from 'zod'
const arrayStringSchemaParttern = z.array(z.string()) const arrayStringSchemaParttern = z.array(z.string())
const arrayNumberSchemaParttern = z.array(z.number())
export const validateArrayString = (schema: any) => { // # jsonSchema from https://zod.dev/?id=json-type
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()])
type Literal = z.infer<typeof literalSchema>
type Json = Literal | { [key: string]: Json } | Json[]
const jsonSchema: z.ZodType<Json> = z.lazy(() => z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]))
const arrayJsonSchema: z.ZodType<Json[]> = z.lazy(() => z.array(jsonSchema))
export const validateJSONSchema = (schema: any, type: string) => {
if (type === 'array[string]') {
const result = arrayStringSchemaParttern.safeParse(schema) const result = arrayStringSchemaParttern.safeParse(schema)
return result return result
}
else if (type === 'array[number]') {
const result = arrayNumberSchemaParttern.safeParse(schema)
return result
}
else if (type === 'object') {
const result = jsonSchema.safeParse(schema)
return result
}
else if (type === 'array[object]') {
const result = arrayJsonSchema.safeParse(schema)
return result
}
else {
return { success: true } as any
}
} }

@ -1,12 +1,18 @@
import { useEffect, useRef, useState } from 'react' import { useEffect, useRef, useState } from 'react'
// import { useTranslation } from 'react-i18next' // import { useTranslation } from 'react-i18next'
import { debounce } from 'lodash-es'
import Textarea from '@/app/components/base/textarea' import Textarea from '@/app/components/base/textarea'
import SchemaEditor from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/schema-editor' import SchemaEditor from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/schema-editor'
import ErrorMessage from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/error-message' import ErrorMessage from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/error-message'
import { import {
validateArrayString, checkJsonSchemaDepth,
getValidationErrorMessage,
validateSchemaAgainstDraft7,
} from '@/app/components/workflow/nodes/llm/utils'
import {
validateJSONSchema,
} from '@/app/components/workflow/variable-inspect/utils' } from '@/app/components/workflow/variable-inspect/utils'
import { debounce } from 'lodash-es' import { JSON_SCHEMA_MAX_DEPTH } from '@/config'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
export const currentVar = { export const currentVar = {
@ -17,19 +23,19 @@ export const currentVar = {
name: 'out_put', name: 'out_put',
// var_type: 'string', // var_type: 'string',
// var_type: 'number', // var_type: 'number',
// var_type: 'object', var_type: 'object',
var_type: 'array[string]', // var_type: 'array[string]',
// var_type: 'array[number]', // var_type: 'array[number]',
// var_type: 'array[object]', // var_type: 'array[object]',
// var_type: 'file', // var_type: 'file',
// var_type: 'array[file]', // var_type: 'array[file]',
// value: 'tuituitui', // value: 'tuituitui',
value: ['aaa', 'bbb', 'ccc'], // value: ['aaa', 'bbb', 'ccc'],
// value: { value: {
// abc: '123', abc: '123',
// def: 456, def: 456,
// fff: true, fff: true,
// }, },
edited: true, edited: true,
} }
@ -58,15 +64,27 @@ const ValueContent = () => {
} }
} }
const arrayStringValidate = (value: string) => { const jsonValueValidate = (value: string, type: string) => {
try { try {
const newJSONSchema = JSON.parse(value) const newJSONSchema = JSON.parse(value)
setParseError(null) setParseError(null)
const result = validateArrayString(newJSONSchema) const result = validateJSONSchema(newJSONSchema, type)
if (!result.success) { if (!result.success) {
setValidationError(result.error.message) setValidationError(result.error.message)
return false return false
} }
if (type === 'object' || type === 'array[object]') {
const schemaDepth = checkJsonSchemaDepth(newJSONSchema)
if (schemaDepth > JSON_SCHEMA_MAX_DEPTH) {
setValidationError(`Schema exceeds maximum depth of ${JSON_SCHEMA_MAX_DEPTH}.`)
return false
}
const validationErrors = validateSchemaAgainstDraft7(newJSONSchema)
if (validationErrors.length > 0) {
setValidationError(getValidationErrorMessage(validationErrors))
return false
}
}
setValidationError('') setValidationError('')
return true return true
} }
@ -84,22 +102,11 @@ const ValueContent = () => {
} }
const handleEditorChange = (value: string) => { const handleEditorChange = (value: string) => {
if (current.var_type === 'array[string]') {
setJson(value) setJson(value)
if (arrayStringValidate(value)) { if (jsonValueValidate(value, current.var_type)) {
const parsed = JSON.parse(value) const parsed = JSON.parse(value)
setJsonSchema(parsed) setJsonSchema(parsed)
} // TODO call api of value update
return
}
if (current.var_type === 'array[number]') {
// TODO update array[number]
}
if (current.var_type === 'object') {
// TODO update object
}
if (current.var_type === 'array[object]') {
// TODO update array[object]
} }
} }

Loading…
Cancel
Save