-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix 3759 - uneditable & permanent defaults with additional properties #4490
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
119bfc9
a9497a6
395d066
7b59146
adb0597
06255ab
6b1354a
a0e64aa
9a6426d
8c400bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,7 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo | |
* | ||
* @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 initialDefaultsGenerated - Indicates whether or not initial defaults have been generated | ||
* @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. | ||
|
@@ -111,13 +112,15 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo | |
getDefaultFormState( | ||
schema: S, | ||
formData?: T, | ||
initialDefaultsGenerated?: boolean, | ||
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. @Piotr-Debicki Unfortunately, unless you add it to the end of the function call, you are introducing a breaking change. This will need to be the LAST, optional, parameter to the function. |
||
includeUndefinedValues: boolean | 'excludeObjectChildren' = false | ||
): T | T[] | undefined { | ||
return getDefaultFormState<T, S, F>( | ||
this.validator, | ||
schema, | ||
formData, | ||
this.rootSchema, | ||
initialDefaultsGenerated, | ||
includeUndefinedValues, | ||
this.experimental_defaultFormStateBehavior, | ||
this.experimental_customMergeAllOf | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,8 @@ interface ComputeDefaultsProps<T = any, S extends StrictRJSFSchema = RJSFSchema> | |
* The formData should take precedence unless it's not valid. This is useful when for example the value from formData does not exist in the schema 'enum' property, in such cases we take the value from the defaults because the value from the formData is not valid. | ||
*/ | ||
shouldMergeDefaultsIntoFormData?: boolean; | ||
/** Indicates whether initial defaults have been generated */ | ||
initialDefaultsGenerated?: boolean; | ||
} | ||
|
||
/** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into | ||
|
@@ -203,6 +205,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema | |
experimental_customMergeAllOf = undefined, | ||
required, | ||
shouldMergeDefaultsIntoFormData = false, | ||
initialDefaultsGenerated, | ||
} = computeDefaultsProps; | ||
const formData: T = (isObject(rawFormData) ? rawFormData : {}) as T; | ||
const schema: S = isObject(rawSchema) ? rawSchema : ({} as S); | ||
|
@@ -323,6 +326,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema | |
rawFormData: formData as T, | ||
required, | ||
shouldMergeDefaultsIntoFormData, | ||
initialDefaultsGenerated, | ||
}); | ||
} | ||
|
||
|
@@ -417,6 +421,7 @@ export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSche | |
experimental_customMergeAllOf = undefined, | ||
required, | ||
shouldMergeDefaultsIntoFormData, | ||
initialDefaultsGenerated, | ||
}: ComputeDefaultsProps<T, S> = {}, | ||
defaults?: T | T[] | undefined | ||
): T { | ||
|
@@ -452,6 +457,7 @@ export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSche | |
rawFormData: get(formData, [key]), | ||
required: retrievedSchema.required?.includes(key), | ||
shouldMergeDefaultsIntoFormData, | ||
initialDefaultsGenerated, | ||
}); | ||
maybeAddDefaultToObject<T>( | ||
acc, | ||
|
@@ -467,7 +473,7 @@ export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSche | |
}, | ||
{} | ||
) as T; | ||
if (retrievedSchema.additionalProperties) { | ||
if (retrievedSchema.additionalProperties && !initialDefaultsGenerated) { | ||
// as per spec additionalProperties may be either schema or boolean | ||
const additionalPropertiesSchema = isObject(retrievedSchema.additionalProperties) | ||
? retrievedSchema.additionalProperties | ||
|
@@ -497,6 +503,7 @@ export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSche | |
rawFormData: get(formData, [key]), | ||
required: retrievedSchema.required?.includes(key), | ||
shouldMergeDefaultsIntoFormData, | ||
initialDefaultsGenerated, | ||
}); | ||
// Since these are additional properties we don't need to add the `experimental_defaultFormStateBehavior` prop | ||
maybeAddDefaultToObject<T>( | ||
|
@@ -532,6 +539,7 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem | |
experimental_customMergeAllOf = undefined, | ||
required, | ||
shouldMergeDefaultsIntoFormData, | ||
initialDefaultsGenerated, | ||
}: ComputeDefaultsProps<T, S> = {}, | ||
defaults?: T | T[] | undefined | ||
): T | T[] | undefined { | ||
|
@@ -560,6 +568,7 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem | |
parentDefaults: item, | ||
required, | ||
shouldMergeDefaultsIntoFormData, | ||
initialDefaultsGenerated, | ||
}); | ||
}) as T[]; | ||
} | ||
|
@@ -580,6 +589,7 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem | |
parentDefaults: get(defaults, [idx]), | ||
required, | ||
shouldMergeDefaultsIntoFormData, | ||
initialDefaultsGenerated, | ||
}); | ||
}) as T[]; | ||
|
||
|
@@ -629,6 +639,7 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem | |
experimental_customMergeAllOf, | ||
required, | ||
shouldMergeDefaultsIntoFormData, | ||
initialDefaultsGenerated, | ||
}) | ||
) as T[]; | ||
// then fill up the rest with either the item default or empty, up to minItems | ||
|
@@ -671,6 +682,7 @@ export function getDefaultBasedOnSchemaType< | |
* @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 initialDefaultsGenerated - Indicates whether or not initial defaults have been generated | ||
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults. | ||
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as | ||
* false when computing defaults for any nested object properties. | ||
|
@@ -687,6 +699,7 @@ export default function getDefaultFormState< | |
theSchema: S, | ||
formData?: T, | ||
rootSchema?: S, | ||
initialDefaultsGenerated?: boolean, | ||
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. @Piotr-Debicki Unfortunately, unless you add it to the end of the function call, you are introducing a breaking change. This will need to be the LAST, optional, parameter to the function. |
||
includeUndefinedValues: boolean | 'excludeObjectChildren' = false, | ||
experimental_defaultFormStateBehavior?: Experimental_DefaultFormStateBehavior, | ||
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S> | ||
|
@@ -706,6 +719,7 @@ export default function getDefaultFormState< | |
experimental_customMergeAllOf, | ||
rawFormData: formData, | ||
shouldMergeDefaultsIntoFormData: true, | ||
initialDefaultsGenerated, | ||
}); | ||
|
||
// If the formData is an object or an array, add additional properties from formData and override formData with | ||
|
Uh oh!
There was an error while loading. Please reload this page.