-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathadditionalProperties.test.ts
175 lines (147 loc) · 6.09 KB
/
additionalProperties.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { strict as assert } from "assert";
import { compileSchema } from "../compileSchema";
describe("keyword : additionalProperties : get", () => {
it("should step into additionalProperties", () => {
const node = compileSchema({
type: "object",
additionalProperties: { type: "string", minLength: 1 }
});
const schema = node.getNodeChild("header", { header: "huhu" })?.node?.schema;
assert.deepEqual(schema, { type: "string", minLength: 1 });
});
it("should NOT step into additionalProperties if false", () => {
const node = compileSchema({
type: "object",
additionalProperties: false
});
const schema = node.getNodeChild("header", { header: "huhu" })?.node?.schema;
assert.deepEqual(schema, undefined);
});
it("should return undefined if additionalProperties is true", () => {
const node = compileSchema({
type: "object",
additionalProperties: true
});
const schema = node.getNodeChild("header", { header: "huhu" })?.node?.schema;
assert.deepEqual(schema, undefined);
});
it("should apply additionalProperties from allOf", () => {
const node = compileSchema({
type: "object",
allOf: [{ additionalProperties: true }]
});
const schema = node.getNodeChild("header", { header: "huhu" })?.node?.schema;
assert.deepEqual(schema, undefined);
});
it("should override additionalProperties from allOf", () => {
const node = compileSchema({
type: "object",
additionalProperties: { type: "number" },
allOf: [{ additionalProperties: { type: "boolean" } }]
});
const schema = node.getNodeChild("header")?.node?.schema;
assert.deepEqual(schema, { type: "boolean" });
});
});
describe("keyword : additionalProperties : validate", () => {
it("should return no-additional-properties-error if no schema is given for an additional property", () => {
const { errors } = compileSchema({ type: "object", additionalProperties: false }).validate({ a: 1 });
assert.deepEqual(errors.length, 1);
});
it("should return error for property not in properties schema", () => {
const { errors } = compileSchema({
$schema: "https://json-schema.org/draft/2019-09/schema",
properties: { foo: { $ref: "#" } },
additionalProperties: false
}).validate({ bar: false });
assert.deepEqual(errors.length, 1);
});
it("should return all no-additional-properties-error", () => {
const { errors } = compileSchema({
type: "object",
patternProperties: {
dummy: false
},
additionalProperties: false
}).validate({
a: 1,
b: 2
});
assert.deepEqual(errors.length, 2);
assert.deepEqual(errors[0].code, "no-additional-properties-error");
assert.deepEqual(errors[1].code, "no-additional-properties-error");
});
it("should be valid if 'additionalProperties' is 'true'", () => {
const { errors } = compileSchema({ type: "object", additionalProperties: true }).validate({ a: 1 });
assert.deepEqual(errors.length, 0);
});
it("should be valid if value matches 'additionalProperties' schema", () => {
const { errors } = compileSchema({
type: "object",
properties: { b: { type: "string" } },
additionalProperties: { type: "number" }
}).validate({ a: 1 });
assert.deepEqual(errors.length, 0);
});
it("should only validate existing definition in 'properties'", () => {
const { errors } = compileSchema({
type: "object",
properties: { b: { type: "string" } },
additionalProperties: { type: "number" }
}).validate({ b: "i am valid" });
assert.deepEqual(errors.length, 0);
});
it("should return error if value does not match 'additionalProperties' schema", () => {
const { errors } = compileSchema({
type: "object",
properties: { b: { type: "string" } },
additionalProperties: { type: "string" }
}).validate({ a: 1 });
assert.deepEqual(errors.length, 1);
assert.deepEqual(errors[0].type, "error");
});
it("should be valid if value matches 'additionalProperties' oneOf schema", () => {
const { errors } = compileSchema({
type: "object",
properties: { b: { type: "string" } },
additionalProperties: {
oneOf: [{ type: "number" }]
}
}).validate({ a: 1 });
assert.deepEqual(errors.length, 0);
});
it("should be invalid if value does not match 'additionalProperties' in oneOf schema", () => {
const { errors } = compileSchema({
type: "object",
properties: { b: { type: "string" } },
additionalProperties: {
oneOf: [{ type: "string" }]
}
}).validate({ a: 1 });
assert.deepEqual(errors.length, 1);
});
it("should be ignore properties that are matched by patternProperties", () => {
const { errors } = compileSchema({
type: "object",
properties: { b: { type: "string" } },
patternProperties: {
"^.$": { type: "number" }
},
additionalProperties: {
oneOf: [{ type: "string" }]
}
}).validate({ a: 1 });
assert.deepEqual(errors.length, 0);
});
it("should be invalid if value does match multiple 'additionalProperties' in oneOf schema", () => {
const { errors } = compileSchema({
type: "object",
properties: { b: { type: "string" } },
additionalProperties: {
oneOf: [{ type: "string" }, { type: "string" }]
}
}).validate({ a: "a string" });
assert.deepEqual(errors.length, 1);
// assert.deepEqual(errors[0].code, "additional-properties-error");
});
});