UNPKG

@tsed/cli-core

Version:
24 lines (23 loc) 637 B
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 }; }