Open csavio opened 5 years ago
It’s worth showing two samples: one for using ONLY draft4 (which requires setting default meta as well) and another to support draft4&7.
requires setting default meta as well
Where is this documented?
I'm trying to validate the following schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"CreateTokenTypeParams": {
"title": "CreateTokenTypeParams",
"type": "object",
"properties": {
"issuer": {
"type": "string",
"required": true,
"title": "issuer",
"arguments": []
},
"name": {
"type": "string",
"required": true,
"title": "name",
"arguments": []
},
"url": {
"type": "string",
"required": false,
"title": "url",
"arguments": []
}
},
"required": [
"issuer",
"name"
]
}
}
}
Using this function:
const validate = (schema, data) => {
const avg = new Ajv({
schemaId: 'id',
meta: false
});
avg.addMetaSchema(metaSchema);
avg._opts.defaultMeta = metaSchema.id;
const { errors } = avg.validate(schema, data);
return errors;
};
But I get the following error:
Error: schema is invalid: data.definitions['CreateTokenTypeParams'].properties['issuer'].required should be array
This does appear to be a valid draft-04 schema according to https://www.jsonschemavalidator.net/
@ericelliott "required" should be the list of properties on the object level, "required": true
is invalid.
This question is not related to this issue btw.
requires setting default meta as well
Where is this documented?
Good question. It was well hidden in release notes: https://github.com/epoberezkin/ajv/releases/tag/5.0.0
If you use "meta" option it will become default meta (i.e. it will be used to validate schemas without $schema).
If you use "meta: false" option and then later add schema with addMetsSchema, there will be no default meta, unless you explicitly set it.
Perhaps, it is worth to make the first added meta default (however it is added), but it should be a major version change as it may break things for some people (schemas that were not validated before witll start being validated).
"required" should be the list of properties on the object level, "required": true is invalid.
In the process of trying to get ajv to validate against that schema, I passed it through a filter to remove the required
props from each param, and ajv
ignored the required props, returned true, and produced zero errors, even when required props were missing. I gave up and wrote my own validator. I don't know if there's something else wrong with the schema, but I didn't get any hints about what else was wrong.
The schema was produced by the graphql-to-json-schema
package, which I'm using so that I can use GraphQL types to generate input validations.
When following the instructions for validating against draft-04 multiple warnings are thrown in the console every time. Might just be a documentation issue as putting
meta: false
or usingschemaId: 'auto'
in the configuration options prevents these warnings.What version of Ajv are you using? Does the issue happen if you use the latest version? 6.10.0 Yes, this happens on latest
Your code
Warning messages
What results did you expect? No warnings when following the instructions for using draft-04
Are you going to resolve the issue? No