feat: update JSON schema handling to convert boolean types to strings and improve JSON validation

This commit is contained in:
twwu 2025-03-27 15:11:38 +08:00
parent d7cda1157d
commit 6c73216416
3 changed files with 29 additions and 4 deletions

View File

@ -12,7 +12,7 @@ import { useVisualEditorStore } from './visual-editor/store'
import Toast from '@/app/components/base/toast'
type JsonImporterProps = {
onSubmit: (schema: string) => void
onSubmit: (schema: any) => void
updateBtnWidth: (width: number) => void
}
@ -55,6 +55,10 @@ const JsonImporter: FC<JsonImporterProps> = ({
const handleSubmit = useCallback(() => {
try {
const parsedJSON = JSON.parse(json)
if (typeof parsedJSON !== 'object' || Array.isArray(parsedJSON)) {
setParseError(new Error('Root must be an object, not an array or primitive value.'))
return
}
const maxDepth = checkDepth(parsedJSON)
if (maxDepth > JSON_SCHEMA_MAX_DEPTH) {
setParseError({

View File

@ -9,7 +9,7 @@ import { useTranslation } from 'react-i18next'
import Button from '@/app/components/base/button'
import VisualEditor from './visual-editor'
import SchemaEditor from './schema-editor'
import { getValidationErrorMessage, jsonToSchema, validateSchemaAgainstDraft7 } from '../../utils'
import { convertBooleanToString, getValidationErrorMessage, jsonToSchema, validateSchemaAgainstDraft7 } from '../../utils'
import { MittProvider, VisualEditorContextProvider } from './visual-editor/context'
import ErrorMessage from './error-message'
import { useVisualEditorStore } from './visual-editor/store'
@ -74,7 +74,8 @@ const JsonSchemaConfig: FC<JsonSchemaConfigProps> = ({
if (currentTab === value) return
if (currentTab === SchemaView.JsonSchema) {
try {
const schema = JSON.parse(json)
const parsedJson = JSON.parse(json)
const schema = convertBooleanToString(parsedJson)
setParseError(null)
const ajvError = validateSchemaAgainstDraft7(schema)
if (ajvError.length > 0) {
@ -116,7 +117,7 @@ const JsonSchemaConfig: FC<JsonSchemaConfigProps> = ({
setJson(JSON.stringify(schema, null, 2))
}, [currentTab])
const handleSubmit = useCallback((schema: string) => {
const handleSubmit = useCallback((schema: any) => {
const jsonSchema = jsonToSchema(schema) as SchemaRoot
if (currentTab === SchemaView.VisualEditor)
setJsonSchema(jsonSchema)

View File

@ -105,3 +105,23 @@ export const getValidationErrorMessage = (errors: ErrorObject[]) => {
}).join('; ')
return message
}
export const convertBooleanToString = (schema: any) => {
if (schema.type === Type.boolean)
schema.type = Type.string
if (schema.type === Type.array && schema.items && schema.items.type === Type.boolean)
schema.items.type = Type.string
if (schema.type === Type.object) {
schema.properties = Object.entries(schema.properties).reduce((acc, [key, value]) => {
acc[key] = convertBooleanToString(value)
return acc
}, {} as any)
}
if (schema.type === Type.array && schema.items && schema.items.type === Type.object) {
schema.items.properties = Object.entries(schema.items.properties).reduce((acc, [key, value]) => {
acc[key] = convertBooleanToString(value)
return acc
}, {} as any)
}
return schema
}