Skip to content

Commit

Permalink
fix omit field names with dot #2643 (#3203)
Browse files Browse the repository at this point in the history
* fix omit field names with dot #2643

* add type casting

Co-authored-by: Heath C <51679588+heath-freenome@users.noreply.github.com>
  • Loading branch information
v1ack and heath-freenome committed Oct 26, 2022
1 parent cc0ccae commit 4ef3756
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -23,6 +23,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/core
- BREAKING CHANGE: ShowErrorList prop changed to support `false`, `top` or `bottom`; `true` is no longer a valid value as the default changed from `true` to `top` [#634](https://github.com/rjsf-team/react-jsonschema-form/issues/634)
- Added the new generic, `S extends StrictRJSFSchema = RJSFSchema`, for `schema`/`rootSchema` to every component that needed it.
- Fix omitExtraData with field names with dots #2643

## @rjsf/utils
- Beta-only potentially BREAKING CHANGE: Changed all types that directly or indirectly defined `schema`/`rootSchema` to add the generic `S extends StrictRJSFSchema = RJSFSchema` and use `S` as the type for them.
Expand Down
17 changes: 10 additions & 7 deletions packages/core/src/components/Form.tsx
Expand Up @@ -453,13 +453,17 @@ export default class Form<
* @param formData - The data for the `Form`
* @param fields - The fields to keep while filtering
*/
getUsedFormData = (formData: T, fields: string[]): T => {
getUsedFormData = (formData: T, fields: string[][]): T => {
// For the case of a single input form
if (fields.length === 0 && typeof formData !== "object") {
return formData;
}

const data: GenericObjectType = _pick(formData, fields);
// _pick has incorrect type definition, it works with string[][], because lodash/hasIn supports it
const data: GenericObjectType = _pick(
formData,
fields as unknown as string[]
);
if (Array.isArray(formData)) {
return Object.keys(data).map((key: string) => data[key]) as unknown as T;
}
Expand All @@ -472,15 +476,15 @@ export default class Form<
* @param pathSchema - The `PathSchema` object for the form
* @param formData - The form data to use while checking for empty objects/arrays
*/
getFieldNames = (pathSchema: PathSchema<T>, formData: T) => {
getFieldNames = (pathSchema: PathSchema<T>, formData: T): string[][] => {
const getAllPaths = (
_obj: GenericObjectType,
acc: string[] = [],
paths = [""]
acc: string[][] = [],
paths: string[][] = [[]]
) => {
Object.keys(_obj).forEach((key: string) => {
if (typeof _obj[key] === "object") {
const newPaths = paths.map((path) => `${path}.${key}`);
const newPaths = paths.map((path) => [...path, key]);
// If an object is marked with additionalProperties, all its keys are valid
if (
_obj[key][RJSF_ADDITONAL_PROPERTIES_FLAG] &&
Expand All @@ -492,7 +496,6 @@ export default class Form<
}
} else if (key === NAME_KEY && _obj[key] !== "") {
paths.forEach((path) => {
path = path.replace(/^\./, "");
const formValue = _get(formData, path);
// adds path to fieldNames if it points to a value
// or an empty object/array
Expand Down
22 changes: 11 additions & 11 deletions packages/core/test/Form_test.js
Expand Up @@ -3073,10 +3073,10 @@ describe("Form omitExtraData and liveOmit", () => {
const fieldNames = comp.getFieldNames(pathSchema, formData);
expect(fieldNames.sort()).eql(
[
"level1a",
"level1.level2",
"level1.anotherThing.anotherThingNested",
"level1.anotherThing.anotherThingNested2",
["level1", "anotherThing", "anotherThingNested"],
["level1", "anotherThing", "anotherThingNested2"],
["level1", "level2"],
["level1a"],
].sort()
);
});
Expand Down Expand Up @@ -3124,7 +3124,7 @@ describe("Form omitExtraData and liveOmit", () => {

const fieldNames = comp.getFieldNames(pathSchema, formData);
expect(fieldNames.sort()).eql(
["level1a", "level1.level2", "level1.mixedMap"].sort()
[["level1", "level2"], "level1.mixedMap", ["level1a"]].sort()
);
});

Expand Down Expand Up @@ -3186,12 +3186,12 @@ describe("Form omitExtraData and liveOmit", () => {
const fieldNames = comp.getFieldNames(pathSchema, formData);
expect(fieldNames.sort()).eql(
[
"address_list.0.city",
"address_list.0.state",
"address_list.0.street_address",
"address_list.1.city",
"address_list.1.state",
"address_list.1.street_address",
["address_list", "0", "city"],
["address_list", "0", "state"],
["address_list", "0", "street_address"],
["address_list", "1", "city"],
["address_list", "1", "state"],
["address_list", "1", "street_address"],
].sort()
);
});
Expand Down

0 comments on commit 4ef3756

Please sign in to comment.