-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathproperties.test.ts
111 lines (91 loc) · 3.36 KB
/
properties.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { strict as assert } from "assert";
import { compileSchema } from "../compileSchema";
describe("keyword : properties : get", () => {
it("should step into properties without data", () => {
const node = compileSchema({
type: "object",
properties: {
header: { type: "string", minLength: 1 }
}
});
const schema = node.getNodeChild("header")?.node?.schema;
assert.deepEqual(schema, { type: "string", minLength: 1 });
});
it("should step into properties", () => {
const node = compileSchema({
type: "object",
properties: {
header: { type: "string", minLength: 1 }
}
});
const schema = node.getNodeChild("header", { header: "huhu" })?.node?.schema;
assert.deepEqual(schema, { type: "string", minLength: 1 });
});
it("should step into nested properties", () => {
const node = compileSchema({
type: "object",
properties: {
header: {
type: "object",
properties: {
title: { type: "string", minLength: 1 }
}
}
}
});
const { node: next } = node.getNodeChild("header", { header: { title: "huhu" } });
const schema = next.getNodeChild("title")?.node?.schema;
assert.deepEqual(schema, { type: "string", minLength: 1 });
});
it("should step into properties with if-then present", () => {
const node = compileSchema({
type: "object",
properties: {
withHeader: { type: "boolean", default: true }
},
if: { required: ["withHeader"], properties: { withHeader: { const: true } } },
then: { allOf: [{ required: ["header"], properties: { header: { type: "string", minLength: 1 } } }] }
});
const schema = node.getNodeChild("withHeader", { withHeader: false })?.node?.schema;
assert.deepEqual(schema, { type: "boolean", default: true });
});
});
describe("keyword : properties : validate", () => {
it("should validate matching property type", () => {
const { errors } = compileSchema({
type: "object",
properties: {
header: { type: "string", minLength: 1 }
}
}).validate({ header: "123" });
assert.deepEqual(errors.length, 0);
});
it("should validate boolean schema `true`", () => {
const { errors } = compileSchema({
type: "object",
properties: {
header: true
}
}).validate({ header: "123" });
assert.deepEqual(errors.length, 0);
});
it("should validate boolean schema `false` if property not given", () => {
const { errors } = compileSchema({
type: "object",
properties: {
header: true,
missing: false
}
}).validate({ header: "123" });
assert.deepEqual(errors.length, 0);
});
it("should NOT validate boolean schema `false`", () => {
const { errors } = compileSchema({
type: "object",
properties: {
header: false
}
}).validate({ header: "123" });
assert.deepEqual(errors.length, 1);
});
});