Skip to content

Commit b429bdd

Browse files
yang2007chunn1k0
authored andcommitted
Allow using field names containing a dot character (rjsf-team#397)
1 parent 83253b8 commit b429bdd

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"dependencies": {
3232
"jsonschema": "^1.0.2",
33+
"lodash.topath": "^4.5.2",
3334
"setimmediate": "^1.0.5"
3435
},
3536
"devDependencies": {
@@ -49,7 +50,6 @@
4950
"chai": "^3.3.0",
5051
"cross-env": "^2.0.1",
5152
"css-loader": "^0.23.1",
52-
"eslint": "^2.9.0",
5353
"eslint-plugin-react": "^4.2.3",
5454
"estraverse": "^4.2.0",
5555
"estraverse-fb": "^1.3.1",

src/validate.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
1+
import toPath from 'lodash.topath';
12
import {validate as jsonValidate} from "jsonschema";
23

34
import {isObject, mergeObjects} from "./utils";
45

5-
6-
const RE_ERROR_ARRAY_PATH = /\[\d+]/g;
7-
8-
function errorPropertyToPath(property) {
9-
// Parse array indices, eg. "instance.level1.level2[2].level3"
10-
// => ["instance", "level1", "level2", 2, "level3"]
11-
return property.split(".").reduce((path, node) => {
12-
const match = node.match(RE_ERROR_ARRAY_PATH);
13-
if (match) {
14-
const nodeName = node.slice(0, node.indexOf("["));
15-
const indices = match.map(str => parseInt(str.slice(1, -1), 10));
16-
path = path.concat(nodeName, indices);
17-
} else {
18-
path.push(node);
19-
}
20-
return path;
21-
}, []);
22-
}
23-
246
function toErrorSchema(errors) {
257
// Transforms a jsonschema validation errors list:
268
// [
@@ -42,7 +24,7 @@ function toErrorSchema(errors) {
4224
}
4325
return errors.reduce((errorSchema, error) => {
4426
const {property, message} = error;
45-
const path = errorPropertyToPath(property);
27+
const path = toPath(property);
4628
let parent = errorSchema;
4729
for (const segment of path.slice(1)) {
4830
if (!(segment in parent)) {

test/validate_test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,31 @@ import {createFormComponent} from "./test_utils";
99
describe("Validation", () => {
1010
describe("validate.validateFormData()", () => {
1111
describe("No custom validate function", () => {
12+
const illFormedKey = "bar.'\"[]()=+*&^%$#@!";
1213
const schema = {
1314
type: "object",
14-
properties: {
15-
foo: {type: "string"}
16-
}
15+
properties: { foo: {type: "string"}, [illFormedKey]: {type: "string"} }
1716
};
1817

1918
let errors, errorSchema;
2019

2120
beforeEach(() => {
22-
const result = validateFormData({foo: 42}, schema);
21+
const result = validateFormData({ foo: 42, [illFormedKey]: 41 }, schema);
2322
errors = result.errors;
2423
errorSchema = result.errorSchema;
2524
});
2625

2726
it("should return an error list", () => {
28-
expect(errors).to.have.length.of(1);
27+
expect(errors).to.have.length.of(2);
2928
expect(errors[0].message).eql("is not of a type(s) string");
29+
expect(errors[1].message).eql("is not of a type(s) string");
3030
});
3131

3232
it("should return an errorSchema", () => {
3333
expect(errorSchema.foo.__errors).to.have.length.of(1);
3434
expect(errorSchema.foo.__errors[0]).eql("is not of a type(s) string");
35+
expect(errorSchema[illFormedKey].__errors).to.have.length.of(1);
36+
expect(errorSchema[illFormedKey].__errors[0]).eql("is not of a type(s) string");
3537
});
3638
});
3739

0 commit comments

Comments
 (0)