You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Inject defaults in arrays When using dependencies in array items, defaults are now injected. This adjusts injecting arrays into the form data from defaults to be in line with the previous behaviour. So the defaults are injected when there is either no value defined in the form data or when an array is already set in the form data, injects only into existing entries. This makes sure that we don't change any existing behaviour by creating array entries where we did not create them before. For example for following schema: { "type": "array", "minItems": 2, "items": { "title": "asd", "type": "object", "properties": { "item": { "type": "string", "default": "foo" } } } } and following form data: [{}] The result will be (note how it did not full up the array to min items) [{ "item": "foo" }] * Remove redundant comment * Add documentation and example * Merge arrays defined in parent schema by overwriting same level arrays * fix typo * fix typos * wording changes Co-Authored-By: Dominik del Bondio <dominik.del.bondio@bitextender.com>
Copy file name to clipboardExpand all lines: docs/index.md
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,6 +196,31 @@ This component follows [JSON Schema](http://json-schema.org/documentation.html)
196
196
197
197
*`"additionalProperties":false` produces incorrect schemas when used with [schema dependencies](#schema-dependencies). This library does not remove extra properties, which causes validation to fail. It is recommended to avoid setting `"additionalProperties":false` when you use schema dependencies. See [#848](https://github.com/mozilla-services/react-jsonschema-form/issues/848)[#902](https://github.com/mozilla-services/react-jsonschema-form/issues/902)[#992](https://github.com/mozilla-services/react-jsonschema-form/issues/992)
198
198
199
+
## Handling of schema defaults
200
+
201
+
This library automatically fills default values defined the [JSON Schema](http://json-schema.org/documentation.html) as initial values in your form. This also works for complex structures in the schema. If a field has a default defined, it should always appear as default value in form. This also works when using [schema dependencies](#schema-dependencies).
202
+
203
+
Since there is a complex interaction between any supplied original form data and any injected defaults, this library tries to do the injection in a way which keeps the original intention of the original form data.
204
+
205
+
Check out the defaults example on the [live playground](https://mozilla-services.github.io/react-jsonschema-form/) to see this in action.
206
+
207
+
### Merging of defaults into the form data
208
+
209
+
There are three different cases which need to be considered for the merging. Objects, arrays and scalar values. This library always deeply merges any defaults with the existing form data for objects.
210
+
211
+
This are the rules which are used when injecting the defaults:
212
+
213
+
- When the is a scalar in the form data, nothing is changed.
214
+
- When the value is `undefined` in the form data, the default is created in the form data.
215
+
- When the value is an object in the form data, the defaults are deeply merged into the form data, using the rules defined here for the deep merge.
216
+
- Then the value is an array in the form data, defaults are only injected in existing array items. No new array items will be created, even if the schema has minItems or additional items defined.
217
+
218
+
### Merging of defaults within the schema
219
+
220
+
In the schema itself, defaults of parent elements are propagated into children. So when you have a schema which defines a deeply nested object as default, these defaults will be applied to children of the current node. This also merges objects defined at different levels together with the "deeper" not having precedence. If the parent node defines properties, which are not defined in the child, they will be merged so that the default for the child will be the merged defaults of parent and child.
221
+
222
+
For arrays this is not the case. Defining an array, when a parent also defines an array, will be overwritten. This is only true when arrays are used in the same level, for objects within these arrays, they will be deeply merged again.
223
+
199
224
## Tips and tricks
200
225
201
226
- Custom field template: <https://jsfiddle.net/hdp1kgn6/1/>
0 commit comments