Debian Codegraph @

spec/options/meta_validateSchema.spec.ts

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import _Ajv from "../ajv"
2 import chai from "../chai"
3 const should = chai.should()
4
5 describe("meta and validateSchema options", () => {
6 it("should add draft-7 meta schema by default", () => {
7 testOptionMeta(new _Ajv())
8 testOptionMeta(new _Ajv({meta: true}))
9
10 function testOptionMeta(ajv) {
11 ajv.getSchema("http://json-schema.org/draft-07/schema").should.be.a("function")
12 ajv.validateSchema({$id: "ok", type: "integer"}).should.equal(true)
13 ajv.validateSchema({$id: "wrong", type: 123}).should.equal(false)
14 should.not.throw(() => {
15 ajv.addSchema({$id: "ok", type: "integer"})
16 })
17 should.throw(() => {
18 ajv.addSchema({$id: "wrong", type: 123})
19 }, /schema is invalid/)
20 }
21 })
22
23 it("should throw if meta: false and validateSchema: true", () => {
24 const ajv = new _Ajv({meta: false, logger: false})
25 should.not.exist(ajv.getSchema("http://json-schema.org/draft-07/schema"))
26 should.not.throw(() => {
27 ajv.addSchema({type: "wrong_type"}, "integer")
28 })
29 })
30
31 it("should skip schema validation with validateSchema: false", () => {
32 let ajv = new _Ajv()
33 should.throw(() => {
34 ajv.addSchema({type: 123}, "integer")
35 }, /schema is invalid/)
36
37 ajv = new _Ajv({validateSchema: false})
38 should.not.throw(() => {
39 ajv.addSchema({type: 123}, "integer")
40 })
41
42 ajv = new _Ajv({validateSchema: false, meta: false})
43 should.not.throw(() => {
44 ajv.addSchema({type: 123}, "integer")
45 })
46 })
47
48 describe('validateSchema: "log"', () => {
49 let loggedError, loggedWarning
50 const logger = {
51 log() {},
52 warn: () => (loggedWarning = true),
53 error: () => (loggedError = true),
54 }
55
56 beforeEach(() => {
57 loggedError = false
58 loggedWarning = false
59 })
60
61 it("should not throw on invalid schema", () => {
62 const ajv = new _Ajv({validateSchema: "log", logger})
63 should.not.throw(() => {
64 ajv.addSchema({type: 123}, "integer")
65 })
66 loggedError.should.equal(true)
67 loggedWarning.should.equal(false)
68 })
69
70 it("should not throw on invalid schema with meta: false", () => {
71 const ajv = new _Ajv({validateSchema: "log", meta: false, logger})
72 should.not.throw(() => {
73 ajv.addSchema({type: 123}, "integer")
74 })
75 loggedError.should.equal(false)
76 loggedWarning.should.equal(true)
77 })
78 })
79
80 it("should validate v6 schema", () => {
81 const ajv = new _Ajv()
82 ajv.validateSchema({contains: {minimum: 2}}).should.equal(true)
83 ajv.validateSchema({contains: 2}).should.equal(false)
84 })
85
86 it("should use option meta as default meta schema", () => {
87 const meta = {
88 $schema: "http://json-schema.org/draft-07/schema",
89 properties: {
90 myKeyword: {type: "boolean"},
91 },
92 }
93 let ajv = new _Ajv({meta: meta})
94 ajv.validateSchema({myKeyword: true}).should.equal(true)
95 ajv.validateSchema({myKeyword: 2}).should.equal(false)
96 ajv
97 .validateSchema({
98 $schema: "http://json-schema.org/draft-07/schema",
99 myKeyword: 2,
100 })
101 .should.equal(true)
102
103 ajv = new _Ajv()
104 ajv.validateSchema({myKeyword: true}).should.equal(true)
105 ajv.validateSchema({myKeyword: 2}).should.equal(true)
106 })
107 })