-
-
Notifications
You must be signed in to change notification settings - Fork 872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
addSchema() produces error when {nullable: true} #1171
Comments
Workaround const ajv = new Ajv({nullable: true});
const validate = ajv.compile(schema);
const valid1 = validate(value1);
const valid2 = validate(value2); So why use const ajv = new Ajv({nullable: true});
ajv.addSchema(apiDoc, '#')
.validate('#/components/schemas/Details', response.body) In this case the workaround we found is to "trick" const validate = ajv.compile({
$ref: '#/components/schemas/Details',
...schema,
});
validate(response.body); |
A better approach would be to give schema some ID and then using getSchema or validate: ajv.addSchema(schema, 'foo')
validate = ajv.getSchema('foo#/components/schemas/Details')
validate(data)
// or
ajv.validate('foo#/components/schemas/Details', data) |
I thought a |
schema id is URI with JSON pointer in hash fragment, so “foo” and “foo#” are treated as the same schema id. That’s why # on its own is an empty URI. I am still not sure why it works without nullable and doesn’t work with it... |
Well, I leave it to you to decide whether this is a bug and the issue should remain open. |
Yes, I want to investigate it at some point. It is definitely worth clarifying schema ID in docs, in line with the specification, in this section: https://github.com/epoberezkin/ajv#ref All the examples there have full URI as schema IDs. |
This works in the latest version. |
Using
nullable: true
option when callingaddSchema()
an error is thrown:What version of Ajv are you using?
Ajv version: 6.12.0 (atm latest)
Example code to reproduce
A bit more details
On the example code when you remove the
nullable
option everything works as expected and the console printstrue
andfalse
. Setting the option to eithernullable: true
ornullable: false
produces the error.What results did you expect?
Not throwing error and console logging
true
2 times.Are you going to resolve the issue?
No, but I will provide a workaround in the comments.
The text was updated successfully, but these errors were encountered: