jsonschema
jsonschema copied to clipboard
Wrong validation result
In the following example, "default" fields have wrong type. Yet, validator gives a positive result.
local schema = [[
{
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"type": "object",
"$defs": {
"parameter": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"string",
"bool",
"int",
"real"
]
}
},
"required": [
"type"
],
"allOf": [
{
"if": {
"properties": {
"type": {
"const": "string"
}
}
},
"then": {
"$ref": "#/$defs/parameter-string"
}
},
{
"if": {
"properties": {
"type": {
"const": "bool"
}
}
},
"then": {
"$ref": "#/$defs/parameter-bool"
}
},
{
"if": {
"properties": {
"type": {
"const": "int"
}
}
},
"then": {
"$ref": "#/$defs/parameter-int"
}
},
{
"if": {
"properties": {
"type": {
"const": "real"
}
}
},
"then": {
"$ref": "#/$defs/parameter-real"
}
}
]
},
"parameter-string": {
"properties": {
"default": {
"type": "string",
"default": ""
}
}
},
"parameter-bool": {
"properties": {
"default": {
"type": "boolean",
"default": false
}
}
},
"parameter-int": {
"properties": {
"default": {
"type": "integer",
"default": 0
}
}
},
"parameter-real": {
"properties": {
"default": {
"type": "number",
"default": 0
}
}
}
},
"properties": {
"test1": {"$ref": "#/$defs/parameter"}
}
}
]]
local json = [[
{
"test1" : {"type":"string", "default": false},
"test2" : {"type":"bool", "default": 43},
"test3" : {"type":"int", "default": 22.2},
"test4" : {"type":"real", "default": "foo"}
}
]]
local options = {match_pattern=function () return true end, name="test"}
local validator = JsonSchema.generate_validator(Json.decode(schema), options)
local isValid, errorStr = validator(Json.decode(json)) -- returns true, nil
welcome PR to fix it
Is the if-then-construct the issue?
yes, that is not a easy job