Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin-sommer committed Nov 22, 2024
1 parent bda8da0 commit c40e1a3
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/components/fields/MultiSchemaField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
const oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : undefined;

let newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
if (newFormData && newOption) {
if (newOption) {
// Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
// so that only the root objects themselves are created without adding undefined children properties
newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, 'excludeObjectChildren') as T;
Expand Down
12 changes: 1 addition & 11 deletions packages/utils/src/schema/sanitizeDataForNewSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ const NO_VALUE = Symbol('no Value');
* - For each element in the `data` recursively sanitize the data, stopping at `maxItems` if specified
* - Otherwise, just return the `data` removing any values after `maxItems` if it is set
* - If the type of the old and new schema `items` are booleans of the same value, return `data` as is
* - If the new schema contains a default value then:
* - return the default value. We expect this to be a scalar value
* - Otherwise return `undefined`
*
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
Expand All @@ -62,8 +60,7 @@ const NO_VALUE = Symbol('no Value');
* @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The new form data, with all the fields uniquely associated with the old schema set
* to `undefined`. Will return `undefined` if the new schema is not an object containing properties
* and doesn't provide a default value
* to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
*/
export default function sanitizeDataForNewSchema<
T = any,
Expand Down Expand Up @@ -237,12 +234,5 @@ export default function sanitizeDataForNewSchema<
}
// Also probably want to deal with `prefixItems` as tuples with the latest 2020 draft
}
// Schema contains a single scalar value
else {
const newDefaultValue = get(newSchema, 'default');
if (newDefaultValue !== undefined) {
newFormData = newDefaultValue;
}
}
return newFormData as T;
}
11 changes: 0 additions & 11 deletions packages/utils/test/schema/sanitizeDataForNewSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,5 @@ export default function sanitizeDataForNewSchemaTest(testValidator: TestValidato
};
expect(schemaUtils.sanitizeDataForNewSchema(newSchema, oldSchema, ['qwerty', 'asdfg'])).toEqual({});
});

it('returns default value when new schema is a scalar schema', () => {
const oldSchema: RJSFSchema = {
type: 'string',
};
const newSchema: RJSFSchema = {
type: 'boolean',
default: true,
};
expect(schemaUtils.sanitizeDataForNewSchema(newSchema, oldSchema, 'oldValue')).toEqual(true);
});
});
}

0 comments on commit c40e1a3

Please sign in to comment.