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.

anyOf returns repeated errors

See original GitHub issue

What version of Ajv are you using? Does the issue happen if you use the latest version?

"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",

Ajv options object

I am using Ajv with allErrors option, a few keywords and formats as well

const ajv = new Ajv({allErrors: true})
const formats: FormatName[] = ['date']
this.validator = addFormats(ajv, formats).addKeyword('kind').addKeyword('modifier')

JSON Schema

My schema is being generated through another package called typebox. This is what the schema looks like when it’s passed to Ajv for the validation.

{
  kind: Symbol(ObjectKind),
  type: 'object',
  properties: {
    enumProperty: {
      kind: Symbol(EnumKind),
      anyOf: [Array],
      modifier: Symbol(OptionalModifier)
    }
  }
}

Sample data

{
  "enumProperty": "invalid-value"
}

Your code

Not relevant at this point, I think.

Validation result, data AFTER validation, error messages

"data/enumProperty must be equal to constant, data/enumProperty must be equal to constant, data/enumProperty must be equal to constant, data/enumProperty must be equal to constant, data/enumProperty must be equal to constant, data/enumProperty must be equal to constant, data/enumProperty must match a schema in anyOf"

What results did you expect?

I would like to have a simple message indicating that the value is not valid for the schema and the acceptable values for that property:

"data/enumProperty must match a schema in anyOf: ["value1", "value2"]"

If that’s not possible, than just the first part would suffice:

"data/enumProperty must match a schema in anyOf"

Are you going to resolve the issue?

Unlikely

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sinclairzx81commented, May 25, 2022

@felixjb Refer to comment https://github.com/sinclairzx81/typebox/issues/174#issuecomment-1134944761 for reasoning behind TypeBox’s preference for anyOf over enum.

0reactions
felixjbcommented, May 25, 2022

Thank you so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Json Validation against JsonSchema and anyOf [closed]
I have a json to validate against jsonSchema, I don't understand why i have an error with the first anyOf, indeed, we can...
Read more >
Strict mode - Ajv JSON schema validator
Strict mode intends to prevent any unexpected behaviours or silently ignored mistakes in user schemas. It does not change any validation results compared ......
Read more >
Understanding JSON Schema
JSON Schema is a powerful tool for validating the structure of JSON data. However, learning to use it by reading its.
Read more >
Improved Error Messages for Schema Validation in MongoDB ...
The error returns information about failure for two rules: "array did not match specified length" and "found a duplicate item," and it also...
Read more >
Applying multiple subschemas | Opis JSON Schema
The value of this keyword must be an array of valid JSON schemas (objects or booleans). Schema. { "type": "array", "anyOf ...
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