Corrected defaulting & non-required empty property handling#542
Corrected defaulting & non-required empty property handling#542dropdevcoding wants to merge 3 commits intorjsf-team:masterfrom
Conversation
glasserc
left a comment
There was a problem hiding this comment.
Hi! Thanks for your contribution! Sorry it took me so long to get to this. In thinking about it, I do agree that there are some issues with defaults and non-requried property handling, but I'm not completely sure about the fixes you propose. I'm concerned about the increased complexity here, where adding required now doubles the possible code paths through all these functions. More test cases would make me feel better, but I only see one.
Here's an example of the kind of edge case I'm concerned about. With this patch applied, I tried this schema:
{
"type": "object",
"definitions": {
"Thing": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
},
"properties": {
"foo": {
"type": "array",
"minItems": 2,
"items": {
"$ref": "#/definitions/Thing"
}
}
}
}(based on the test case you added). Then, I clicked the "Add" button twice, and then I clicked one "X" button once. I'd expect to have one element left in the array afterwards, but both of them will be removed.
I think this PR should mostly focus on validation/filling in defaults without trying to get too fancy in the UX. But I'd be willing to be convinced if you find that we can't do validation without updating formData or something like that.
| // refs #195: revalidate to ensure properly reindexing errors | ||
| onChange(formData.filter((_, i) => i !== index), { validate: true }); | ||
|
|
||
| formData = formData ? formData.filter((_, i) => i !== index) : formData; |
There was a problem hiding this comment.
Shouldn't we be doing this filter before the if (!required) check?
| return value => { | ||
| const { formData, onChange } = this.props; | ||
| const newFormData = formData.map((item, i) => { | ||
| let newFormData = formData.map((item, i) => { |
There was a problem hiding this comment.
Nit: Does this still need to be let?
| if (required && schema.minItems) { | ||
| return new Array(schema.minItems).fill( | ||
| computeDefaults(schema.items, defaults, definitions) | ||
| computeDefaults(schema.items, defaults, definitions, required) |
There was a problem hiding this comment.
Should schema.required be relevant here?
|
|
||
| export function cleanUpNonRequiredArray(values) { | ||
| values = values.filter(item => { | ||
| return item !== null && item !== undefined; |
There was a problem hiding this comment.
Why do we need to filter out undefined values here?
| export function mergeObjects(obj1, obj2, concatArrays = false) { | ||
| // Recursively merge deeply nested objects. | ||
| obj1 = obj1 || {}; | ||
| obj2 = obj2 || {}; |
There was a problem hiding this comment.
What is this change about?
|
Okay, I'll sum up the reasons for the changes: Which means nonRequiredOuterProp may be undefined or if set must match the given criteria, As soon as I fill in which is correct, but when I remove the whole input the objects falls back to So the expected behaviour is: As soon as the user fills out a property the empty object remains and the user hasn't got any chance to transfer it to initial state. Same behaviour applies to non required arrays. I hope this will help to clarify. If you got further questions, please don't hesitate to ask :-) |
|
Hi @dropdevcoding, thanks for explaining the reasoning behind the PR. What do you think about my objections to the behavior of the changes, and my comments on the code review? |
|
Closing this PR due to inactivity. If you'd still like to get this PR in, please make a new PR rebased to the latest code. Thanks! |
Reasons for making this change
In the current version imho the defaulting is broken:
The value handling imho also is broken for non required fields:
When having defined an object which is not required but has required properties, the following should apply:
When deleting the last item of a non required array, the following should apply:
If this is related to existing tickets, include links to them as well.
#465
Checklist
npm run cs-formaton my branch to conform my code to prettier coding style