@rjsf/utils schema functions take a SchemaContext in place of the validator
Detect
- In your code, a call to an
@rjsf/utilsschema function that passes avalidatorfirst:findFieldInSchema(),findSelectedOptionInXxxOf(),getClosestMatchingOption(),getDisplayLabel(),getFirstMatchingOption(),getFromSchema(),isFilesArray(),isMultiSelect(),isSelect(),omitExtraData(),retrieveSchema()orsanitizeDataForNewSchema(). - A call to
createSchemaUtils()ordoesSchemaUtilsDiffer(). In development, acreateSchemaUtils()call that passes a validator in place of theSchemaContextlogs a warning ending in[create-schema-utils-validator-arg]. - A
computeSkipPopulatecallback indefaultFormStateBehavior.arrayMinItemsthat uses its first argument as a validator. - The type checker reports
Property 'validator' is missing in type 'ValidatorType<…>' but required in type 'SchemaContext<…>'.(TS2741) on the first argument of each such call.
Change
The validator, the form's customMergeAllOf and its defaultFormStateBehavior now travel together in a SchemaContext:
import {
CustomMergeAllOf,
DefaultFormStateBehavior,
FormContextType,
RJSFSchema,
StrictRJSFSchema,
ValidatorType,
} from '@rjsf/utils';
interface SchemaContext<S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = FormContextType> {
readonly validator: ValidatorType<S, F>;
readonly customMergeAllOf?: CustomMergeAllOf<S>;
readonly defaultFormStateBehavior?: DefaultFormStateBehavior;
}
Every function that took a validator takes a SchemaContext in its place, and passes it along whole to every schema function it calls. The separate customMergeAllOf and defaultFormStateBehavior parameters are gone, and the parameters that followed them move up a position; every other parameter, rootSchema included, is unchanged:
-const resolvedSchema = retrieveSchema(validator, schema, rootSchema, formData, customMergeAllOf);
-const isSelectField = isSelect(validator, schema, rootSchema, customMergeAllOf);
+const context: SchemaContext = { validator, customMergeAllOf, defaultFormStateBehavior };
+const resolvedSchema = retrieveSchema(context, schema, rootSchema, formData);
+const isSelectField = isSelect(context, schema, rootSchema);
| Function | v6 parameters | v7 parameters |
|---|---|---|
findFieldInSchema() | validator, rootSchema, schema, path, formData?, customMergeAllOf? | context, rootSchema, schema, path, formData? |
findSelectedOptionInXxxOf() | validator, rootSchema, schema, fallbackField, xxx, formData?, customMergeAllOf? | context, rootSchema, schema, fallbackField, xxx, formData? |
getClosestMatchingOption() | validator, rootSchema, formData, options, selectedOption?, discriminatorField?, customMergeAllOf? | context, rootSchema, formData, options, selectedOption?, discriminatorField?, offeredBy? |
getDisplayLabel() | validator, schema, uiSchema?, rootSchema?, globalOptions?, customMergeAllOf? | context, schema, uiSchema?, rootSchema?, globalOptions? |
getFirstMatchingOption() | validator, formData, options, rootSchema, discriminatorField? | context, formData, options, rootSchema, discriminatorField? |
getFromSchema() | validator, rootSchema, schema, path, defaultValue, customMergeAllOf? | context, rootSchema, schema, path, defaultValue |
isFilesArray() | validator, schema, uiSchema?, rootSchema?, customMergeAllOf? | context, schema, uiSchema?, rootSchema? |
isMultiSelect() | validator, schema, rootSchema?, customMergeAllOf? | context, schema, rootSchema? |
isSelect() | validator, theSchema, rootSchema?, customMergeAllOf? | context, theSchema, rootSchema? |
omitExtraData() | validator, schema, rootSchema?, formData?, customMergeAllOf? | context, schema, rootSchema?, formData?, fieldState? |
retrieveSchema() | validator, schema, rootSchema?, rawFormData?, customMergeAllOf?, resolveAnyOfOrOneOfRefs? | context, schema, rootSchema?, rawFormData?, resolveAnyOfOrOneOfRefs?, selectedOption? |
sanitizeDataForNewSchema() | validator, rootSchema, newSchema?, oldSchema?, data?, customMergeAllOf? | context, rootSchema, newSchema?, oldSchema?, data? |
Double-check retrieveSchema(): a v6 call passing resolveAnyOfOrOneOfRefs passed customMergeAllOf (or undefined) before it, so drop that argument instead of only replacing the validator.
createSchemaUtils() and SchemaUtilsType.doesSchemaUtilsDiffer() take a SchemaContext and the rootSchema:
-const schemaUtils = createSchemaUtils(validator, rootSchema, defaultFormStateBehavior, customMergeAllOf);
+const schemaUtils = createSchemaUtils({ validator, defaultFormStateBehavior, customMergeAllOf }, rootSchema);
setWarningOptions() can make the create-schema-utils-validator-arg warning throw.
SchemaUtilsType's own methods supply the context and root schema themselves, so they are unchanged.
The computeSkipPopulate() callback of defaultFormStateBehavior.arrayMinItems receives the SchemaContext in effect in place of the validator, so any schema function it calls can be handed the form's customMergeAllOf too:
-computeSkipPopulate: (validator, schema, rootSchema) => !isSelect(validator, schema.items, rootSchema),
+computeSkipPopulate: (context, schema, rootSchema) => !createSchemaUtils(context, rootSchema ?? schema).isSelect(schema.items),
getDefaultFormState() takes a props object after the context; see getDefaultFormState() takes a SchemaContext and a props object. The lib/schema/* helpers such as computeDefaults() can no longer be imported; see the entry on default-state internals.
Verify
- The type checker accepts every call.
- A
customMergeAllOfyou pass in the context applies through the whole call chain.
Warns at run time as create-schema-utils-validator-arg.
Also read:
- getDefaultFormState() takes a SchemaContext and a props object
- SchemaUtilsType gains constrainsAlike(), childFillOf(), getFieldDefault() and getFilledDefault()
- A form's customMergeAllOf and defaultFormStateBehavior apply where they used to be dropped
- getDefaultFormState internals and calculateIndexScore have no import path
- Standalone schema functions with a SchemaUtilsType method are deprecated