Skip to content

Commit 7182576

Browse files
Use default values when switching anyOf option
1 parent 62f3397 commit 7182576

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

packages/core/test/anyOf.test.jsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,43 @@ describe('anyOf', () => {
165165
);
166166
});
167167

168+
it('should assign a default value and set defaults on option change for scalar types schemas', () => {
169+
const { node, onChange } = createFormComponent({
170+
schema: {
171+
type: 'object',
172+
properties: {
173+
foo: {
174+
anyOf: [
175+
{
176+
type: 'string',
177+
default: 'defaultfoo',
178+
},
179+
{
180+
type: 'boolean',
181+
default: true,
182+
},
183+
],
184+
},
185+
},
186+
},
187+
});
188+
sinon.assert.calledWithMatch(onChange.lastCall, {
189+
formData: { foo: 'defaultfoo' },
190+
});
191+
192+
const $select = node.querySelector('select');
193+
194+
act(() => {
195+
fireEvent.change($select, {
196+
target: { value: $select.options[1].value },
197+
});
198+
});
199+
200+
sinon.assert.calledWithMatch(onChange.lastCall, {
201+
formData: { foo: true },
202+
});
203+
});
204+
168205
it('should assign a default value and set defaults on option change when using references', () => {
169206
const { node, onChange } = createFormComponent({
170207
schema: {

packages/utils/src/schema/sanitizeDataForNewSchema.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const NO_VALUE = Symbol('no Value');
4444
* - For each element in the `data` recursively sanitize the data, stopping at `maxItems` if specified
4545
* - Otherwise, just return the `data` removing any values after `maxItems` if it is set
4646
* - If the type of the old and new schema `items` are booleans of the same value, return `data` as is
47+
* - If the new schema contains a default value then:
48+
* - return the default value. We expect this to be a scalar value
4749
* - Otherwise return `undefined`
4850
*
4951
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
@@ -52,7 +54,8 @@ const NO_VALUE = Symbol('no Value');
5254
* @param [oldSchema] - The old schema from which the data originated
5355
* @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
5456
* @returns - The new form data, with all the fields uniquely associated with the old schema set
55-
* to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
57+
* to `undefined`. Will return `undefined` if the new schema is not an object containing properties
58+
* and doesn't provide a default value
5659
*/
5760
export default function sanitizeDataForNewSchema<
5861
T = any,
@@ -193,5 +196,12 @@ export default function sanitizeDataForNewSchema<
193196
}
194197
// Also probably want to deal with `prefixItems` as tuples with the latest 2020 draft
195198
}
199+
// Schema contains a single scalar value
200+
else {
201+
const newDefaultValue = get(newSchema, 'default');
202+
if (newDefaultValue !== undefined) {
203+
newFormData = newDefaultValue;
204+
}
205+
}
196206
return newFormData as T;
197207
}

packages/utils/test/schema/sanitizeDataForNewSchemaTest.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,5 +483,16 @@ export default function sanitizeDataForNewSchemaTest(testValidator: TestValidato
483483
};
484484
expect(schemaUtils.sanitizeDataForNewSchema(newSchema, oldSchema, ['qwerty', 'asdfg'])).toEqual({});
485485
});
486+
487+
it('returns default value when new schema is a scalar schema', () => {
488+
const oldSchema: RJSFSchema = {
489+
type: 'string',
490+
};
491+
const newSchema: RJSFSchema = {
492+
type: 'boolean',
493+
default: true,
494+
};
495+
expect(schemaUtils.sanitizeDataForNewSchema(newSchema, oldSchema, 'oldValue')).toEqual(true);
496+
});
486497
});
487498
}

0 commit comments

Comments
 (0)