question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

oneOf validate failed

See original GitHub issue

What 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:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
epoberezkincommented, Mar 22, 2022

@JacobLey thank you!

1reaction
JacobLeycommented, Mar 22, 2022

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 a minItems or something like that to disqualify an empty array. So your example data with an empty array validates against both, hence failing validation for oneOf.

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 in oneOf.

Alternatively could switch to anyOf instead of oneOf. 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 valid

So it looks like a bug in your schema, not AJV

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found