Skip to content

Validation does not work as expected on inner, required field #63

@musteresel

Description

@musteresel

I have an "outer" array, which has "inner" objects, which need to have a "num" field.

JSON Schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Demo",
  "type": "object",
  "properties": {
    "outer": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/inner"
      }
    }
  },
  "required": ["outer"],
  "additionalProperties": false,
  "$defs": {
    "inner": {
      "type": "object",
      "properties": {
        "num": {
          "type": "number"
        }
      },
      "required": ["num"],
      "additionalProperties": false
    }
  }
}

JSON:

{
  "outer": [
    {
    }
  ]
}

Expected: Failing validation because "num" is missing, this happens for example with this online validator: https://www.jsonschemavalidator.net/
Outcome: Validation succeeds.

Code to reproduce (using busted):

require "busted.runner" ()
local assert = require "luassert"
---@diagnostic disable:undefined-global
local describe = describe
local it = it
---@diagnostic enable:undefined-global

local rapidjson = require "rapidjson"

describe("rapidjson", function()
    local schema = [[
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Demo",
  "type": "object",
  "properties": {
    "outer": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/inner"
      }
    }
  },
  "required": ["outer"],
  "additionalProperties": false,
  "$defs": {
    "inner": {
      "type": "object",
      "properties": {
        "num": {
          "type": "number"
        }
      },
      "required": ["num"],
      "additionalProperties": false
    }
  }
}
    ]]
    local json = [[
{
  "outer": [
    {
    }
  ]
}   
    ]]
    it("fails to validate this", function()
        local sd = rapidjson.SchemaDocument(schema)
        local val = rapidjson.SchemaValidator(sd)
        local ok, msg = val:validate(rapidjson.Document(json))
        print(msg)
        assert.is_false(ok)
    end)
end)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions