Skip to content

Commit

Permalink
Minor changes: added lodash get to package.json, rename toNameSchema …
Browse files Browse the repository at this point in the history
…to toPathSchema (rjsf-team#1331)
  • Loading branch information
CodeGains authored and epicfaace committed Jun 23, 2019
1 parent a9e950a commit fe62b49
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 39 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@babel/runtime-corejs2": "^7.4.5",
"ajv": "^6.7.0",
"core-js": "^2.5.7",
"lodash.get": "^4.4.2",
"lodash.pick": "^4.4.0",
"lodash.topath": "^4.5.2",
"prop-types": "^15.5.8",
Expand Down
16 changes: 8 additions & 8 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
setState,
getDefaultRegistry,
deepEquals,
toNameSchema,
toPathSchema,
} from "../utils";
import validateFormData, { toErrorList } from "../validate";

Expand Down Expand Up @@ -77,7 +77,7 @@ export default class Form extends Component {
formData,
props.idPrefix
);
const nameSchema = toNameSchema(retrievedSchema, "", definitions, formData);
const pathSchema = toPathSchema(retrievedSchema, "", definitions, formData);
return {
schema,
uiSchema,
Expand All @@ -87,7 +87,7 @@ export default class Form extends Component {
errors,
errorSchema,
additionalMetaSchemas,
nameSchema,
pathSchema,
};
}

Expand Down Expand Up @@ -141,7 +141,7 @@ export default class Form extends Component {
return _pick(formData, fields);
};

