-
-
Notifications
You must be signed in to change notification settings - Fork 878
/
json-schema.spec.ts
355 lines (323 loc) · 9.22 KB
/
json-schema.spec.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import _Ajv from "../ajv"
import type {JSONSchemaType} from "../.."
import type {SchemaObject} from "../.."
import chai from "../chai"
const should = chai.should()
interface MyData {
foo: string
bar?: number // "boo" should be present if "bar" is present
baz: {
quux: "quux"
[x: string]: string
}
boo?: true
tuple?: readonly [number, string]
arr: {id: number}[]
map: {[K in string]?: number}
notBoo?: string // should not be present if "boo" is present
negativeIfBoo?: number // should be negative if "boo" is present
}
const arrSchema: JSONSchemaType<MyData["arr"]> = {
type: "array",
items: {
type: "object",
properties: {
id: {
type: "integer",
},
},
additionalProperties: false,
required: ["id"],
},
uniqueItems: true,
} as const
const mySchema: JSONSchemaType<MyData> & {
definitions: {
baz: JSONSchemaType<MyData["baz"]>
tuple: JSONSchemaType<MyData["tuple"]>
}
} = {
type: "object",
definitions: {
baz: {
// schema type is checked here ...
type: "object",
properties: {
quux: {type: "string", const: "quux"},
},
patternProperties: {
abc: {type: "string"},
},
additionalProperties: false,
required: [],
},
tuple: {
// ... and here ...
type: "array",
items: [{type: "number"}, {type: "string"}],
minItems: 2,
additionalItems: false,
nullable: true,
},
},
dependencies: {
bar: ["boo"],
boo: {
not: {required: ["notBoo"]}, // optional properties can be cheched in "required" in PartialSchema
required: ["negativeIfBoo"],
properties: {
// partial properties can be used in partial schemas
negativeIfBoo: {type: "number", nullable: true, exclusiveMaximum: 0},
},
},
},
properties: {
foo: {type: "string"},
bar: {type: "number", nullable: true},
baz: {$ref: "#/definitions/baz"}, // ... but it does not check type here, ...
boo: {
type: "boolean",
nullable: true,
enum: [true, null],
},
tuple: {$ref: "#/definitions/tuple"}, // ... nor here.
arr: arrSchema, // ... The alternative is to define it externally - here it checks type
map: {
type: "object",
required: [],
additionalProperties: {type: "number"},
},
notBoo: {type: "string", nullable: true},
negativeIfBoo: {type: "number", nullable: true},
},
additionalProperties: false,
required: ["foo", "baz", "arr", "map"], // any other property added here won't typecheck
} as const
type MyUnionData = {a: boolean} | string | number
const myUnionSchema: JSONSchemaType<MyUnionData> = {
anyOf: [
{
type: "object",
properties: {
a: {type: "boolean"},
},
required: ["a"],
},
{
type: ["string", "number"],
// can specify properties for either type
minimum: 0,
minLength: 1,
},
],
} as const
// because of the current definition, you can do this nested recusion
const myNestedUnionSchema: JSONSchemaType<MyUnionData> = {
anyOf: [
{
oneOf: [
{
type: "object",
properties: {
a: {type: "boolean"},
},
required: ["a"],
},
{
type: "string",
},
],
},
{
type: "number",
},
],
} as const
// allowing union types necessarily allows this to pass :/
const emptyType: JSONSchemaType<MyData> = {
type: [],
} as const
type MyEnumRecord = Record<"a" | "b" | "c" | "d", number | undefined>
describe("JSONSchemaType type and validation as a type guard", () => {
const ajv = new _Ajv({allowUnionTypes: true})
const validData: unknown = {
foo: "foo",
bar: 1,
baz: {
quux: "quux",
abc: "abc",
},
boo: true,
arr: [{id: 1}, {id: 2}],
tuple: [1, "abc"],
map: {
a: 1,
b: 2,
},
negativeIfBoo: -1,
}
describe("schema has type JSONSchemaType<MyData>", () => {
it("should prove the type of validated data", () => {
const validate = ajv.compile(mySchema)
if (validate(validData)) {
validData.foo.should.equal("foo")
}
should.not.exist(validate.errors)
if (ajv.validate(mySchema, validData)) {
validData.foo.should.equal("foo")
}
should.not.exist(ajv.errors)
})
})
const validUnionData: unknown = {
a: true,
}
describe("schema has type JSONSchemaType<MyUnionData>", () => {
it("should prove the type of validated data", () => {
const validate = ajv.compile(myUnionSchema)
if (validate(validUnionData)) {
if (typeof validUnionData === "string") {
should.fail("not a string")
} else if (typeof validUnionData === "number") {
should.fail("not a number")
} else {
validUnionData.a.should.equal(true)
}
} else {
should.fail("is valid")
}
should.not.exist(validate.errors)
if (ajv.validate(myUnionSchema, validUnionData)) {
if (typeof validUnionData === "string") {
should.fail("not a string")
} else if (typeof validUnionData === "number") {
should.fail("not a number")
} else {
validUnionData.a.should.equal(true)
}
} else {
should.fail("is valid")
}
should.not.exist(ajv.errors)
})
it("should prove the type of validated nested data", () => {
const validate = ajv.compile(myNestedUnionSchema)
if (validate(validUnionData)) {
if (typeof validUnionData === "string") {
should.fail("not a string")
} else if (typeof validUnionData === "number") {
should.fail("not a number")
} else {
validUnionData.a.should.equal(true)
}
} else {
should.fail("is valid")
}
should.not.exist(validate.errors)
if (ajv.validate(myNestedUnionSchema, validUnionData)) {
if (typeof validUnionData === "string") {
should.fail("not a string")
} else if (typeof validUnionData === "number") {
should.fail("not a number")
} else {
validUnionData.a.should.equal(true)
}
} else {
should.fail("is valid")
}
should.not.exist(ajv.errors)
})
it("should fail for invalid unions", () => {
// @ts-expect-error extra type
const extraSchema: JSONSchemaType<string | number> = {
type: ["string", "number", "boolean"],
} as const
// @ts-expect-error extra properties
const extraProps: JSONSchemaType<string, boolean> = {
type: ["string", "boolean"],
maximum: 5, // number property
} as const
// eslint-disable-next-line no-void
void [extraSchema, extraProps]
})
})
describe("schema has type SchemaObject", () => {
it("should prove the type of validated data", () => {
const schema = mySchema as SchemaObject
const validate = ajv.compile<MyData>(schema)
if (validate(validData)) {
validData.foo.should.equal("foo")
}
should.not.exist(validate.errors)
if (ajv.validate<MyData>(schema, validData)) {
validData.foo.should.equal("foo")
}
should.not.exist(ajv.errors)
})
})
describe("schema should be simple for record types", () => {
it("typechecks a valid type without required", () => {
const myEnumRecordSchema: JSONSchemaType<MyEnumRecord> = {
type: "object",
propertyNames: {enum: ["a", "b", "c", "d"]},
additionalProperty: {type: "number"},
}
// eslint-disable-next-line no-void
void myEnumRecordSchema
})
it("requires required for non-optional types", () => {
// @ts-expect-error missing required
const requiredSchema: JSONSchemaType<{a: number}> = {
type: "object",
}
// eslint-disable-next-line no-void
void requiredSchema
})
it("doesn't require required for optional types", () => {
const optionalSchema: JSONSchemaType<{a?: number}> = {
type: "object",
}
// eslint-disable-next-line no-void
void optionalSchema
})
it("won't accept nullable for non-null types", () => {
// @ts-expect-error can't set nullable
const nonNullSchema: JSONSchemaType<{a: number}> = {
type: "object",
properties: {
a: {
type: "number",
nullable: true,
},
},
required: [],
}
// eslint-disable-next-line no-void
void nonNullSchema
})
})
describe("schema works for primitives", () => {
it("allows partial boolean sub schemas", () => {
// this schema doesn't have much meaning, but it wasn't allowed before
const trueSchema: JSONSchemaType<true> = {
type: "boolean",
not: {const: false},
} as const
// eslint-disable-next-line no-void
void trueSchema
})
it("validates simple null", () => {
const nullSchema: JSONSchemaType<null> = {
type: "null",
nullable: true,
const: null,
enum: [null],
} as const
const validate = ajv.compile(nullSchema)
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
validate(null).should.be.true
})
})
})
// eslint-disable-next-line no-void
void emptyType