mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-19 17:09:05 +08:00
feat: add precompile script for AJV and update schema validation logic
This commit is contained in:
parent
66cc4c91a5
commit
9f7fd193ff
@ -1,6 +1,7 @@
|
||||
import { ArrayType, Type } from './types'
|
||||
import type { ArrayItems, Field, LLMNodeType } from './types'
|
||||
import Ajv, { type ErrorObject } from 'ajv'
|
||||
import type { ErrorObject } from 'ajv'
|
||||
import { validateDraft07 } from '@/public/validate-esm.mjs'
|
||||
import produce from 'immer'
|
||||
|
||||
export const checkNodeValid = (payload: LLMNodeType) => {
|
||||
@ -82,11 +83,6 @@ export const findPropertyWithPath = (target: any, path: string[]) => {
|
||||
return current
|
||||
}
|
||||
|
||||
const ajv = new Ajv({
|
||||
allErrors: true,
|
||||
verbose: true,
|
||||
})
|
||||
|
||||
export const validateSchemaAgainstDraft7 = (schemaToValidate: any) => {
|
||||
const schema = produce(schemaToValidate, (draft: any) => {
|
||||
// Make sure the schema has the $schema property for draft-07
|
||||
@ -94,9 +90,12 @@ export const validateSchemaAgainstDraft7 = (schemaToValidate: any) => {
|
||||
draft.$schema = 'http://json-schema.org/draft-07/schema#'
|
||||
})
|
||||
|
||||
const valid = ajv.validateSchema(schema)
|
||||
const valid = validateDraft07(schema)
|
||||
|
||||
return valid ? [] : ajv.errors || []
|
||||
// Access errors from the validation result
|
||||
const errors = valid ? [] : (validateDraft07 as any).errors || []
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
export const getValidationErrorMessage = (errors: ErrorObject[]) => {
|
||||
|
32
web/bin/precompile-ajv.js
Normal file
32
web/bin/precompile-ajv.js
Normal file
@ -0,0 +1,32 @@
|
||||
const fs = require('node:fs')
|
||||
const path = require('node:path')
|
||||
const Ajv = require('ajv')
|
||||
const standaloneCode = require('ajv/dist/standalone').default
|
||||
|
||||
const ajv = new Ajv({
|
||||
allErrors: true,
|
||||
verbose: true,
|
||||
code: { source: true, esm: true },
|
||||
})
|
||||
|
||||
const moduleCode = standaloneCode(ajv, {
|
||||
validateDraft07: 'http://json-schema.org/draft-07/schema#',
|
||||
})
|
||||
|
||||
const preamble = [
|
||||
'"use strict";',
|
||||
].join('')
|
||||
const imports = new Set()
|
||||
const requireRegex = /const (\S+)\s*=\s*require\((.+)\)\.(\S+);/g
|
||||
const replaced = moduleCode
|
||||
.replace(requireRegex, (_match, p1, p2, p3) => {
|
||||
imports.add(`import { ${p3} as ${p1} } from ${p2};`)
|
||||
return ''
|
||||
})
|
||||
.replace('"use strict";', '')
|
||||
|
||||
const uglyOutput = [preamble, Array.from(imports).join(''), replaced].join(
|
||||
'',
|
||||
)
|
||||
|
||||
fs.writeFileSync(path.join(__dirname, '../public/validate-esm.mjs'), uglyOutput)
|
@ -22,6 +22,7 @@
|
||||
"test:watch": "jest --watch",
|
||||
"storybook": "storybook dev -p 6006",
|
||||
"build-storybook": "storybook build",
|
||||
"precompile-ajv": "node ./bin/precompile-ajv",
|
||||
"preinstall": "npx only-allow pnpm"
|
||||
},
|
||||
"dependencies": {
|
||||
|
1
web/public/validate-esm.mjs
Normal file
1
web/public/validate-esm.mjs
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user