Standalone schema functions with a SchemaUtilsType method are deprecated
Detect
- An import from
@rjsf/utilsof any of these functions:findFieldInSchema,findSelectedOptionInXxxOf,getClosestMatchingOption,getDefaultFormState,getFirstMatchingOption,getFromSchema,isFilesArray,isMultiSelect,isSelect,omitExtraData,retrieveSchema,sanitizeDataForNewSchema. - Your editor strikes the name through, and a lint rule such as
@typescript-eslint/no-deprecatedreports it, as in`retrieveSchema` is deprecated. Kept through v7: call `retrieveSchema()` on the `SchemaUtilsType` that `createSchemaUtils()` returns.
Change
Each function is a method of the same name on SchemaUtilsType. The SchemaUtilsType holds the SchemaContext and the root schema, so the method takes neither.
In a custom field, widget or template, call the method on registry.schemaUtils.
-import { retrieveSchema } from '@rjsf/utils';
-
function MyField(props: FieldProps) {
const { schema, formData, registry } = props;
- const retrieved = retrieveSchema({ validator }, schema, registry.rootSchema, formData);
+ const retrieved = registry.schemaUtils.retrieveSchema(schema, formData);
Outside a form, create the SchemaUtilsType with createSchemaUtils(), once for each SchemaContext and root schema, and call its methods.
-import { getDefaultFormState, retrieveSchema } from '@rjsf/utils';
+import { createSchemaUtils } from '@rjsf/utils';
const context = { validator, customMergeAllOf, defaultFormStateBehavior };
-const retrieved = retrieveSchema(context, schema, rootSchema, formData);
-const defaults = getDefaultFormState(context, { schema, formData, rootSchema, uiSchema });
+const schemaUtils = createSchemaUtils(context, rootSchema);
+const retrieved = schemaUtils.retrieveSchema(schema, formData);
+const defaults = schemaUtils.getDefaultFormState(schema, formData, false, undefined, uiSchema);
Every method keeps the function's other parameters in the function's order. getDefaultFormState() is the exception: the function takes them as one props object, and the method takes them one by one, as schema, formData, includeUndefinedValues, initialDefaultsGenerated, uiSchema, uiSchemaDefinitions and fieldState.
A computeSkipPopulate() callback of defaultFormStateBehavior.arrayMinItems receives the SchemaContext in effect and no SchemaUtilsType. Create one from that context inside the callback.
-computeSkipPopulate: (context, schema, rootSchema) => !isSelect(context, schema.items, rootSchema),
+computeSkipPopulate: (context, schema, rootSchema) => !createSchemaUtils(context, rootSchema ?? schema).isSelect(schema.items),
The defaults always pass the callback its rootSchema, so the ?? schema only satisfies the optional parameter's type.
Where a call left the root schema out, pass the schema itself to createSchemaUtils() as the root. getDefaultFormState() already read the schema as its root, so its result is unchanged. retrieveSchema(), isSelect(), isMultiSelect(), isFilesArray() and omitExtraData() read a missing root as {} and threw on a $ref into the schema, such as #/definitions/a, which the method now resolves against the schema.
A SchemaUtilsType takes the type arguments T, S and F once, and its T is never inferred from the data a method is passed. Pass a call's type arguments to createSchemaUtils(), as in createSchemaUtils<MyData>(context, rootSchema). Without them a method that returns T, such as getDefaultFormState(), omitExtraData() or sanitizeDataForNewSchema(), returns unknown.
The utility functions reference describes createSchemaUtils().
Verify
- No import of the listed functions from
@rjsf/utilsis left. - Each converted call returns what the function returned, except one that left the root schema out and threw on a
$refinto the schema, which now resolves it. - A call moved to
registry.schemaUtilsruns under the form'sSchemaContext. Where it passed a context without the form'scustomMergeAllOfordefaultFormStateBehavior, such as{ validator }, its result now follows them. - A
SchemaUtilsTypeis created where itsSchemaContextor root schema changes, and reused in between.
Also read: