Skip to content

Commit bb51457

Browse files
committed
#15 missed validation update
1 parent 055c17e commit bb51457

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/utils.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import selectn from "selectn";
22

3+
export function normRef(ref) {
4+
return ref.replace(/\$/g, ".");
5+
}
6+
37
export function selectRef(field, formData) {
4-
let ref = field.replace(/\$/g, ".");
8+
let ref = normRef(field);
59
return selectn(ref, formData);
610
}
711

src/validation.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
toError,
66
isRefArray,
77
extractRefSchema,
8+
normRef,
89
} from "./utils";
910
import { OR, AND, NOT } from "./constants";
1011

@@ -51,6 +52,7 @@ export function predicatesFromRule(rule, schema) {
5152
export function predicatesFromCondition(condition, schema) {
5253
return flatMap(Object.keys(condition), ref => {
5354
let refVal = condition[ref];
55+
ref = normRef(ref);
5456
if (ref === OR || ref === AND) {
5557
if (Array.isArray(refVal)) {
5658
return flatMap(refVal, c => predicatesFromCondition(c, schema));
@@ -129,7 +131,7 @@ export function fieldsFromCondition(condition) {
129131
} else if (ref === NOT) {
130132
return fieldsFromCondition(refCondition);
131133
} else {
132-
return [ref].concat(fieldsFromPredicates(refCondition));
134+
return [normRef(ref)].concat(fieldsFromPredicates(refCondition));
133135
}
134136
});
135137
}

test/issues/15.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Engine from "../../src";
2+
import { listInvalidFields } from "../../src/validation";
23

34
test("support $ single level of nesting", () => {
45
let rules = [
@@ -35,3 +36,23 @@ test("support $ double level of nesting", () => {
3536
expect(events[0]).toEqual({ type: "match" });
3637
});
3738
});
39+
40+
test("support $ during validation", () => {
41+
let schema = {
42+
type: "object",
43+
properties: {
44+
address: {
45+
type: "object",
46+
properties: {
47+
zip: { type: "number" },
48+
},
49+
},
50+
},
51+
};
52+
let conditions = [
53+
{
54+
address$zip: { less: 1000 },
55+
},
56+
];
57+
expect(listInvalidFields(conditions, schema)).toEqual([]);
58+
});

0 commit comments

Comments
 (0)