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

JSON Schema oneOf does not respect const as a discriminator #2314

Open
evanpurkhiser opened this issue Oct 24, 2024 · 0 comments
Open

JSON Schema oneOf does not respect const as a discriminator #2314

evanpurkhiser opened this issue Oct 24, 2024 · 0 comments

Comments

@evanpurkhiser
Copy link

Given the schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "oneOf": [
    {
      "$ref": "#/definitions/First"
    },
    {
      "$ref": "#/definitions/Second"
    }
  ],
  "definitions": {
    "First": {
      "type": "object",
      "properties": {
        "type":  {"const": "one"},
        "value": {"type": "number"}
      },
      "required": ["type", "value"]
    },
    "Second": {
      "type": "object",
      "properties": {
        "type":  {"const": "two"},
        "value": {"type": "number"}
      },
      "required": ["type", "value"]
    }
  }
}

I would expect these two schemas to be seen as different shapes and should correctly match these two examples

{"type": "one", "value": 5}
{"type": "two", "value": 5}

And for this to be invald

{"type": "three", "value": 5}

Using the python-rapidjson library:

>>> import rapidjson
>>> compiled = rapidjson.Validator(open('example.schema.json').read())
>>> compiled('{"type": "one", "value": 5}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
rapidjson.ValidationError: ('oneOf', '#', '#')
>>> compiled('{"type": "two", "value": 5}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
rapidjson.ValidationError: ('oneOf', '#', '#')
>>> compiled('{"type": "three", "value": 5}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
rapidjson.ValidationError: ('oneOf', '#', '#')

If I change the type from using const to something like enum: ["one"] this works.

I would expect const as a shape discriminator to work.

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

1 participant