ajv: Overriding default errors on custom keyword is not working
I am using version 4.0.5 <- there I defined the following keyword:
ajv.addKeyword("validateMaxPoints", {
errors: true,
async: false,
validate: function(value, dataObject) {
if (this.errors === null)
this.errors = [];
this.errors.push({
keyword: "validateMaxPoints",
message: "maxPoints attribute should be " + 0 + ", but is " + 0,
params: {
keyword: "validateMaxPoints"
}
});
return false;
}
});
I added validateMaxPoints to the schema, compiled it and validated my data.
I expected to see my custom error object in the errors array, but instead I got:
{ keyword: "validateMaxPoints",
dataPath: "",
schemaPath: "#/validateMaxPoints",
params: { keyword: "validateMaxPoints" },
message: "should pass "validateMaxPoints" keyword validation",
schema: true,
parentSchema:
{ type: "object",
properties: [Object],
required: [Object],
validateMaxPoints: true },
data:
{ user: "112233445566778899101112",
title: "title",
timestamp: "Wed May 04 2016 12:26:24 GMT+0200 (CEST)",
maxPoints: 10,
type: "list",
limit: "",
questions: [Object] } }
Is this an error or did I missed something?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 19 (8 by maintainers)
This is a pretty awkward API…
I didn’t grok what it might mean to add
errors
to the validator function until I saw @epoberezkin’s example of doing so. Sure would have helped to see this in the documentation:Almost lost my mind trying to figure out why the errors I was assigning to
this.errors
were not showing up. It might be helpful to include a specific note on the need to name yourvalidate
function in order for this setup to work? The docs at this point are not clear enough on that point.https://tonicdev.com/esp/5731a55eabbf001200bbc028 - it works. The issue seems to be though that it doesn’t validate this custom keyword if there is some other error before it.