jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

Wrong validation result

Open ypnos opened this issue 4 years ago • 3 comments

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

ypnos avatar Sep 18 '20 09:09 ypnos

welcome PR to fix it

membphis avatar Sep 18 '20 10:09 membphis

Is the if-then-construct the issue?

ypnos avatar Sep 21 '20 08:09 ypnos

yes, that is not a easy job

membphis avatar Sep 22 '20 01:09 membphis