diff --git a/packages/docs/docs/api-reference/utility-functions.md b/packages/docs/docs/api-reference/utility-functions.md index fdee1aa0f6..81c75649a9 100644 --- a/packages/docs/docs/api-reference/utility-functions.md +++ b/packages/docs/docs/api-reference/utility-functions.md @@ -1080,6 +1080,67 @@ Generates an `PathSchema` object for the `schema`, recursively - PathSchema<T> - The `PathSchema` object for the `schema` +### omitExtraData() + +The function takes a schema and formData and returns a copy of the formData with any fields not defined in the schema removed. This is useful for ensuring that only data that is relevant to the schema is preserved. Objects with `additionalProperties` keyword set to `true` will not have their extra fields removed. + +```ts +const schema = { + type: 'object', + properties: { + child1: { + type: 'object', + properties: { + name: { type: 'string' }, + }, + }, + child2: { + type: 'object', + properties: { + name: { type: 'string' }, + }, + additionalProperties: true, + }, + }, +}; + +const formData = { + child1: { + name: 'John Doe', + extraField: 'This should be removed', + }, + child2: { + name: 'Jane Doe', + extraField: 'This should NOT be removed', + }, +}; + +const filteredFormData = omitExtraData(validator, schema, schema, formData); +console.log(filteredFormData); +/* +{ + child1: { + name: "John Doe", + }, + child2: { + name: "Jane Doe", + extraField: "This should NOT be removed", + }, +} +*/ +``` + +#### Parameters + +- validator: ValidatorType - An implementation of the `ValidatorType` interface that will be used when necessary +- schema: S - The schema to use for filtering the formData +- [rootSchema]: S | undefined - The root schema, used to primarily to look up `$ref`s +- [formData]: T | undefined - The formData to filter + +#### Returns + +- T: The new form data, with any fields not defined in the schema removed + ## Schema utils creation function ### createSchemaUtils() diff --git a/packages/utils/src/createSchemaUtils.ts b/packages/utils/src/createSchemaUtils.ts index b8120cdc88..c407bede92 100644 --- a/packages/utils/src/createSchemaUtils.ts +++ b/packages/utils/src/createSchemaUtils.ts @@ -277,6 +277,15 @@ class SchemaUtils(this.validator, schema, name, this.rootSchema, formData); } + /** + * The function takes a `schema` and `formData` and returns a copy of the formData with any fields not defined in the schema removed. + * This is useful for ensuring that only data that is relevant to the schema is preserved. Objects with `additionalProperties` + * keyword set to `true` will not have their extra fields removed. + * + * @param schema - The schema to use for filtering the `formData` + * @param [formData] - The formData to filter + * @returns The new form data, with any fields not defined in the schema removed + */ omitExtraData(schema: S, formData?: T): T | undefined { return omitExtraData(this.validator, schema, this.rootSchema, formData); } diff --git a/packages/utils/src/schema/omitExtraData.ts b/packages/utils/src/schema/omitExtraData.ts index b50ba5a44f..aab70a219b 100644 --- a/packages/utils/src/schema/omitExtraData.ts +++ b/packages/utils/src/schema/omitExtraData.ts @@ -6,6 +6,13 @@ import _isEmpty from 'lodash/isEmpty'; import toPathSchema from './toPathSchema'; import { NAME_KEY, RJSF_ADDITONAL_PROPERTIES_FLAG } from '../constants'; +/** + * A helper function that extracts all the existing paths from the given `pathSchema` and `formData`. + * + * @param pathSchema - The `PathSchema` from which to extract the field names + * @param [formData] - The formData to use to determine which fields are valid + * @returns A list of paths represented as string arrays + */ export function getFieldNames(pathSchema: PathSchema, formData?: T): string[][] { const getAllPaths = (_obj: GenericObjectType, acc: string[][] = [], paths: string[][] = [[]]) => { Object.keys(_obj).forEach((key: string) => { @@ -38,6 +45,12 @@ export function getFieldNames(pathSchema: PathSchema, formData?: T): return getAllPaths(pathSchema); } +/** + * A helper function to keep only the fields in the `formData` that are defined in the `fields` array. + * + * @param formData - The formData to extract the fields from + * @param fields - The fields to keep in the `formData` + */ export function getUsedFormData(formData: T | undefined, fields: string[][]): T | undefined { // For the case of a single input form if (fields.length === 0 && typeof formData !== 'object') { @@ -53,6 +66,17 @@ export function getUsedFormData(formData: T | undefined, fields: string return data as T; } +/** + * The function takes a `schema` and `formData` and returns a copy of the formData with any fields not defined in the schema removed. + * This is useful for ensuring that only data that is relevant to the schema is preserved. Objects with `additionalProperties` + * keyword set to `true` will not have their extra fields removed. + * + * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary + * @param schema - The schema to use for filtering the formData + * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s + * @param [formData] - The formData to filter + * @returns The new form data, with any fields not defined in the schema removed + */ export default function omitExtraData< T = any, S extends StrictRJSFSchema = RJSFSchema,