pboettch / json-schema-validator

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

Default values not taken into account for conditional schema #228

Open didier-brizet opened 1 year ago

didier-brizet commented 1 year ago

Default values of sub-schema are not taken into account when a conditional schema is validated.

For instance , an empty JSON document must be validated against the follow JSON schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
   "default": {},
    "properties": {
        "condition": {
          "type": "boolean",
          "default": false
        },
        "data": {
          "type": "string"
        }
    },
    "if": {
        "properties": { "condition": { "const": true } }
    },
    "then": {
        "required": ["data"]
    }
}

The property condition is not defined in document to validate but its default value should be used to test the if condition.

The current implementation of json-schema-validator throws an exception of type std::invalid_argument: At of {} - required property 'data' not found in object

didier-brizet commented 1 year ago

I propose the following patch.

issue_226.txt

pboettch commented 1 year ago

Could you please create a pull-request with your change? It will be much easier to review. Thanks.