Skip to content
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

"Constant" does not seem to validate the same way as equivalent single-value enum #682

Closed
jthomerson opened this issue Jan 23, 2018 · 6 comments

Comments

@jthomerson
Copy link

Please see the details in my StackOverflow question: https://stackoverflow.com/questions/48389997/json-schema-why-does-constant-not-validate-the-same-way-as-a-single-valued-e/

It's unclear why changing { "constant": "client" } to { "enum": [ "client" ] } affects the validation. Is that an error in my usage, or a bug in AJV? Thanks!

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

Ajv options object

Not sure. I'm using this via ajv-cli@3.0.0. I also tried with my own script, using AJV@6.0.1 directly, with no options, and received the same error.

JSON Schema

See https://stackoverflow.com/questions/48389997/json-schema-why-does-constant-not-validate-the-same-way-as-a-single-valued-e/

Sample data

See https://stackoverflow.com/questions/48389997/json-schema-why-does-constant-not-validate-the-same-way-as-a-single-valued-e/

Your code
N/A

Validation result, data AFTER validation, error messages

https://stackoverflow.com/questions/48389997/json-schema-why-does-constant-not-validate-the-same-way-as-a-single-valued-e/

What results did you expect?

I expected the sample data (two shown in the SO question) to be valid. However, both were invalid because the validator said that they matched both sub-schemas in the oneOf clause. I don't see how the sample documents would ever match both of the sub-schemas - they should only match one. When changing { "constant": "client" } to { "enum": [ "client" ] }, the validation worked as expected.

Are you going to resolve the issue?

@epoberezkin
Copy link
Member

@jthomerson It should be “const”: https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#const

@jthomerson
Copy link
Author

picard-facepalm

Thanks.

@epoberezkin
Copy link
Member

It was originally proposed as “constant” and previously implemented this way though, so I understand where the confusion can be coming from...

@jthomerson
Copy link
Author

Yeah, the Google results are hurting us here ... json schema constant results all pull up those previous proposals (for good reasons ... constant vs const). I should've looked at the actual spec, but that (ironically) is harder to find when you're searching by keywords. Sorry for the wasted time. Thanks for your work on the library!

@mnpenner
Copy link

mnpenner commented Jun 6, 2018

Gahhh... I spent like an hour trying to figure out why my anyOf wasn't working -- because it wasn't switching on the "constant" like I was expecting it to 😭

@epoberezkin
Copy link
Member

@mnpenner It is “const” in draft-07 and it is available by default. Detecting unknown keywords #781 would help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants