oneOf validate failed
See original GitHub issueWhat version of Ajv are you using? Does the issue happen if you use the latest version? 8.10.0
Ajv options object
{}
JSON Schema
{
"title": "Block object",
"type": "object",
"required": [
"transactions"
],
"properties": {
"transactions": {
"oneOf": [
{
"title": "Full transactions",
"type": "array",
"items": {
"title": "Signed 1559 Transaction",
"type": "object",
"required": [
"chainId",
"input"
],
"properties": {
"input": {
"title": "input data",
"type": "string",
"pattern": "^0x[0-9a-f]*$"
},
"chainId": {
"title": "chainId",
"type": "string",
"pattern": "^0x([1-9a-f]+[0-9a-f]*|0)$",
"description": "Chain ID that this transaction is valid on."
}
}
}
},
{
"title": "Transaction hashes",
"type": "array",
"items": {
"title": "32 byte hex value",
"type": "string",
"pattern": "^0x[0-9a-f]{64}$"
}
}
]
}
}
}
Sample data
{
"transactions": []
}
Your code
const ajv = new Ajv();
const blockSchemaValidator = ajv.compile(schema);
const valid = blockSchemaValidator(block.result);
Validation result, data AFTER validation, error messages
// valid: false
[
{
instancePath: '/transactions',
schemaPath: '#/properties/transactions/oneOf',
keyword: 'oneOf',
params: { passingSchemas: [Array] },
message: 'must match exactly one schema in oneOf'
}
]
What results did you expect? The result should pass , because the transactions field is an empty array
Are you going to resolve the issue? Not Sure
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
How to fix this error? Failed validating 'oneOf' in schema
Failed validating 'oneOf' in schema ... the file with the app instance using the command python3 <filename> , this is the error produced:...
Read more >oneOf Validation Issue - Google Groups
I am working on creating a complex JSON schema and am having issues with validating a "oneOf" construction.
Read more >JSON schema validation fails if anyOf is nested in oneOf
JSON schema validation fails to check a valid document if an "anyOf" condition is nested in an "oneOf" condition: Schema:.
Read more >Handling Validation Errors - python-jsonschema
Within the different validation keywords that can fail, this function considers anyOf and oneOf to be weak validation errors, and will sort them...
Read more >oneOf, anyOf, allOf, not - Swagger
Note the inline or referenced schema must be a schema object, not a standard JSON Schema. Now, to validation. The following JSON object...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@JacobLey thank you!
It would be easier to review with a more minimal schema, but on my review it appears that this is an expected behavior.
Both
oneOf
schemas are an array, just with different items. But neither specifies aminItems
or something like that to disqualify an empty array. So your example data with an empty array validates against both, hence failing validation foroneOf
.There are a couple ways to go about this, but I’d recommend just adding a
minItems: 1
to one of your array schemas. That will disqualify it from validation, and allow the other to pass as the only valid schema inoneOf
.Alternatively could switch to
anyOf
instead ofoneOf
. The only case where both are valid is empty should be an empty array, which sounds fine? I suspect that is a slight performance gain as well, because AJV internals can skip the second schema if the first is validSo it looks like a bug in your schema, not AJV