Skip to content

Commit 4df43e8

Browse files
committed
fix #1
1 parent 0c7cdf1 commit 4df43e8

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

src/utils.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,30 @@ export function toError(message) {
1414
}
1515
}
1616

17-
export function isArray(field, schema) {
18-
return schema.properties[field] && schema.properties[field].type === "array";
17+
export function isRefArray(field, schema) {
18+
return (
19+
schema.properties[field] &&
20+
schema.properties[field].type === "array" &&
21+
schema.properties[field].items &&
22+
schema.properties[field].items["$ref"]
23+
);
24+
}
25+
26+
function fetchSchema(ref, schema) {
27+
let relevantSchema = ref.split("/");
28+
return relevantSchema
29+
.filter(ref => ref !== "#")
30+
.reduce((schema, field) => schema[field], schema);
1931
}
2032

2133
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);
34+
if (isRefArray(field, schema)) {
35+
return fetchSchema(schema.properties[field].items["$ref"], schema);
36+
} else if (schema.properties[field] && schema.properties[field]["$ref"]) {
37+
return fetchSchema(schema.properties[field]["$ref"], schema);
38+
} else {
39+
return undefined;
3040
}
31-
return undefined;
3241
}
3342

3443
const concat = (x, y) => x.concat(y);

src/validation.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import predicate from "predicate";
2-
import { flatMap, isObject, toError, isArray, extractRefSchema } from "./utils";
2+
import {
3+
flatMap,
4+
isObject,
5+
toError,
6+
isRefArray,
7+
extractRefSchema,
8+
} from "./utils";
39
import { OR, AND, NOT } from "./constants";
410

511
export function predicatesFromRule(rule, schema) {
@@ -42,22 +48,27 @@ export function predicatesFromCondition(condition, schema) {
4248
}
4349
} else if (ref === NOT) {
4450
return predicatesFromCondition(refVal, schema);
45-
} else {
46-
// TODO disable validation of nested structures
47-
let isField = schema.properties[ref] !== undefined;
48-
let isFieldAnArray = isArray(ref, schema);
49-
console.log(`${ref} is field ${isField} and array ${isFieldAnArray}`);
50-
if (isField && !isFieldAnArray) {
51-
return predicatesFromRule(refVal, schema);
51+
} else if (ref.indexOf(".") !== -1) {
52+
let separator = ref.indexOf(".");
53+
let schemaField = ref.substr(0, separator);
54+
let subSchema = extractRefSchema(schemaField, schema);
55+
56+
if (subSchema) {
57+
let subSchemaField = ref.substr(separator + 1);
58+
let newCondition = { [subSchemaField]: refVal };
59+
return predicatesFromCondition(newCondition, subSchema);
5260
} else {
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-
}
61+
toError(`Can't find schema for ${schemaField}`);
62+
return [];
6063
}
64+
} else if (isRefArray(ref, schema)) {
65+
let refSchema = extractRefSchema(ref, schema);
66+
return refSchema ? predicatesFromCondition(refVal, refSchema) : [];
67+
} else if (schema.properties[ref]) {
68+
return predicatesFromRule(refVal, schema);
69+
} else {
70+
toError(`Can't validate ${ref}`);
71+
return [];
6172
}
6273
});
6374
}

test/utils.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
isDevelopment,
55
toError,
66
extractRefSchema,
7-
isArray,
7+
isRefArray,
88
} from "../src/utils";
99
import { testInProd } from "./utils";
1010

@@ -55,13 +55,16 @@ test("extract referenced schema", () => {
5555
items: { $ref: "#/definitions/medication" },
5656
},
5757
primaryMedication: {
58-
$ref: "#/definitions/medications",
58+
$ref: "#/definitions/medication",
5959
},
6060
},
6161
};
6262

63-
expect(isArray("medications", schema)).toBeTruthy();
63+
expect(isRefArray("medications", schema)).toBeTruthy();
6464
expect(extractRefSchema("medications", schema)).toEqual(
6565
schema.definitions.medication
6666
);
67+
expect(extractRefSchema("primaryMedication", schema)).toEqual(
68+
schema.definitions.medication
69+
);
6770
});

test/validation.nestedFields.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ let schema = {
4747
test("list all predicates", () => {
4848
expect(listAllPredicates(rules.map(r => r.conditions), schema)).toEqual([
4949
"is",
50+
"equal",
5051
]);
5152
});
5253

0 commit comments

Comments
 (0)