Hi,
could anyone clarify the interaction of default and required ?
Here is a sample schema :
{
"type": "object",
"properties": {
"name": { "type": "string" },
"gender": { "type": "string", "enum": ["male", "female"], "default": "Male" }
},
"additionalProperties": false,
"required": ["name", "gender"]
}
I would expect one of two behaviors :
- default and required can't both be specified for a given property
- default and required can both be specified and the property is therefore not required in the input
The validator will enrich the instance with the default value.
The processing application would therefore always have a value. In that sense, the value is required.
In this case, this instance should be valid and it's not when using your validator :
when I look at the specification, I don't find an answer to this question.
What about this instance ?
{
"name": "aowss",
"gender":"Male"
}
I also expected this instance to be valid since the specification says : It is RECOMMENDED that a default value be valid against the associated schema.
This therefore means that the value doesn't have to be.
Which in turn means that the default value keyword takes precedence over the schema.
If this is true, shouldn't it also take precedence over the required keyword ?
Note that in XML Schema, default behaves differently for elements and attributes.
JSON properties are more like attributes in the sense that you can't have an empty property.
In XML Schema, default values for attributes can only be specified if the attribute is optional. (
2 If default and use are both present, use must have the ·actual value· optional. )
If the attribute is absent, the processor will provide one with the specified default value.
It isn't possible for the default value not to conform to the attribute's type.
Thanks,
Aowss