getFieldNames = (nameSchema, formData) => {
getFieldNames = (pathSchema, formData) => {
const getAllPaths = (_obj, acc = [], paths = []) => {
Object.keys(_obj).forEach(key => {
if (typeof _obj[key] === "object") {
Expand All @@ -167,7 +167,7 @@ export default class Form extends Component {
return acc;
};

return getAllPaths(nameSchema);
return getAllPaths(pathSchema);
};

onChange = (formData, newErrorSchema) => {
Expand All @@ -179,7 +179,7 @@ export default class Form extends Component {
const newState = this.getStateFromProps(this.props, formData);

const fieldNames = this.getFieldNames(
newState.nameSchema,
newState.pathSchema,
newState.formData
);

Expand Down Expand Up @@ -220,10 +220,10 @@ export default class Form extends Component {
event.persist();
let newFormData = this.state.formData;

const { nameSchema } = this.state;
const { pathSchema } = this.state;

if (this.props.omitExtraData === true) {
const fieldNames = this.getFieldNames(nameSchema, this.state.formData);
const fieldNames = this.getFieldNames(pathSchema, this.state.formData);
newFormData = this.getUsedFormData(this.state.formData, fieldNames);
}

Expand Down
18 changes: 9 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,19 +839,19 @@ export function toIdSchema(
return idSchema;
}

export function toNameSchema(schema, name = "", definitions, formData = {}) {
const nameSchema = {
export function toPathSchema(schema, name = "", definitions, formData = {}) {
const pathSchema = {
$name: name,
};
if ("$ref" in schema || "dependencies" in schema) {
const _schema = retrieveSchema(schema, definitions, formData);
return toNameSchema(_schema, name, definitions, formData);
return toPathSchema(_schema, name, definitions, formData);
}
if ("items" in schema) {
const retVal = {};
if (Array.isArray(formData) && formData.length > 0) {
formData.forEach((element, index) => {
retVal[`${index}`] = toNameSchema(
retVal[`${index}`] = toPathSchema(
schema.items,
`${name}.${index}`,
definitions,
Expand All @@ -862,15 +862,15 @@ export function toNameSchema(schema, name = "", definitions, formData = {}) {
return retVal;
}
if (schema.type !== "object") {
return nameSchema;
return pathSchema;
}
for (const property in schema.properties || {}) {
const field = schema.properties[property];
const fieldId = nameSchema.$name
? nameSchema.$name + "." + property
const fieldId = pathSchema.$name
? pathSchema.$name + "." + property
: property;

nameSchema[property] = toNameSchema(
pathSchema[property] = toPathSchema(
field,
fieldId,
definitions,
Expand All @@ -879,7 +879,7 @@ export function toNameSchema(schema, name = "", definitions, formData = {}) {
(formData || {})[property]
);
}
return nameSchema;
return pathSchema;
}

export function parseDateString(dateString, includeTime = true) {
Expand Down
16 changes: 8 additions & 8 deletions test/Form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,15 +946,15 @@ describe("Form", () => {
onSubmit,
});

const nameSchema = {
const pathSchema = {
$name: "",
};

const fieldNames = comp.getFieldNames(nameSchema, formData);
const fieldNames = comp.getFieldNames(pathSchema, formData);
expect(fieldNames).eql([]);
});

it("should get field names from nameSchema", () => {
it("should get field names from pathSchema", () => {
const schema = {};

const formData = {
Expand All @@ -979,7 +979,7 @@ describe("Form", () => {
onSubmit,
});

const nameSchema = {
const pathSchema = {
$name: "",
level1: {
$name: "level1",
Expand All @@ -999,7 +999,7 @@ describe("Form", () => {
},
};

const fieldNames = comp.getFieldNames(nameSchema, formData);
const fieldNames = comp.getFieldNames(pathSchema, formData);
expect(fieldNames.sort()).eql(
[
"level1a",
Expand All @@ -1010,7 +1010,7 @@ describe("Form", () => {
);
});

it("should get field names from nameSchema with array", () => {
it("should get field names from pathSchema with array", () => {
const schema = {};

const formData = {
Expand All @@ -1035,7 +1035,7 @@ describe("Form", () => {
onSubmit,
});

const nameSchema = {
const pathSchema = {
$name: "",
address_list: {
"0": {
Expand Down Expand Up @@ -1065,7 +1065,7 @@ describe("Form", () => {
},
};

const fieldNames = comp.getFieldNames(nameSchema, formData);
const fieldNames = comp.getFieldNames(pathSchema, formData);
expect(fieldNames.sort()).eql(
[
"address_list.0.city",
Expand Down
28 changes: 14 additions & 14 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
shouldRender,
toDateString,
toIdSchema,
toNameSchema,
toPathSchema,
guessType,
} from "../src/utils";

Expand Down Expand Up @@ -1868,14 +1868,14 @@ describe("utils", () => {
});
});

describe("toNameSchema", () => {
it("should return a nameSchema for root field", () => {
describe("toPathSchema", () => {
it("should return a pathSchema for root field", () => {
const schema = { type: "string" };

expect(toNameSchema(schema)).eql({ $name: "" });
expect(toPathSchema(schema)).eql({ $name: "" });
});

it("should return a nameSchema for nested objects", () => {
it("should return a pathSchema for nested objects", () => {
const schema = {
type: "object",
properties: {
Expand All @@ -1888,7 +1888,7 @@ describe("utils", () => {
},
};

expect(toNameSchema(schema)).eql({
expect(toPathSchema(schema)).eql({
$name: "",
level1: {
$name: "level1",
Expand All @@ -1897,7 +1897,7 @@ describe("utils", () => {
});
});

it("should return a nameSchema for a schema with dependencies", () => {
it("should return a pathSchema for a schema with dependencies", () => {
const schema = {
type: "object",
properties: {
Expand Down Expand Up @@ -1941,7 +1941,7 @@ describe("utils", () => {
],
};

expect(toNameSchema(schema, "", schema.definitions, formData)).eql({
expect(toPathSchema(schema, "", schema.definitions, formData)).eql({
$name: "",
list: {
"0": {
Expand Down Expand Up @@ -1978,7 +1978,7 @@ describe("utils", () => {
});
});

it("should return a nameSchema for a schema with references", () => {
it("should return a pathSchema for a schema with references", () => {
const schema = {
definitions: {
address: {
Expand Down Expand Up @@ -2014,7 +2014,7 @@ describe("utils", () => {
},
};

expect(toNameSchema(schema, "", schema.definitions, formData)).eql({
expect(toPathSchema(schema, "", schema.definitions, formData)).eql({
$name: "",
billing_address: {
$name: "billing_address",
Expand All @@ -2031,7 +2031,7 @@ describe("utils", () => {
});
});

it("should return a nameSchema for a schema with references in an array item", () => {
it("should return a pathSchema for a schema with references in an array item", () => {
const schema = {
definitions: {
address: {
Expand Down Expand Up @@ -2077,7 +2077,7 @@ describe("utils", () => {
],
};

expect(toNameSchema(schema, "", schema.definitions, formData)).eql({
expect(toPathSchema(schema, "", schema.definitions, formData)).eql({
$name: "",
address_list: {
"0": {
Expand Down Expand Up @@ -2108,7 +2108,7 @@ describe("utils", () => {
});
});

it("should return an nameSchema with different types of arrays", () => {
it("should return an pathSchema with different types of arrays", () => {
const schema = {
definitions: {
Thing: {
Expand Down Expand Up @@ -2294,7 +2294,7 @@ describe("utils", () => {
],
};

expect(toNameSchema(schema, "", schema.definitions, formData)).eql({
expect(toPathSchema(schema, "", schema.definitions, formData)).eql({
$name: "",
defaultsAndMinItems: {
"0": {
Expand Down

0 comments on commit fe62b49

Please sign in to comment.