pboettch / json-schema-validator

JSON schema validator for JSON for Modern C++
Other
466 stars 134 forks source link

Default value of root is not taken into account #243

Closed Finkman closed 10 months ago

Finkman commented 1 year ago

Again after some time, I was playing around with "default" attribute.

My desired use-case is to define a schema that ships default values and it may be applied on an empty json.

Think about the following schema:

  {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "properties": {
          "huhu": {
              "type": "number"
          }
      },
      "type": "object",
      "default":{
        "huhu": 4
      }
  }

and the code:

auto schemaJson = R"(
  {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "title": "A person",
      "properties": {
          "huhu": {
              "type": "number",
              "minimum": 2,
              "maximum": 200
          }
      },
      "type": "object",
      "default":{
        "huhu": 4
      }
  }

  )"_json;

nlohmann::json test;

schema_validator validator{ nullptr, nlohmann::json_schema::default_string_format_check };

validator.set_root_schema( schemaJson );

auto patch = validator.validate( test );

The received patch is null but I'd expect:

[
  {
    "op": "add",
    "path": "/",
    "value": { "huhu": 4 }
  }
]