const Ajv = require("ajv/dist/2019")
ajv = new Ajv({
$data: true,
useDefaults: true,
allErrors: true,
verbose: true,
strict: false
});
const schema = {
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"type": "object",
"properties": {
"testFields": {
"type": "array",
"minItems": 1,
"contains": {
"properties": {
"lastUpdatedAtTime": {
"const": true
}
}
},
"minContains": 1,
"maxContains": 1,
"items": {
"type": "object",
"properties": {
"name": {
"description": "",
"type": "string"
},
"lastUpdatedAtTime": {
"description": "",
"type": "boolean",
"default": false
}
},
"additionalProperties": false
},
"uniqueItemProperties": [
"name"
]
}
}
};
const data = {
"testFields": [
{
"name": "test1",
"lastUpdatedAtTime": true
},
{
"name": "test2",
"lastUpdatedAtTime": false
},
{
"name": "test3",
"lastUpdatedAtTime": false
},
{
"name": "test4",
"lastUpdatedAtTime": true
}
]
}
const validate = ajv.compile(schema);
console.log(validate(data));
console.log(validate.errors);