Skip to content

Commit 6436257

Browse files
committed
Add documentation
1 parent f8623f9 commit 6436257

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

packages/docs/docs/api-reference/utility-functions.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,67 @@ Generates an `PathSchema` object for the `schema`, recursively
10801080

10811081
- PathSchema<T> - The `PathSchema` object for the `schema`
10821082

1083+
### omitExtraData<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>()
1084+
1085+
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.
1086+
1087+
```ts
1088+
const schema = {
1089+
type: 'object',
1090+
properties: {
1091+
child1: {
1092+
type: 'object',
1093+
properties: {
1094+
name: { type: 'string' },
1095+
},
1096+
},
1097+
child2: {
1098+
type: 'object',
1099+
properties: {
1100+
name: { type: 'string' },
1101+
},
1102+
additionalProperties: true,
1103+
},
1104+
},
1105+
};
1106+
1107+
const formData = {
1108+
child1: {
1109+
name: 'John Doe',
1110+
extraField: 'This should be removed',
1111+
},
1112+
child2: {
1113+
name: 'Jane Doe',
1114+
extraField: 'This should NOT be removed',
1115+
},
1116+
};
1117+
1118+
const filteredFormData = omitExtraData(validator, schema, schema, formData);
1119+
console.log(filteredFormData);
1120+
/*
1121+
{
1122+
child1: {
1123+
name: "John Doe",
1124+
},
1125+
child2: {
1126+
name: "Jane Doe",
1127+
extraField: "This should NOT be removed",
1128+
},
1129+
}
1130+
*/
1131+
```
1132+
1133+
#### Parameters
1134+
1135+
- validator: ValidatorType<T, S, F> - An implementation of the `ValidatorType` interface that will be used when necessary
1136+
- schema: S - The schema to use for filtering the formData
1137+
- [rootSchema]: S | undefined - The root schema, used to primarily to look up `$ref`s
1138+
- [formData]: T | undefined - The formData to filter
1139+
1140+
#### Returns
1141+
1142+
- T: The new form data, with any fields not defined in the schema removed
1143+
10831144
## Schema utils creation function
10841145

10851146
### createSchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>()

packages/utils/src/createSchemaUtils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
277277
return toPathSchema<T, S, F>(this.validator, schema, name, this.rootSchema, formData);
278278
}
279279

280+
/**
281+
* The function takes a `schema` and `formData` and returns a copy of the formData with any fields not defined in the schema removed.
282+
* This is useful for ensuring that only data that is relevant to the schema is preserved. Objects with `additionalProperties`
283+
* keyword set to `true` will not have their extra fields removed.
284+
*
285+
* @param schema - The schema to use for filtering the `formData`
286+
* @param [formData] - The formData to filter
287+
* @returns The new form data, with any fields not defined in the schema removed
288+
*/
280289
omitExtraData(schema: S, formData?: T): T | undefined {
281290
return omitExtraData<T, S, F>(this.validator, schema, this.rootSchema, formData);
282291
}

packages/utils/src/schema/omitExtraData.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import _isEmpty from 'lodash/isEmpty';
66
import toPathSchema from './toPathSchema';
77
import { NAME_KEY, RJSF_ADDITONAL_PROPERTIES_FLAG } from '../constants';
88

9+
/**
10+
* A helper function that extracts all the existing paths from the given `pathSchema` and `formData`.
11+
*
12+
* @param pathSchema - The `PathSchema` from which to extract the field names
13+
* @param [formData] - The formData to use to determine which fields are valid
14+
* @returns A list of paths represented as string arrays
15+
*/
916
export function getFieldNames<T = any>(pathSchema: PathSchema<T>, formData?: T): string[][] {
1017
const getAllPaths = (_obj: GenericObjectType, acc: string[][] = [], paths: string[][] = [[]]) => {
1118
Object.keys(_obj).forEach((key: string) => {
@@ -38,6 +45,12 @@ export function getFieldNames<T = any>(pathSchema: PathSchema<T>, formData?: T):
3845
return getAllPaths(pathSchema);
3946
}
4047

48+
/**
49+
* A helper function to keep only the fields in the `formData` that are defined in the `fields` array.
50+
*
51+
* @param formData - The formData to extract the fields from
52+
* @param fields - The fields to keep in the `formData`
53+
*/
4154
export function getUsedFormData<T = any>(formData: T | undefined, fields: string[][]): T | undefined {
4255
// For the case of a single input form
4356
if (fields.length === 0 && typeof formData !== 'object') {
@@ -53,6 +66,17 @@ export function getUsedFormData<T = any>(formData: T | undefined, fields: string
5366
return data as T;
5467
}
5568

69+
/**
70+
* The function takes a `schema` and `formData` and returns a copy of the formData with any fields not defined in the schema removed.
71+
* This is useful for ensuring that only data that is relevant to the schema is preserved. Objects with `additionalProperties`
72+
* keyword set to `true` will not have their extra fields removed.
73+
*
74+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
75+
* @param schema - The schema to use for filtering the formData
76+
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
77+
* @param [formData] - The formData to filter
78+
* @returns The new form data, with any fields not defined in the schema removed
79+
*/
5680
export default function omitExtraData<
5781
T = any,
5882
S extends StrictRJSFSchema = RJSFSchema,

0 commit comments

Comments
 (0)