@tsed/cli-core
Version:
Build your CLI with TypeScript and Decorators
24 lines (23 loc) • 637 B
JavaScript
import { Ajv } from "ajv";
const ajv = new Ajv({
verbose: false,
coerceTypes: true,
strict: false,
allErrors: true
});
export function validate(value, schema) {
const validate = ajv.compile(schema.toJSON());
const result = validate(value);
if (!result) {
const errors = (validate.errors || []).map((e) => ({
path: e.instancePath || e.schemaPath,
message: e.message,
expected: (e.params && e.params.type) || undefined
}));
return {
isValid: false,
errors: errors
};
}
return { isValid: true, value: value };
}