I’m building an API which can consume schema definitions which then get validated using ajv.validateSchema(). I want to add a mandatory keyword ‘sortIndex’ to my meta-schema validation. It needs to be a positive integer. Here’s what I have:
ajv.addKeyword({
keyword: 'sortIndex',
schemaType: 'number',
code(cxt: KeywordCxt) {
const { schema } = cxt;
cxt.fail(_`!(${_`${schema} > 0 && Number.isInteger(${schema})`})`);
},
error: {
message: () => 'keyword "sortIndex" must be a positive integer'
}
});
I have two questions now:
- Can I make this keyword required in all properties in my schema?
- I noticed the meta-schema validation kicks in only after I try to use compile -> validate on some content. Can I validate that the sortIndex keyword is missing during the ajv.validateSchema() check?