Skip to content

Commit

Permalink
responding to feedback - updated once to excludeObjectChildren, updat…
Browse files Browse the repository at this point in the history
…ed changelog
  • Loading branch information
zmagauina-fn committed Dec 8, 2022
1 parent 776f0fc commit d1c69f7
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 18 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ should change the heading of the (upcoming) version to include a major version b
-->
# 5.0.0-beta.14

## @rjsf/antd
- No longer render extra 0 for array without errors, fixing [#3233](https://github.com/rjsf-team/react-jsonschema-form/issues/3233)

## @rjsf/core
- Added `ref` definition to `ThemeProps` fixing [#2135](https://github.com/rjsf-team/react-jsonschema-form/issues/2135)

## @rjsf/utils
- Updated `computedDefaults` (used by `getDefaultFormState`) to skip saving the computed default if it's an empty object unless `includeUndefinedValues` is truthy, fixing [#2150](https://github.com/rjsf-team/react-jsonschema-form/issues/2150) and [#2708](https://github.com/rjsf-team/react-jsonschema-form/issues/2708)
- Expanded the `getDefaultFormState` util's `includeUndefinedValues` prop to accept a boolean or `"excludeObjectChildren"` if it does not want to include undefined values in nested objects

# 5.0.0-beta.13

## @rjsf/playground
Expand All @@ -34,9 +41,6 @@ should change the heading of the (upcoming) version to include a major version b
- For JSON Schemas with `$id`s, use a pre-compiled Ajv validation function when available.
- No longer fail to validate inner schemas with `$id`s, fixing [#2821](https://github.com/rjsf-team/react-jsonschema-form/issues/2181).

## @rjsf/antd
- No longer render extra 0 for array without errors, fixing [#3233](https://github.com/rjsf-team/react-jsonschema-form/issues/3233)

# 5.0.0-beta.12

## @rjsf/antd
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/utility-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ Returns the superset of `formData` that includes the given set updated to includ
- theSchema: S - The schema for which the default state is desired
- [formData]: T - The current formData, if any, onto which to provide any missing defaults
- [rootSchema]: S - The root schema, used to primarily to look up `$ref`s
- [includeUndefinedValues=false]: boolean | "once" - Optional flag, if true, cause undefined values to be added as defaults. If "once", cause undefined values to be added only
- [includeUndefinedValues=false]: boolean | "excludeObjectChildren" - Optional flag, if true, cause undefined values to be added as defaults. If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested object properties.

#### Returns
- T: The resulting `formData` with all the defaults provided
Expand Down
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 @@ -134,7 +134,7 @@ class AnyOfField<
schemaUtils.getDefaultFormState(
options[selectedOption],
newFormData,
"once"
"excludeObjectChildren"
) as T,
undefined,
this.getFieldId()
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/src/createSchemaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ class SchemaUtils<
*
* @param schema - The schema for which the default state is desired
* @param [formData] - The current formData, if any, onto which to provide any missing defaults
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
* If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
* object properties.
* @returns - The resulting `formData` with all the defaults provided
*/
getDefaultFormState(
schema: S,
formData?: T,
includeUndefinedValues: boolean | "once" = false
includeUndefinedValues: boolean | "excludeObjectChildren" = false
): T | T[] | undefined {
return getDefaultFormState<T, S>(
this.validator,
Expand Down
16 changes: 11 additions & 5 deletions packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export function getInnerSchemaForArrayItem<
* @param [parentDefaults] - Any defaults provided by the parent field in the schema
* @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
* @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
* If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
* object properties.
* @returns - The resulting `formData` with all the defaults provided
*/
export function computeDefaults<
Expand All @@ -98,7 +100,7 @@ export function computeDefaults<
parentDefaults?: T,
rootSchema: S = {} as S,
rawFormData?: T,
includeUndefinedValues: boolean | "once" = false
includeUndefinedValues: boolean | "excludeObjectChildren" = false
): T | T[] | undefined {
const formData = isObject(rawFormData) ? rawFormData : {};
// Compute the defaults recursively: give highest priority to deepest nodes.
Expand Down Expand Up @@ -187,7 +189,9 @@ export function computeDefaults<
get(defaults, [key]),
rootSchema,
get(formData, [key]),
includeUndefinedValues === "once" ? false : includeUndefinedValues
includeUndefinedValues === "excludeObjectChildren"
? false
: includeUndefinedValues
);
if (includeUndefinedValues) {
acc[key] = computedDefault;
Expand Down Expand Up @@ -269,7 +273,9 @@ export function computeDefaults<
* @param theSchema - The schema for which the default state is desired
* @param [formData] - The current formData, if any, onto which to provide any missing defaults
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
* If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
* object properties.
* @returns - The resulting `formData` with all the defaults provided
*/
export default function getDefaultFormState<
Expand All @@ -280,7 +286,7 @@ export default function getDefaultFormState<
theSchema: S,
formData?: T,
rootSchema?: S,
includeUndefinedValues: boolean | "once" = false
includeUndefinedValues: boolean | "excludeObjectChildren" = false
) {
if (!isObject(theSchema)) {
throw new Error("Invalid schema: " + theSchema);
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -951,13 +951,15 @@ export interface SchemaUtilsType<
*
* @param schema - The schema for which the default state is desired
* @param [formData] - The current formData, if any, onto which to provide any missing defaults
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
* If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
* object properties.
* @returns - The resulting `formData` with all the defaults provided
*/
getDefaultFormState(
schema: S,
formData?: T,
includeUndefinedValues?: boolean | "once"
includeUndefinedValues?: boolean | "excludeObjectChildren"
): T | T[] | undefined;
/** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
* should be displayed in a UI.
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/test/schema/getDefaultFormStateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default function getDefaultFormStateTest(
requiredProperty: "foo",
});
});
it("test computeDefaults that is passed an object with an optional object property that has a nested required property and includeUndefinedValues is 'once'", () => {
it("test computeDefaults that is passed an object with an optional object property that has a nested required property and includeUndefinedValues is 'excludeObjectChildren'", () => {
const schema: RJSFSchema = {
type: "object",
properties: {
Expand Down Expand Up @@ -142,7 +142,7 @@ export default function getDefaultFormStateTest(
undefined,
schema,
undefined,
"once"
"excludeObjectChildren"
)
).toEqual({
optionalProperty: {
Expand Down
2 changes: 1 addition & 1 deletion packages/validator-ajv6/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export default class AJV6Validator<T = any>
return { errors, errorSchema };
}

// Include form data with undefined values, which is required for validation.
// Include form data with undefined values, which is required for custom validation.
const newFormData = getDefaultFormState<T, RJSFSchema>(
this,
schema,
Expand Down
2 changes: 1 addition & 1 deletion packages/validator-ajv8/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export default class AJV8Validator<
return { errors, errorSchema };
}

// Include form data with undefined values, which is required for validation.
// Include form data with undefined values, which is required for custom validation.
const newFormData = getDefaultFormState<T>(
this,
schema,
Expand Down

0 comments on commit d1c69f7

Please sign in to comment.