mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-28 18:50:41 +00:00
feat(shell): add validate svc
This commit is contained in:
parent
bff08af186
commit
640d3261c3
27
packages/shell/src/services/validate.ts
Normal file
27
packages/shell/src/services/validate.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import type { Schema } from 'ajv'
|
||||
import Ajv from 'ajv'
|
||||
import localize from 'ajv-i18n/localize/zh'
|
||||
import schemas from '../../generated/schemas.json'
|
||||
|
||||
const ajv = new Ajv({
|
||||
discriminator: true,
|
||||
useDefaults: true,
|
||||
})
|
||||
|
||||
ajv.addKeyword({
|
||||
keyword: 'defaultProperties',
|
||||
valid: true,
|
||||
})
|
||||
|
||||
ajv.addSchema(schemas as Schema[])
|
||||
|
||||
export const validate = (id: string) => async (data: unknown) => {
|
||||
const validate = ajv.getSchema(id)
|
||||
if (await validate!(data)) return undefined
|
||||
localize(validate!.errors)
|
||||
let e = ''
|
||||
let i = 0
|
||||
for (const error of validate!.errors!)
|
||||
e += `\t问题 ${++i}:${error.schemaPath}:${error.message}`
|
||||
return e
|
||||
}
|
@ -10,5 +10,6 @@
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user