Skip to content

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

Description

@evanpurkhiser

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions