同时使用ajv
和ajv-merge-patch
时,如果存在一个验证错误,数据验证也会返回以下错误。
should pass "$merge" keyword validation
作为验证错误。 但是,当没有验证错误时,它将返回空。
const Ajv = require('ajv');
let ajv = new Ajv({ useDefaults: true, removeAdditional: true });
require('ajv-merge-patch')(ajv);
let schema = {
$id: 'Job',
type: 'object',
properties: {
title: { type: 'string' },
},
additionalProperties: false,
};
ajv.addSchema(schema);
const createSchema = {
$merge: {
source: { $ref: 'Job' },
with: {
required: [
'title',
],
},
},
};
const createValidator = ajv.compile(createSchema);
const data = {
title: '',
};
if (!createValidator(data)) {
console.log(createValidator.errors);
}