json-editor
json-editor copied to clipboard
Don't include properties that are not required in the value
With this schema:
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}
If user doesn't put any data in, the output value should be {}. None of the properties are required.
If a property is required, then it makes sense to fill in the value for required property with some default value:
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": ["age"]
}
This should output {"age": 0} since "age" is required and empty object will not conform to the schema
There seems to be an undocumented option, remove_empty_properties: true that basically does this. It doesn't seem to check whether the property is required though, so if a user leaves a blank value, you will get JSON out that doesn't pass validation. Which could then cause dramas if you round-trip.
@stevage Thank you very much! That saved me a lot of work. It would be nice if remove_empty_properties is true then it will do what it was doing for required properties.
Yeah. Btw, I was slightly wrong - it's not "undocumented", but it's documented as an editor-level property (not for the whole schema).
Also, now that I look at this more closely, I think there is a bug:
{
"type": "object",
"options": { "remove_empty_properties": true} ,
"required": [ "age" ],
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}
This doesn't let you use an age of 0 because it gets removed, even though it's required.
Right, https://github.com/swagger-api/swagger-editor/issues/482 is exactly this issue
Any update on this?
Hello; For C# (.net 6 ), when you correct your bool value as follows, the false value swagger also works. public bool? IsBlock { get; set; } = false; "worked after adding a question mark"
good work