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

"oneOf" Validation behaves the opposite way #113

Open
ghost opened this issue Apr 1, 2016 · 3 comments
Open

"oneOf" Validation behaves the opposite way #113

ghost opened this issue Apr 1, 2016 · 3 comments

Comments

@ghost
Copy link

ghost commented Apr 1, 2016

I recently tried to validate json against a schema that contains a "oneOf" fied.
The validator I used is http://jsonschemalint.com/ that uses this very library.

here is the shema

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "foo": {
          "type": "string",
          "minLength": 3,
          "maxLength": 5
        }
      }
    },
    {
      "type": "object",
      "properties": {
        "bar": {
          "minLength": 3,
          "maxLength": 5
        }
      }
    }
  ]
}

The weird thing is that it validates only when bar length in smaller than 3 or greater than 5 which is the opposite of the expected behaviour

valid json are :

{ "bar": "123" }
{ "bar": "1234" }
{ "bar": "12345" }

invalid json are :

{ "bar": "1" }
{ "bar": "12" }
{ "bar": "123456" }

Adding the field "additionalProperties" makes it work right.

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "foo": {
          "type": "string",
          "minLength": 3,
          "maxLength": 5
        }
      },
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "bar": {
          "minLength": 3,
          "maxLength": 5
        }
      },
      "additionalProperties": false
    }
  ]
}
@ReinsBrain
Copy link

First I want to say I've tested several json schema validation packages and yours is the best in my opinion. Next, I wanted to show a behavior that I wasn't expecting but could easily be my own misunderstanding of json schema. It involves array items and oneOf:

{
  "title": "test oneOf as array item",
  "type": "object",
  "properties": {
    "anArray": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "a": { "type": "integer" }
              }, "required": [ "a" ]
            },
            {
              "type": "object",
              "properties": {
                "b": { "type": "integer" }
              }, "required": [ "b" ] }
          ]
        }
      ]
    }
  }, "required": [ "anArray" ]
}

A sample JSON:

{  "anArray": [ { "a": 0 }, { "b": 1 }, { "c": 2 }, { "d": 3 } ] }

I also used the online validator at http://jsonschemalint.com/ which may be out-of-date. I was otherwise expecting 3 and 4 items of "anArray" to fail validation. I could have misunderstood how oneOf should be working and any advice is greatly appreciated. I'm also willing to pitch in and fix this if pull requests are welcome.

@emilbayes
Copy link
Collaborator

I have confirmed this edge case by the generated code. I'll try to solve this in the coming week.

@tsholmes
Copy link

tsholmes commented Sep 2, 2016

@Geo42 the behavior here is correct in your case, since oneOf makes it valid if-and-only-if exactly one of the schemas match. The first oneOf subschema is valid for any value of bar, so the oneOf case will only be true when bar doesn't match the second schema.

@ReinsBrain the behavior is correct in your case as well. You have items specified as an array, which makes it specify a schema per-index. The first element in your array matches the oneOf schema, and the rest pass by default since they don't have a schema. If you remove the [] around the items schema it will fail validation on the last 2 elements.

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

No branches or pull requests

3 participants