api7 / jsonschema

Pure Lua JSON schema validator for Lua/LuaJIT
https://www.apiseven.com/
Apache License 2.0
119 stars 28 forks source link

Overwriting properties from $ref does not work #55

Open ypnos opened 3 years ago

ypnos commented 3 years ago

In the following example, we combine "$ref" with another key, here "properties".

Expected result: Validation returns "false" as modelVersion.major is > 2. Observed result: Validation returns "true"

local schema = [[
  {
    "type": "object",
    "$defs": {
      "version": {
        "type": "object",
        "properties": {
          "major": {
            "type": "integer",
            "minimum": 0
          },
          "minor": {
            "type": "integer",
            "minimum": 0
          },
          "patch": {
            "type": "integer",
            "minimum": 0
          }
        },
        "required": [
          "major",
          "minor",
          "patch"
        ]
      }
    },
    "properties": {
      "modelVersion": {
        "$ref": "#/$defs/version",
        "properties": {
          "major": {
            "type": "integer",
            "minimum": 0,
            "maximum": 1
          },
          "minor": {
            "type": "integer",
            "minimum": 0
          },
          "patch": {
            "type": "integer",
            "minimum": 0
          }
        }
      }
    }
  }
]]
local v = JsonSchema.generate_validator(Json.decode(schema), {match_pattern = function () end})
print(v(Json.decode([[
  {
    "modelVersion": {"major": 2, "minor": 2, "patch": 0}
  }
]])))
membphis commented 3 years ago

https://github.com/jdesgats/ljsonschema

does it have this bug too? If yes, welcome to submit a PR to fix it