Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected positive validation #345

Closed
pasbi opened this issue Feb 3, 2025 · 1 comment
Closed

Unexpected positive validation #345

pasbi opened this issue Feb 3, 2025 · 1 comment

Comments

@pasbi
Copy link

pasbi commented Feb 3, 2025

Following validation is unexpectedly positive:

int main()
{
  const auto schema = nlohmann::json::parse(R"(
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "$defs": {
    "foo": { "type": "object" }
  },
  "items": {
    "type": "object",
    "$ref": "#/$defs/foo",
    "required": [ "foobar" ]
  }
})");

  const auto instance = nlohmann::json::parse("[{}]");

  nlohmann::json_schema::json_validator m_validator;
  m_validator.set_root_schema(schema);
  fmt::println("schema: {}", schema.dump(4));
  fmt::println("instance: {}", instance.dump(4));
  fmt::println("Validation result: {}", m_validator.validate(instance).dump(4));
}

The problem is, that the validator is happy although the required "foobar" key is missing.
Online tools (like https://www.jsonschemavalidator.net/) affirm that the instance doesn't satisfy the schema.

This is a minimum example to reproduce the issue, the real schema and instance are much larger.

Please let me know if this is a bug or if I'm missing something.

I use json-schema 2.2.0 with nlohmann-json 3.11.3 on g++ 14.2.1.

@pboettch
Copy link
Owner

pboettch commented Feb 4, 2025

This is because $defs is not supported by draft-7, the standard currently supported. Especially in version 2.2.0

Recently #338 has been integrated on the main-branch. Which adds $defs support to this project. But there hasn't been a release with this yet.

@pboettch pboettch closed this as completed Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants