feat: add precompile script for AJV and update schema validation logic

This commit is contained in:
twwu 2025-03-27 10:56:29 +08:00
parent 66cc4c91a5
commit 9f7fd193ff
4 changed files with 41 additions and 8 deletions

View File

@ -1,6 +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 type { ErrorObject } from 'ajv'
import { validateDraft07 } from '@/public/validate-esm.mjs'
import produce from 'immer' import produce from 'immer'
export const checkNodeValid = (payload: LLMNodeType) => { export const checkNodeValid = (payload: LLMNodeType) => {
@ -82,11 +83,6 @@ export const findPropertyWithPath = (target: any, path: string[]) => {
return current return current
} }
const ajv = new Ajv({
allErrors: true,
verbose: true,
})
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
@ -94,9 +90,12 @@ export const validateSchemaAgainstDraft7 = (schemaToValidate: any) => {
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 = 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[]) => { export const getValidationErrorMessage = (errors: ErrorObject[]) => {

32
web/bin/precompile-ajv.js Normal file
View 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)

View File

@ -22,6 +22,7 @@
"test:watch": "jest --watch", "test:watch": "jest --watch",
"storybook": "storybook dev -p 6006", "storybook": "storybook dev -p 6006",
"build-storybook": "storybook build", "build-storybook": "storybook build",
"precompile-ajv": "node ./bin/precompile-ajv",
"preinstall": "npx only-allow pnpm" "preinstall": "npx only-allow pnpm"
}, },
"dependencies": { "dependencies": {

File diff suppressed because one or more lines are too long