0

How should a JSON schema validator handle the case where a sub-object of an object has a default value but the parent object hasn't?

Imagine the following schema

{
    "type": "object",
    "properties": {
        "element": {
           "type": "object",
           "properties": {
               "number" : { "type": "integer", "default": 15 }
           }
        }
    }
}

Validated against the following JSON: {} it is resulting in {}.

But shouldn't it result in

{
    "element": {
        "number": 15
    }
}

.

How do we have to interpret the default-keyword? I read the corresponding lines in the standard, but they haven't helped me further.

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
  • I created an issue here: https://github.com/json-schema-org/json-schema-spec/issues/200 to better understand things. – Patrick B. Dec 20 '16 at 08:45

2 Answers2

1

The act of validating an instance only returns "valid" or "invalid". JSON Schema validation doesn't change the instance in any way, or "result in" a new instance.

"default" is a fairly generic metadata keyword that can (and is allowed to) mean different things to different people. It doesn't necessarily mean that you can fill in values when they don't exist. It does mean, at the very least, that you can assume an initial value at the time you decide to create it.

Like "title" and "description", the "default" keyword is mostly aimed at user interfaces.

awwright
  • 565
  • 3
  • 8
  • 1
    Default-values seem to be useful in my case (config-files). Also having the validation filling in missing elements if they have a default value. Where do you read that the validator should not modify the instance? – Patrick B. Dec 20 '16 at 08:47
  • A validator can have additional features, including modifying an instance according to some rules. But validation _per se_ only has to return valid/invalid (most validators will also return a list of problems to be corrected). – awwright Dec 20 '16 at 09:17
0

Good question..

But there is a simple solution for it.You can use enum keyword for default values.See the below example that can give you the snapshot of it

  filterType: {
            type: 'String',
            required: true,
            enum: ["Accounts", "portfolios"]
        }

so that field should contain any of those two value..i think this would work for you...

Trojan
  • 1,396
  • 2
  • 16
  • 19
  • `required`, if present is MUST be an array, according to the specification draft4. http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.15 – Patrick B. Dec 29 '16 at 07:02
  • required keyword is used for must be specified fields in json. means, value must be specified to that field which has set to **required : true** – Trojan Dec 29 '16 at 07:10
  • To which schema specification do you refer? – Patrick B. Dec 29 '16 at 07:12
  • No, it's not compliant with the specification I'm citing. The required must be an array. – Patrick B. Dec 29 '16 at 07:24