feat(shell): add validate svc

This commit is contained in:
Il Harper 2024-03-04 02:02:57 +08:00
parent bff08af186
commit 640d3261c3
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 28 additions and 0 deletions

View 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
}

View File

@ -10,5 +10,6 @@
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
}
}