Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix omit field names with dot #2643 #3203

Merged
merged 3 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@ should change the heading of the (upcoming) version to include a major version b

## @rjsf/core
- 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
13 changes: 7 additions & 6 deletions packages/core/src/components/Form.tsx
Expand Up @@ -453,12 +453,14 @@ 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;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore _pick has incorrect type definition, it works with string[][], because lodash/hasIn supports it
const data: GenericObjectType = _pick(formData, fields);
v1ack marked this conversation as resolved.
Show resolved Hide resolved
if (Array.isArray(formData)) {
return Object.keys(data).map((key: string) => data[key]) as unknown as T;
Expand All @@ -472,15 +474,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 +494,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