Description
We've been working with handling defaults in react-jsonschema-form and the logic seems more complicated than one might initially expect. rjsf-team/react-jsonschema-form#1376
Here are some examples of the scenarios we've come across:
- Imagine your schema is:
{
type: "object",
default: {
level1: { level2: ["root-default-1", "root-default-2"] },
},
properties: {
level1: {
type: "object",
properties: {
level2: {
type: "array",
items: [
{
type: "string",
default: "child-default-1",
},
{
type: "string",
},
],
},
},
},
},
}
Then the default value should (probably, intuitively) be {level1: {level2: { ["child-default-1", "root-default-2"] } }
instead of {level1: {level2: { ["root-default-1", "root-default-2"] } }
.
- Another schema:
{
type: "object",
properties: {
level1: {
type: "array",
default: [
{
item: "property-default-1",
},
{},
],
additionalItems: {
type: "object",
properties: {
item: {
type: "string",
default: "additional-default",
},
},
},
items: [
{
type: "object",
properties: {
item: {
type: "string",
},
},
},
],
},
},
}
Here, because the length of items
is 1, the default should probably be level1: [ { item: "property-default-1" }, { item: "additional-default" }, ]
rather than level1: [ { item: "property-default-1" }, {} ]
You can see more examples at https://github.com/mozilla-services/react-jsonschema-form/pull/1376/files#diff-0de170772e45d2602c103dd916607787.
I was wondering if there might be some value in standardizing this behavior, perhaps in the JSON Schema spec.
The spec says:
There are no restrictions placed on the value of this keyword. When multiple occurrences of this keyword are applicable to a single sub-instance, implementations SHOULD remove duplicates.
Perhaps it would be good to more clearly define what "removing duplicates" is, and define an algorithm for doing so. It's true that default
has no effect on validation, though, but it might still be good to somehow standardize how to handle overlapping defaults.