Debian Codegraph @

spec/ajv.spec.ts

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 import type Ajv from ".."
2 import type {KeywordCxt, SchemaObject} from ".."
3 import _Ajv from "./ajv"
4 import {_} from "../dist/compile/codegen/code"
5 import * as assert from "assert"
6 import chai from "./chai"
7 const should = chai.should()
8
9 describe("Ajv", () => {
10 let ajv: Ajv
11
12 beforeEach(() => {
13 ajv = new _Ajv({keywords: ["foo"], allowUnionTypes: true})
14 })
15
16 it("should create instance", () => {
17 ajv.should.be.instanceof(_Ajv)
18 })
19
20 describe("compile method", () => {
21 it("should compile schema and return validating function", () => {
22 const validate = ajv.compile({type: "integer"})
23 validate.should.be.a("function")
24 validate(1).should.equal(true)
25 validate(1.1).should.equal(false)
26 validate("1").should.equal(false)
27 })
28
29 it("should cache compiled functions for the same schema", () => {
30 const schema = {
31 $id: "//e.com/int.json",
32 type: "integer",
33 minimum: 1,
34 }
35 const v1 = ajv.compile(schema)
36 const v2 = ajv.compile(schema)
37 v1.should.equal(v2)
38 })
39
40 it("should throw if different schema has the same id", () => {
41 ajv.compile({$id: "//e.com/int.json", type: "integer"})
42 should.throw(() => {
43 ajv.compile({$id: "//e.com/int.json", type: "integer", minimum: 1})
44 }, /already exists/)
45 })
46
47 it("should throw if invalid schema is compiled", () => {
48 should.throw(() => {
49 ajv.compile({type: null})
50 }, /must be equal to one of the allowed values/)
51 })
52
53 it("should throw if compiled schema has an invalid JavaScript code", () => {
54 const _ajv = new _Ajv({logger: false})
55 _ajv.addKeyword({keyword: "even", code: badEvenCode})
56 let schema = {even: true}
57 const validate: any = _ajv.compile(schema)
58 validate(2).should.equal(true)
59 validate(3).should.equal(false)
60
61 schema = {even: false}
62 should.throw(() => {
63 _ajv.compile(schema)
64 }, /Unexpected token/)
65
66 function badEvenCode(cxt: KeywordCxt) {
67 const op = cxt.schema ? _`===` : _`!===` // invalid on purpose
68 cxt.pass(_`${cxt.data} % 2 ${op} 0`)
69 }
70 })
71 })
72
73 describe("validate method", () => {
74 it("should compile schema and validate data against it", () => {
75 ajv.validate({type: "integer"}, 1).should.equal(true)
76 ajv.validate({type: "integer"}, "1").should.equal(false)
77 ajv.validate({type: "string"}, "a").should.equal(true)
78 ajv.validate({type: "string"}, 1).should.equal(false)
79 })
80
81 it("should validate against previously compiled schema by id (also see addSchema)", () => {
82 ajv.validate({$id: "//e.com/int.json", type: "integer"}, 1).should.equal(true)
83 ajv.validate("//e.com/int.json", 1).should.equal(true)
84 ajv.validate("//e.com/int.json", "1").should.equal(false)
85
86 ajv.compile({$id: "//e.com/str.json", type: "string"}).should.be.a("function")
87 ajv.validate("//e.com/str.json", "a").should.equal(true)
88 ajv.validate("//e.com/str.json", 1).should.equal(false)
89 })
90
91 it("should throw exception if no schema with ref", () => {
92 ajv.validate({$id: "integer", type: "integer"}, 1).should.equal(true)
93 ajv.validate("integer", 1).should.equal(true)
94 should.throw(() => {
95 ajv.validate("string", "foo")
96 }, /no schema with key or ref/)
97 })
98
99 it("should validate schema fragment by ref", () => {
100 ajv.addSchema({
101 $id: "http://e.com/types.json",
102 definitions: {
103 int: {type: "integer"},
104 str: {type: "string"},
105 },
106 })
107
108 ajv.validate("http://e.com/types.json#/definitions/int", 1).should.equal(true)
109 ajv.validate("http://e.com/types.json#/definitions/int", "1").should.equal(false)
110 })
111
112 it("should return schema fragment by id", () => {
113 ajv.addSchema({
114 $id: "http://e.com/types.json",
115 definitions: {
116 int: {$id: "#int", type: "integer"},
117 str: {$id: "#str", type: "string"},
118 },
119 })
120
121 ajv.validate("http://e.com/types.json#int", 1).should.equal(true)
122 ajv.validate("http://e.com/types.json#int", "1").should.equal(false)
123 })
124 })
125
126 describe("addSchema method", () => {
127 it("should add and compile schema with key", () => {
128 ajv.addSchema({type: "integer"}, "int")
129 const validate = ajv.getSchema("int")
130 assert(typeof validate == "function")
131
132 validate(1).should.equal(true)
133 validate(1.1).should.equal(false)
134 validate("1").should.equal(false)
135 ajv.validate("int", 1).should.equal(true)
136 ajv.validate("int", "1").should.equal(false)
137 })
138
139 it("should add and compile schema without key", () => {
140 ajv.addSchema({type: "integer"})
141 ajv.validate("", 1).should.equal(true)
142 ajv.validate("", "1").should.equal(false)
143 })
144
145 it("should add and compile schema with id", () => {
146 ajv.addSchema({$id: "//e.com/int.json", type: "integer"})
147 ajv.validate("//e.com/int.json", 1).should.equal(true)
148 ajv.validate("//e.com/int.json", "1").should.equal(false)
149 })
150
151 it("should normalize schema keys and ids", () => {
152 ajv.addSchema({$id: "//e.com/int.json#", type: "integer"}, "int#")
153 ajv.validate("int", 1).should.equal(true)
154 ajv.validate("int", "1").should.equal(false)
155 ajv.validate("//e.com/int.json", 1).should.equal(true)
156 ajv.validate("//e.com/int.json", "1").should.equal(false)
157 ajv.validate("int#/", 1).should.equal(true)
158 ajv.validate("int#/", "1").should.equal(false)
159 ajv.validate("//e.com/int.json#/", 1).should.equal(true)
160 ajv.validate("//e.com/int.json#/", "1").should.equal(false)
161 })
162
163 it("should add and compile array of schemas with ids", () => {
164 ajv.addSchema([
165 {$id: "//e.com/int.json", type: "integer"},
166 {$id: "//e.com/str.json", type: "string"},
167 ])
168
169 const validate0 = ajv.getSchema("//e.com/int.json")
170 const validate1 = ajv.getSchema("//e.com/str.json")
171 assert(typeof validate0 == "function")
172 assert(typeof validate1 == "function")
173
174 validate0(1).should.equal(true)
175 validate0("1").should.equal(false)
176 validate1("a").should.equal(true)
177 validate1(1).should.equal(false)
178
179 ajv.validate("//e.com/int.json", 1).should.equal(true)
180 ajv.validate("//e.com/int.json", "1").should.equal(false)
181 ajv.validate("//e.com/str.json", "a").should.equal(true)
182 ajv.validate("//e.com/str.json", 1).should.equal(false)
183 })
184
185 it("should throw on duplicate key", () => {
186 ajv.addSchema({type: "integer"}, "int")
187 should.throw(() => {
188 ajv.addSchema({type: "integer", minimum: 1}, "int")
189 }, /already exists/)
190 })
191
192 it("should throw on duplicate normalized key", () => {
193 ajv.addSchema({type: "number"}, "num")
194 should.throw(() => {
195 ajv.addSchema({type: "integer"}, "num#")
196 }, /already exists/)
197 should.throw(() => {
198 ajv.addSchema({type: "integer"}, "num#/")
199 }, /already exists/)
200 })
201
202 it("should allow only one schema without key and id", () => {
203 ajv.addSchema({type: "number"})
204 should.throw(() => {
205 ajv.addSchema({type: "integer"})
206 }, /already exists/)
207 should.throw(() => {
208 ajv.addSchema({type: "integer"}, "")
209 }, /already exists/)
210 should.throw(() => {
211 ajv.addSchema({type: "integer"}, "#")
212 }, /already exists/)
213 })
214
215 it("should throw if schema is not an object", () => {
216 should.throw(() => {
217 // @ts-expect-error
218 ajv.addSchema("foo")
219 }, /schema must be object or boolean/)
220 })
221
222 it("should throw if schema id is not a string", () => {
223 try {
224 // @ts-expect-error
225 ajv.addSchema({$id: 1, type: "integer"})
226 throw new Error("should have throw exception")
227 } catch (e) {
228 ;(e as Error).message.should.equal("schema $id must be string")
229 }
230 })
231
232 it("should return instance of itself", () => {
233 const res = ajv.addSchema({type: "integer"}, "int")
234 res.should.equal(ajv)
235 })
236 })
237
238 describe("getSchema method", () => {
239 it("should return compiled schema by key", () => {
240 ajv.addSchema({type: "integer"}, "int")
241 const validate = ajv.getSchema("int")
242 assert(typeof validate == "function")
243 validate(1).should.equal(true)
244 validate("1").should.equal(false)
245 })
246
247 it("should return compiled schema by id or ref", () => {
248 ajv.addSchema({$id: "//e.com/int.json", type: "integer"})
249 const validate = ajv.getSchema("//e.com/int.json")
250 assert(typeof validate == "function")
251 validate(1).should.equal(true)
252 validate("1").should.equal(false)
253 })
254
255 it("should return compiled schema without key or with empty key", () => {
256 ajv.addSchema({type: "integer"})
257 const validate = ajv.getSchema("")
258 assert(typeof validate == "function")
259 validate(1).should.equal(true)
260 validate("1").should.equal(false)
261 })
262
263 it("should return schema fragment by ref", () => {
264 ajv.addSchema({
265 $id: "http://e.com/types.json",
266 definitions: {
267 int: {type: "integer"},
268 str: {type: "string"},
269 },
270 })
271
272 const vInt = ajv.getSchema("http://e.com/types.json#/definitions/int")
273 assert(typeof vInt == "function")
274 vInt(1).should.equal(true)
275 vInt("1").should.equal(false)
276 })
277
278 it("should return schema fragment by ref with protocol-relative URIs", () => {
279 ajv.addSchema({
280 $id: "//e.com/types.json",
281 definitions: {
282 int: {type: "integer"},
283 str: {type: "string"},
284 },
285 })
286
287 const vInt = ajv.getSchema("//e.com/types.json#/definitions/int")
288 assert(typeof vInt == "function")
289 vInt(1).should.equal(true)
290 vInt("1").should.equal(false)
291 })
292
293 it("should return schema fragment by id", () => {
294 ajv.addSchema({
295 $id: "http://e.com/types.json",
296 definitions: {
297 int: {$id: "#int", type: "integer"},
298 str: {$id: "#str", type: "string"},
299 },
300 })
301
302 const vInt = ajv.getSchema("http://e.com/types.json#int")
303 assert(typeof vInt == "function")
304 vInt(1).should.equal(true)
305 vInt("1").should.equal(false)
306 })
307 })
308
309 describe("removeSchema method", () => {
310 it("should remove schema by key", () => {
311 const schema = {type: "integer"}
312 ajv.addSchema(schema, "int")
313 const v = ajv.getSchema("int")
314 assert(typeof v == "function")
315 v.should.be.a("function")
316 //@ts-expect-error
317 ajv._cache.get(schema).validate.should.equal(v)
318
319 ajv.removeSchema("int")
320 should.not.exist(ajv.getSchema("int"))
321 //@ts-expect-error
322 should.not.exist(ajv._cache.get(schema))
323 })
324
325 it("should remove schema by id", () => {
326 const schema = {$id: "//e.com/int.json", type: "integer"}
327 ajv.addSchema(schema)
328
329 const v = ajv.getSchema("//e.com/int.json")
330 assert(typeof v == "function")
331 v.should.be.a("function")
332 //@ts-expect-error
333 ajv._cache.get(schema).validate.should.equal(v)
334
335 ajv.removeSchema("//e.com/int.json")
336 should.not.exist(ajv.getSchema("//e.com/int.json"))
337 //@ts-expect-error
338 should.not.exist(ajv._cache.get(schema))
339 })
340
341 it("should remove schema by schema object", () => {
342 const schema = {type: "integer"}
343 ajv.addSchema(schema)
344 //@ts-expect-error
345 ajv._cache.get(schema).should.be.an("object")
346 ajv.removeSchema(schema)
347 //@ts-expect-error
348 should.not.exist(ajv._cache.get(schema))
349 })
350
351 it("should remove schema with id by schema object", () => {
352 const schema = {$id: "//e.com/int.json", type: "integer"}
353 ajv.addSchema(schema)
354 //@ts-expect-error
355 ajv._cache.get(schema).should.be.an("object")
356 ajv.removeSchema(schema)
357 should.not.exist(ajv.getSchema("//e.com/int.json"))
358 //@ts-expect-error
359 should.not.exist(ajv._cache.get(schema))
360 })
361
362 it("should not throw if there is no schema with passed id", () => {
363 should.not.exist(ajv.getSchema("//e.com/int.json"))
364 should.not.throw(() => {
365 ajv.removeSchema("//e.com/int.json")
366 })
367 })
368
369 it("should remove all schemas but meta-schemas if called without an arguments", () => {
370 const schema1 = {$id: "//e.com/int.json", type: "integer"}
371 ajv.addSchema(schema1)
372 //@ts-expect-error
373 ajv._cache.get(schema1).should.be.an("object")
374
375 const schema2 = {type: "integer"}
376 ajv.addSchema(schema2)
377 //@ts-expect-error
378 ajv._cache.get(schema2).should.be.an("object")
379
380 ajv.removeSchema()
381 //@ts-expect-error
382 should.not.exist(ajv._cache.get(schema1))
383 //@ts-expect-error
384 should.not.exist(ajv._cache.get(schema2))
385 })
386
387 it("should remove all schemas but meta-schemas with key/id matching pattern", () => {
388 const schema1 = {$id: "//e.com/int.json", type: "integer"}
389 ajv.addSchema(schema1)
390 //@ts-expect-error
391 ajv._cache.get(schema1).should.be.an("object")
392
393 const schema2 = {$id: "str.json", type: "string"}
394 ajv.addSchema(schema2, "//e.com/str.json")
395 //@ts-expect-error
396 ajv._cache.get(schema2).should.be.an("object")
397
398 const schema3 = {type: "integer"}
399 ajv.addSchema(schema3)
400 //@ts-expect-error
401 ajv._cache.get(schema3).should.be.an("object")
402
403 ajv.removeSchema(/e\.com/)
404 //@ts-expect-error
405 should.not.exist(ajv._cache.get(schema1))
406 //@ts-expect-error
407 should.not.exist(ajv._cache.get(schema2))
408 //@ts-expect-error
409 ajv._cache.get(schema3).should.be.an("object")
410 })
411
412 it("should return instance of itself", () => {
413 const res = ajv.addSchema({type: "integer"}, "int").removeSchema("int")
414 res.should.equal(ajv)
415 })
416 })
417
418 describe("addFormat method", () => {
419 it("should add format as regular expression", () => {
420 ajv.addFormat("identifier", /^[a-z_$][a-z0-9_$]*$/i)
421 testFormat()
422 })
423
424 it("should add format as string", () => {
425 ajv.addFormat("identifier", "^[A-Za-z_$][A-Za-z0-9_$]*$")
426 testFormat()
427 })
428
429 it("should add format as function", () => {
430 ajv.addFormat("identifier", (str) => /^[a-z_$][a-z0-9_$]*$/i.test(str))
431 testFormat()
432 })
433
434 it("should add format as object", () => {
435 ajv.addFormat("identifier", {
436 validate: (str: string) => /^[a-z_$][a-z0-9_$]*$/i.test(str),
437 })
438 testFormat()
439 })
440
441 it("should return instance of itself", () => {
442 const res = ajv.addFormat("identifier", /^[a-z_$][a-z0-9_$]*$/i)
443 res.should.equal(ajv)
444 })
445
446 function testFormat() {
447 const validate = ajv.compile({
448 type: ["number", "string"],
449 format: "identifier",
450 })
451 validate("Abc1").should.equal(true)
452 validate("123").should.equal(false)
453 validate(123).should.equal(true)
454 }
455
456 describe("formats for number", () => {
457 it("should validate only numbers", () => {
458 ajv.addFormat("positive", {
459 type: "number",
460 validate: function (x: number) {
461 return x > 0
462 },
463 })
464
465 const validate = ajv.compile({
466 type: ["string", "number"],
467 format: "positive",
468 })
469 validate(-2).should.equal(false)
470 validate(0).should.equal(false)
471 validate(2).should.equal(true)
472 validate("abc").should.equal(true)
473 })
474
475 it("should validate numbers with format via $data", () => {
476 ajv = new _Ajv({$data: true, allowUnionTypes: true})
477 ajv.addFormat("positive", {
478 type: "number",
479 validate: function (x: number) {
480 return x > 0
481 },
482 })
483
484 const validate = ajv.compile({
485 type: "object",
486 properties: {
487 data: {
488 type: ["number", "string"],
489 format: {$data: "1/frmt"},
490 },
491 frmt: {type: "string"},
492 },
493 })
494 validate({data: -2, frmt: "positive"}).should.equal(false)
495 validate({data: 0, frmt: "positive"}).should.equal(false)
496 validate({data: 2, frmt: "positive"}).should.equal(true)
497 validate({data: "abc", frmt: "positive"}).should.equal(true)
498 })
499 })
500 })
501
502 describe("validateSchema method", () => {
503 it("should validate schema against meta-schema", () => {
504 let valid = ajv.validateSchema({
505 $schema: "http://json-schema.org/draft-07/schema#",
506 type: "number",
507 })
508
509 valid.should.equal(true)
510 should.equal(ajv.errors, null)
511
512 valid = ajv.validateSchema({
513 $schema: "http://json-schema.org/draft-07/schema#",
514 type: "wrong_type",
515 })
516
517 valid.should.equal(false)
518 assert(Array.isArray(ajv.errors))
519 ajv.errors.length.should.equal(3)
520 ajv.errors[0].keyword.should.equal("enum")
521 ajv.errors[1].keyword.should.equal("type")
522 ajv.errors[2].keyword.should.equal("anyOf")
523 })
524
525 it("should throw exception if meta-schema is unknown", () => {
526 should.throw(() => {
527 ajv.validateSchema({
528 $schema: "http://example.com/unknown/schema#",
529 type: "number",
530 })
531 }, /no schema with key or ref/)
532 })
533
534 it("should throw exception if $schema is not a string", () => {
535 should.throw(() => {
536 ajv.validateSchema({
537 //@ts-expect-error
538 $schema: {},
539 type: "number",
540 })
541 }, /\$schema must be a string/)
542 })
543
544 describe("sub-schema validation outside of definitions during compilation", () => {
545 it("maximum", () => {
546 passValidationThrowCompile({
547 $ref: "#/foo",
548 foo: {type: "number", maximum: "bar"},
549 })
550 })
551
552 it("exclusiveMaximum", () => {
553 passValidationThrowCompile({
554 $ref: "#/foo",
555 foo: {type: "number", exclusiveMaximum: "bar"},
556 })
557 })
558
559 it("maxItems", () => {
560 passValidationThrowCompile({
561 $ref: "#/foo",
562 foo: {type: "array", maxItems: "bar"},
563 })
564 })
565
566 it("maxLength", () => {
567 passValidationThrowCompile({
568 $ref: "#/foo",
569 foo: {type: "string", maxLength: "bar"},
570 })
571 })
572
573 it("maxProperties", () => {
574 passValidationThrowCompile({
575 $ref: "#/foo",
576 foo: {type: "object", maxProperties: "bar"},
577 })
578 })
579
580 it("multipleOf", () => {
581 passValidationThrowCompile({
582 $ref: "#/foo",
583 foo: {type: "number", multipleOf: "bar"},
584 })
585 })
586
587 function passValidationThrowCompile(schema: SchemaObject) {
588 ajv.validateSchema(schema).should.equal(true)
589 should.throw(() => {
590 ajv.compile(schema)
591 }, /value must be/)
592 }
593 })
594 })
595 })