mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-17 01:15:56 +08:00
feat: enhance JSON schema validation and error handling in configuration modal
This commit is contained in:
parent
8a4a6720d1
commit
695d3b7a03
@ -9,7 +9,7 @@ import { useTranslation } from 'react-i18next'
|
|||||||
import Button from '@/app/components/base/button'
|
import Button from '@/app/components/base/button'
|
||||||
import VisualEditor from './visual-editor'
|
import VisualEditor from './visual-editor'
|
||||||
import SchemaEditor from './schema-editor'
|
import SchemaEditor from './schema-editor'
|
||||||
import { jsonToSchema } from '../../utils'
|
import { getValidationErrorMessage, jsonToSchema, validateSchemaAgainstDraft7 } from '../../utils'
|
||||||
import { MittProvider, VisualEditorContextProvider } from './visual-editor/context'
|
import { MittProvider, VisualEditorContextProvider } from './visual-editor/context'
|
||||||
import ErrorMessage from './error-message'
|
import ErrorMessage from './error-message'
|
||||||
|
|
||||||
@ -59,15 +59,15 @@ const JsonSchemaConfig: FC<JsonSchemaConfigProps> = ({
|
|||||||
try {
|
try {
|
||||||
const schema = JSON.parse(json)
|
const schema = JSON.parse(json)
|
||||||
setParseError(null)
|
setParseError(null)
|
||||||
// const ajvError = validateSchemaAgainstDraft7(schema)
|
const ajvError = validateSchemaAgainstDraft7(schema)
|
||||||
// if (ajvError.length > 0) {
|
if (ajvError.length > 0) {
|
||||||
// setValidationError(getValidationErrorMessage(ajvError))
|
setValidationError(getValidationErrorMessage(ajvError))
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// else {
|
else {
|
||||||
setJsonSchema(schema)
|
setJsonSchema(schema)
|
||||||
setValidationError('')
|
setValidationError('')
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
setValidationError('')
|
setValidationError('')
|
||||||
@ -117,15 +117,15 @@ const JsonSchemaConfig: FC<JsonSchemaConfigProps> = ({
|
|||||||
try {
|
try {
|
||||||
schema = JSON.parse(json)
|
schema = JSON.parse(json)
|
||||||
setParseError(null)
|
setParseError(null)
|
||||||
// const ajvError = validateSchemaAgainstDraft7(schema)
|
const ajvError = validateSchemaAgainstDraft7(schema)
|
||||||
// if (ajvError.length > 0) {
|
if (ajvError.length > 0) {
|
||||||
// setValidationError(getValidationErrorMessage(ajvError))
|
setValidationError(getValidationErrorMessage(ajvError))
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// else {
|
else {
|
||||||
setJsonSchema(schema)
|
setJsonSchema(schema)
|
||||||
setValidationError('')
|
setValidationError('')
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
setValidationError('')
|
setValidationError('')
|
||||||
|
@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next'
|
|||||||
import Button from '@/app/components/base/button'
|
import Button from '@/app/components/base/button'
|
||||||
import CodeEditor from '../code-editor'
|
import CodeEditor from '../code-editor'
|
||||||
import ErrorMessage from '../error-message'
|
import ErrorMessage from '../error-message'
|
||||||
// import { getValidationErrorMessage, validateSchemaAgainstDraft7 } from '../../../utils'
|
import { getValidationErrorMessage, validateSchemaAgainstDraft7 } from '../../../utils'
|
||||||
|
|
||||||
type GeneratedResultProps = {
|
type GeneratedResultProps = {
|
||||||
schema: SchemaRoot
|
schema: SchemaRoot
|
||||||
@ -44,14 +44,14 @@ const GeneratedResult: FC<GeneratedResultProps> = ({
|
|||||||
const jsonSchema = useMemo(() => formatJSON(schema), [schema])
|
const jsonSchema = useMemo(() => formatJSON(schema), [schema])
|
||||||
|
|
||||||
const handleApply = useCallback(() => {
|
const handleApply = useCallback(() => {
|
||||||
// const ajvError = validateSchemaAgainstDraft7(schema)
|
const ajvError = validateSchemaAgainstDraft7(schema)
|
||||||
// if (ajvError.length > 0) {
|
if (ajvError.length > 0) {
|
||||||
// setValidationError(getValidationErrorMessage(ajvError))
|
setValidationError(getValidationErrorMessage(ajvError))
|
||||||
// }
|
}
|
||||||
// else {
|
else {
|
||||||
onApply()
|
onApply()
|
||||||
setValidationError('')
|
setValidationError('')
|
||||||
// }
|
}
|
||||||
}, [schema, onApply])
|
}, [schema, onApply])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import { ArrayType, Type } from './types'
|
import { ArrayType, Type } from './types'
|
||||||
import type { ArrayItems, Field, LLMNodeType } from './types'
|
import type { ArrayItems, Field, LLMNodeType } from './types'
|
||||||
// import Ajv, { type ErrorObject } from 'ajv'
|
import Ajv, { type ErrorObject } from 'ajv'
|
||||||
// import draft7MetaSchema from 'ajv/dist/refs/json-schema-draft-07.json'
|
import produce from 'immer'
|
||||||
// import produce from 'immer'
|
|
||||||
|
|
||||||
export const checkNodeValid = (payload: LLMNodeType) => {
|
export const checkNodeValid = (payload: LLMNodeType) => {
|
||||||
return true
|
return true
|
||||||
@ -83,29 +82,26 @@ export const findPropertyWithPath = (target: any, path: string[]) => {
|
|||||||
return current
|
return current
|
||||||
}
|
}
|
||||||
|
|
||||||
// const ajv = new Ajv({
|
const ajv = new Ajv({
|
||||||
// allErrors: true,
|
allErrors: true,
|
||||||
// verbose: true,
|
verbose: true,
|
||||||
// validateSchema: true,
|
})
|
||||||
// meta: false,
|
|
||||||
// })
|
|
||||||
// ajv.addMetaSchema(draft7MetaSchema)
|
|
||||||
|
|
||||||
// export const validateSchemaAgainstDraft7 = (schemaToValidate: any) => {
|
export const validateSchemaAgainstDraft7 = (schemaToValidate: any) => {
|
||||||
// const schema = produce(schemaToValidate, (draft: any) => {
|
const schema = produce(schemaToValidate, (draft: any) => {
|
||||||
// // Make sure the schema has the $schema property for draft-07
|
// Make sure the schema has the $schema property for draft-07
|
||||||
// if (!draft.$schema)
|
if (!draft.$schema)
|
||||||
// draft.$schema = 'http://json-schema.org/draft-07/schema#'
|
draft.$schema = 'http://json-schema.org/draft-07/schema#'
|
||||||
// })
|
})
|
||||||
|
|
||||||
// const valid = ajv.validateSchema(schema)
|
const valid = ajv.validateSchema(schema)
|
||||||
|
|
||||||
// return valid ? [] : ajv.errors || []
|
return valid ? [] : ajv.errors || []
|
||||||
// }
|
}
|
||||||
|
|
||||||
// export const getValidationErrorMessage = (errors: ErrorObject[]) => {
|
export const getValidationErrorMessage = (errors: ErrorObject[]) => {
|
||||||
// const message = errors.map((error) => {
|
const message = errors.map((error) => {
|
||||||
// return `Error: ${error.instancePath} ${error.message} Details: ${JSON.stringify(error.params)}`
|
return `Error: ${error.instancePath} ${error.message} Details: ${JSON.stringify(error.params)}`
|
||||||
// }).join('; ')
|
}).join('; ')
|
||||||
// return message
|
return message
|
||||||
// }
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user