Skip to content

Commit a9acaa3

Browse files
committed
fix #10
1 parent 5018d35 commit a9acaa3

File tree

4 files changed

+120
-12
lines changed

4 files changed

+120
-12
lines changed

src/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export function extractRefSchema(field, schema) {
5151
return fetchSchema(schema.properties[field].items["$ref"], schema);
5252
} else if (schema.properties[field] && schema.properties[field]["$ref"]) {
5353
return fetchSchema(schema.properties[field]["$ref"], schema);
54+
} else if (
55+
schema.properties[field] &&
56+
schema.properties[field].type === "object"
57+
) {
58+
return schema.properties[field];
5459
} else {
5560
toError(`${field} has no $ref field ref schema extraction is impossible`);
5661
return undefined;

test/issues/10.test.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import {
2+
listInvalidPredicates,
3+
validateConditionFields,
4+
validatePredicates,
5+
} from "../../src/validation";
6+
7+
let rules = [
8+
{
9+
title: "Rule #2",
10+
description:
11+
"This hides Address, Email, Gender and the Password fields until First Name and Last Name have a value",
12+
conditions: {
13+
and: [
14+
{
15+
or: [
16+
{ "registration.firstName": "empty" },
17+
{ "registration.lastName": "empty" },
18+
],
19+
},
20+
],
21+
},
22+
event: {
23+
type: "remove",
24+
params: {
25+
field: [
26+
"address",
27+
"registration.gender",
28+
"registration.email",
29+
"registration.password",
30+
"registration.confirmPassword",
31+
],
32+
},
33+
},
34+
},
35+
];
36+
37+
let schema = {
38+
type: "object",
39+
properties: {
40+
registration: {
41+
type: "object",
42+
properties: {
43+
firstName: { type: "string" },
44+
lastName: { type: "string" },
45+
gender: {
46+
type: "string",
47+
enum: ["Male", "Female"],
48+
},
49+
dob: { type: "string" },
50+
email: { type: "string" },
51+
password: { type: "string" },
52+
confirmPassword: { type: "string" },
53+
},
54+
required: [
55+
"firstName",
56+
"lastName",
57+
"email",
58+
"password",
59+
"confirmPassword",
60+
],
61+
},
62+
address: {
63+
type: "object",
64+
properties: {
65+
streetNo: { type: "string" },
66+
street: { type: "string" },
67+
suburb: { type: "string" },
68+
postCode: { type: "string" },
69+
state: {
70+
type: "string",
71+
enum: ["SA", "WA", "NSW", "VIC", "TAS", "ACT", "QLD", "NT"],
72+
},
73+
country: {
74+
type: "string",
75+
enum: [],
76+
},
77+
propertyDescription: {
78+
type: "string",
79+
},
80+
},
81+
},
82+
},
83+
};
84+
85+
test("#10 validation of predicates", () => {
86+
let conditions = rules.map(({ conditions }) => conditions);
87+
expect(listInvalidPredicates(conditions, schema)).toEqual([]);
88+
expect(validatePredicates(conditions, schema)).toBeUndefined();
89+
expect(validateConditionFields(conditions, schema)).toBeUndefined();
90+
});

test/utils.test.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,32 @@ test("extract referenced schema", () => {
6161
externalConfig: {
6262
$ref: "http://example.com/oneschema.json",
6363
},
64+
registration: {
65+
type: "object",
66+
properties: {
67+
firstName: { type: "string" },
68+
lastName: { type: "string" },
69+
},
70+
},
6471
},
6572
};
6673

74+
let { definitions: { medication }, properties: { registration } } = schema;
75+
6776
expect(isRefArray("medications", schema)).toBeTruthy();
68-
expect(extractRefSchema("medications", schema)).toEqual(
69-
schema.definitions.medication
70-
);
71-
expect(extractRefSchema("primaryMedication", schema)).toEqual(
72-
schema.definitions.medication
73-
);
77+
expect(extractRefSchema("medications", schema)).toEqual(medication);
78+
expect(extractRefSchema("primaryMedication", schema)).toEqual(medication);
79+
expect(extractRefSchema("registration", schema)).toEqual(registration);
7480

7581
expect(() => extractRefSchema("externalConfig", schema)).toThrow();
76-
expect(testInProd(() => extractRefSchema("externalConfig", schema))).toEqual(
77-
undefined
78-
);
82+
expect(
83+
testInProd(() => extractRefSchema("externalConfig", schema))
84+
).toBeUndefined();
7985

8086
expect(() => extractRefSchema("lastName", schema)).toThrow();
81-
expect(testInProd(() => extractRefSchema("lastName", schema))).toEqual(
82-
undefined
83-
);
87+
expect(
88+
testInProd(() => extractRefSchema("lastName", schema))
89+
).toBeUndefined();
8490
});
8591

8692
test("array transformation", () => {

test/validation.nestedFields.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ let schema = {
4141
primaryMedication: {
4242
$ref: "#/definitions/medications",
4343
},
44+
registration: {
45+
type: "object",
46+
properties: {
47+
firstName: { type: "string" },
48+
lastName: { type: "string" },
49+
},
50+
},
4451
},
4552
};
4653

0 commit comments

Comments
 (0)