Skip to content

Commit 0c7cdf1

Browse files
committed
1 parent 04b7f96 commit 0c7cdf1

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

src/utils.js

+17
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,22 @@ export function toError(message) {
1414
}
1515
}
1616

17+
export function isArray(field, schema) {
18+
return schema.properties[field] && schema.properties[field].type === "array";
19+
}
20+
21+
export function extractRefSchema(field, schema) {
22+
if (isArray(field, schema)) {
23+
let relevantSchema =
24+
schema.properties[field].items && schema.properties[field].items["$ref"]
25+
? schema.properties[field].items["$ref"].split("/")
26+
: [];
27+
return relevantSchema
28+
.filter(ref => ref !== "#")
29+
.reduce((schema, field) => schema[field], schema);
30+
}
31+
return undefined;
32+
}
33+
1734
const concat = (x, y) => x.concat(y);
1835
export const flatMap = (xs, f) => xs.map(f).reduce(concat, []);

src/validation.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import predicate from "predicate";
2-
import { flatMap, isObject, toError } from "./utils";
2+
import { flatMap, isObject, toError, isArray, extractRefSchema } from "./utils";
33
import { OR, AND, NOT } from "./constants";
44

55
export function predicatesFromRule(rule, schema) {
@@ -45,11 +45,18 @@ export function predicatesFromCondition(condition, schema) {
4545
} else {
4646
// TODO disable validation of nested structures
4747
let isField = schema.properties[ref] !== undefined;
48-
let isArray = isField && schema.properties[ref].type === "array";
49-
if (isField && !isArray) {
48+
let isFieldAnArray = isArray(ref, schema);
49+
console.log(`${ref} is field ${isField} and array ${isFieldAnArray}`);
50+
if (isField && !isFieldAnArray) {
5051
return predicatesFromRule(refVal, schema);
5152
} else {
52-
return [];
53+
if (isFieldAnArray) {
54+
let refSchema = extractRefSchema(ref, schema);
55+
console.log(`Ref schema ${JSON.stringify(refSchema)}`);
56+
return refSchema ? predicatesFromCondition(refVal, refSchema) : [];
57+
} else {
58+
return [];
59+
}
5360
}
5461
}
5562
});

test/utils.test.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { flatMap, isObject, isDevelopment, toError } from "../src/utils";
1+
import {
2+
flatMap,
3+
isObject,
4+
isDevelopment,
5+
toError,
6+
extractRefSchema,
7+
isArray,
8+
} from "../src/utils";
29
import { testInProd } from "./utils";
310

411
test("array flatmap", () => {
@@ -22,3 +29,39 @@ test("error throws exception", () => {
2229
expect(() => toError("Yes")).toThrow();
2330
expect(testInProd(() => toError("Yes"))).toBeUndefined();
2431
});
32+
33+
test("extract referenced schema", () => {
34+
let schema = {
35+
definitions: {
36+
medication: {
37+
type: "object",
38+
properties: {
39+
type: { type: "string" },
40+
isLiquid: { type: "boolean" },
41+
},
42+
},
43+
},
44+
type: "object",
45+
required: ["medications", "firstName", "lastName"],
46+
properties: {
47+
firstName: {
48+
type: "string",
49+
},
50+
lastName: {
51+
type: "string",
52+
},
53+
medications: {
54+
type: "array",
55+
items: { $ref: "#/definitions/medication" },
56+
},
57+
primaryMedication: {
58+
$ref: "#/definitions/medications",
59+
},
60+
},
61+
};
62+
63+
expect(isArray("medications", schema)).toBeTruthy();
64+
expect(extractRefSchema("medications", schema)).toEqual(
65+
schema.definitions.medication
66+
);
67+
});

test/validation.nestedFields.test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Engine from "../src/Engine";
2+
import { listAllPredicates } from "../src/validation";
23

34
let rules = [
45
{
56
conditions: {
67
medications: {
7-
type: { equal: "D" },
8+
type: { is: "D" },
89
},
910
"primaryMedication.type": { equal: "C" },
1011
},
@@ -35,14 +36,20 @@ let schema = {
3536
},
3637
medications: {
3738
type: "array",
38-
items: { $ref: "#/definitions/hobby" },
39+
items: { $ref: "#/definitions/medications" },
3940
},
4041
primaryMedication: {
41-
$ref: "#/definitions/hobby",
42+
$ref: "#/definitions/medications",
4243
},
4344
},
4445
};
4546

47+
test("list all predicates", () => {
48+
expect(listAllPredicates(rules.map(r => r.conditions), schema)).toEqual([
49+
"is",
50+
]);
51+
});
52+
4653
test("valid rules", () => {
4754
expect(new Engine(rules, schema)).not.toBeUndefined();
4855

0 commit comments

Comments
 (0)