Skip to content

Commit cee44a7

Browse files
committed
fix #12
1 parent 1222800 commit cee44a7

File tree

3 files changed

+147
-9
lines changed

3 files changed

+147
-9
lines changed

src/utils.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ function fetchSchema(ref, schema) {
5858
}
5959

6060
export function extractRefSchema(field, schema) {
61-
if (isRefArray(field, schema)) {
62-
return fetchSchema(schema.properties[field].items["$ref"], schema);
63-
} else if (schema.properties[field] && schema.properties[field]["$ref"]) {
64-
return fetchSchema(schema.properties[field]["$ref"], schema);
65-
} else if (
66-
schema.properties[field] &&
67-
schema.properties[field].type === "object"
68-
) {
69-
return schema.properties[field];
61+
let { properties } = schema;
62+
if (!properties || !properties[field]) {
63+
toError(`${field} not defined in properties`);
64+
return undefined;
65+
} else if (properties[field].type === "array") {
66+
if (isRefArray(field, schema)) {
67+
return fetchSchema(properties[field].items["$ref"], schema);
68+
} else {
69+
return properties[field].items;
70+
}
71+
} else if (properties[field] && properties[field]["$ref"]) {
72+
return fetchSchema(properties[field]["$ref"], schema);
73+
} else if (properties[field] && properties[field].type === "object") {
74+
return properties[field];
7075
} else {
7176
toError(`${field} has no $ref field ref schema extraction is impossible`);
7277
return undefined;

test/issues/12.test.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import Engine from "../../src/Engine";
2+
3+
const ADDRESS_SCHEMA = {
4+
type: "object",
5+
properties: {
6+
zip: { type: "string" },
7+
city: { type: "string" },
8+
street: { type: "string" },
9+
flat: { type: "string" },
10+
},
11+
};
12+
13+
const SCHEMA = {
14+
definitions: {
15+
address: ADDRESS_SCHEMA,
16+
},
17+
type: "object",
18+
properties: {
19+
homeAddress: {
20+
$ref: "#/definitions/address",
21+
},
22+
workAddress: ADDRESS_SCHEMA,
23+
favFoodLocations: {
24+
type: "array",
25+
items: {
26+
$ref: "#/definitions/address",
27+
},
28+
},
29+
favoritePlaces: {
30+
type: "array",
31+
items: ADDRESS_SCHEMA,
32+
},
33+
},
34+
};
35+
36+
let engine = new Engine([], SCHEMA);
37+
38+
test("invalidates ref object", () => {
39+
expect(() =>
40+
engine.addRule({
41+
conditions: {
42+
"homeAddress.home": { is: "true" },
43+
},
44+
})
45+
).toThrow();
46+
});
47+
48+
test("invalidates embedded object", () => {
49+
expect(() =>
50+
engine.addRule({
51+
conditions: {
52+
"workAddress.home": { is: "true" },
53+
},
54+
})
55+
).toThrow();
56+
});
57+
58+
test("invalidates array object", () => {
59+
expect(() =>
60+
engine.addRule({
61+
conditions: {
62+
"favFoodLocations.home": { is: "true" },
63+
},
64+
})
65+
).toThrow();
66+
});
67+
68+
test("invalidates array with $ref object", () => {
69+
expect(() =>
70+
engine.addRule({
71+
conditions: {
72+
"favoritePlaces.home": { is: "true" },
73+
},
74+
})
75+
).toThrow();
76+
});
77+
78+
test("Validates ref object", () => {
79+
expect(
80+
engine.addRule({
81+
conditions: {
82+
"homeAddress.zip": { is: "true" },
83+
},
84+
})
85+
).toBeUndefined();
86+
});
87+
88+
test("Validates embedded object", () => {
89+
expect(
90+
engine.addRule({
91+
conditions: {
92+
"workAddress.zip": { is: "true" },
93+
},
94+
})
95+
).toBeUndefined();
96+
});
97+
98+
test("Validates array object", () => {
99+
expect(
100+
engine.addRule({
101+
conditions: {
102+
"favFoodLocations.zip": { is: "true" },
103+
},
104+
})
105+
).toBeUndefined();
106+
});
107+
108+
test("Validates array with $ref object", () => {
109+
expect(
110+
engine.addRule({
111+
conditions: {
112+
"favoritePlaces.zip": { is: "true" },
113+
},
114+
})
115+
).toBeUndefined();
116+
});

test/validation.ref.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ let schema = {
3030
externalConfig: {
3131
$ref: "http://example.com/oneschema.json",
3232
},
33+
invalidArrayRef: {
34+
type: "array",
35+
items: {
36+
$ref: "http://example.com/oneschema.json",
37+
},
38+
},
3339
},
3440
};
3541

@@ -44,6 +50,17 @@ test("condition with external ref", () => {
4450
).toEqual([]);
4551
});
4652

53+
test("array condition with external ref", () => {
54+
expect(() =>
55+
predicatesFromCondition({ "invalidArrayRef.name": "empty" }, schema)
56+
).toThrow();
57+
expect(
58+
testInProd(() =>
59+
predicatesFromCondition({ "invalidArrayRef.name": "empty" }, schema)
60+
)
61+
).toEqual([]);
62+
});
63+
4764
test("condition with fake ref field", () => {
4865
expect(() =>
4966
predicatesFromCondition({ "fakeRef.name": "empty" }, schema)

0 commit comments

Comments
 (0)