JSON Schema `oneOf` does not respect `const` as a discriminator
Author: evanpurkhiserCreated Oct 24, 2024Updated May 22, 2026
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.
Source: Tencent/rapidjson