-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Corrected defaulting & non-required empty property handling #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,26 +202,45 @@ class ArrayField extends Component { | |
|
|
||
| onAddClick = event => { | ||
| event.preventDefault(); | ||
| const { schema, registry, formData } = this.props; | ||
| const { schema, registry, required } = this.props; | ||
| const { definitions } = registry; | ||
| let formData = this.props.formData; | ||
| let itemSchema = schema.items; | ||
| if (isFixedItems(schema) && allowAdditionalItems(schema)) { | ||
| itemSchema = schema.additionalItems; | ||
| } | ||
|
|
||
| this.props.onChange( | ||
| [...formData, getDefaultFormState(itemSchema, undefined, definitions)], | ||
| [ | ||
| ...formData, | ||
| getDefaultFormState(itemSchema, undefined, definitions, required), | ||
| ], | ||
| { validate: false } | ||
| ); | ||
| }; | ||
|
|
||
| onDropIndexClick = index => { | ||
| console.log("DROP INDEX"); | ||
| return event => { | ||
| if (event) { | ||
| event.preventDefault(); | ||
| } | ||
| const { formData, onChange } = this.props; | ||
| const { onChange, required } = this.props; | ||
| let formData = this.props.formData; | ||
|
|
||
| // if field isn't required and doesn't contain any entries | ||
| // remove the whole property from formData to guarantee correct validation | ||
| if (!required) { | ||
| formData = formData.filter(item => { | ||
| return item !== null && item !== undefined; | ||
| }); | ||
|
|
||
| formData = formData.length ? formData : undefined; | ||
| } | ||
| // refs #195: revalidate to ensure properly reindexing errors | ||
| onChange(formData.filter((_, i) => i !== index), { validate: true }); | ||
|
|
||
| formData = formData ? formData.filter((_, i) => i !== index) : formData; | ||
| onChange(formData, { validate: true }); | ||
| }; | ||
| }; | ||
|
|
||
|
|
@@ -250,12 +269,13 @@ class ArrayField extends Component { | |
| onChangeForIndex = index => { | ||
| return value => { | ||
| const { formData, onChange } = this.props; | ||
| const newFormData = formData.map((item, i) => { | ||
| let newFormData = formData.map((item, i) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Does this still need to be |
||
| // We need to treat undefined items as nulls to have validation. | ||
| // See https://github.com/tdegrunt/jsonschema/issues/206 | ||
| const jsonValue = typeof value === "undefined" ? null : value; | ||
| return index === i ? jsonValue : item; | ||
| }); | ||
|
|
||
| onChange(newFormData, { validate: false }); | ||
| }; | ||
| }; | ||
|
|
@@ -576,7 +596,7 @@ if (process.env.NODE_ENV !== "production") { | |
| errorSchema: PropTypes.object, | ||
| onChange: PropTypes.func.isRequired, | ||
| onBlur: PropTypes.func, | ||
| formData: PropTypes.array, | ||
| //formData: PropTypes.array, | ||
| required: PropTypes.bool, | ||
| disabled: PropTypes.bool, | ||
| readonly: PropTypes.bool, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,7 +103,12 @@ export function getWidget(schema, widget, registeredWidgets = {}) { | |
| throw new Error(`No widget "${widget}" for type "${type}"`); | ||
| } | ||
|
|
||
| function computeDefaults(schema, parentDefaults, definitions = {}) { | ||
| function computeDefaults( | ||
| schema, | ||
| parentDefaults, | ||
| definitions = {}, | ||
| required = false | ||
| ) { | ||
| // Compute the defaults recursively: give highest priority to deepest nodes. | ||
| let defaults = parentDefaults; | ||
| if (isObject(defaults) && isObject(schema.default)) { | ||
|
|
@@ -126,6 +131,10 @@ function computeDefaults(schema, parentDefaults, definitions = {}) { | |
| defaults = schema.default; | ||
| } | ||
|
|
||
| if (!required) { | ||
| return defaults; | ||
| } | ||
|
|
||
| switch (schema.type) { | ||
| // We need to recur for object schema inner default values. | ||
| case "object": | ||
|
|
@@ -136,7 +145,8 @@ function computeDefaults(schema, parentDefaults, definitions = {}) { | |
| acc[key] = computeDefaults( | ||
| schema.properties[key], | ||
| (defaults || {})[key], | ||
| definitions | ||
| definitions, | ||
| schema.required && schema.required.indexOf(key) > -1 | ||
| ); | ||
| return acc; | ||
| }, | ||
|
|
@@ -153,12 +163,22 @@ function computeDefaults(schema, parentDefaults, definitions = {}) { | |
| return defaults; | ||
| } | ||
|
|
||
| export function getDefaultFormState(_schema, formData, definitions = {}) { | ||
| export function getDefaultFormState( | ||
| _schema, | ||
| formData, | ||
| definitions = {}, | ||
| required = false | ||
| ) { | ||
| if (!isObject(_schema)) { | ||
| throw new Error("Invalid schema: " + _schema); | ||
| } | ||
| const schema = retrieveSchema(_schema, definitions); | ||
| const defaults = computeDefaults(schema, _schema.default, definitions); | ||
| const defaults = computeDefaults( | ||
| schema, | ||
| _schema.default, | ||
| definitions, | ||
| required | ||
| ); | ||
| if (typeof formData === "undefined") { | ||
| // No form data? Use schema defaults. | ||
| return defaults; | ||
|
|
@@ -197,6 +217,8 @@ export function isObject(thing) { | |
|
|
||
| export function mergeObjects(obj1, obj2, concatArrays = false) { | ||
| // Recursively merge deeply nested objects. | ||
| obj1 = obj1 || {}; | ||
| obj2 = obj2 || {}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this change about? |
||
| var acc = Object.assign({}, obj1); // Prevent mutation of source object. | ||
| return Object.keys(obj2).reduce( | ||
| (acc, key) => { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we be doing this
filterbefore theif (!required)check?