Debian Codegraph @

spec/options/comment.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 chai.should()
4
5 describe("$comment option", () => {
6 describe("= true", () => {
7 let logCalls: any[][]
8
9 function log(...args: any[]) {
10 logCalls.push(args)
11 }
12
13 const logger = {log, warn: log, error: log}
14
15 it("should log the text from $comment keyword", () => {
16 const schema = {
17 $comment: "object root",
18 type: "object",
19 properties: {
20 foo: {$comment: "property foo"},
21 bar: {$comment: "property bar", type: "integer"},
22 },
23 }
24
25 const ajv = new _Ajv({$comment: true, logger})
26 const fullAjv = new _Ajv({allErrors: true, $comment: true, logger})
27
28 ;[ajv, fullAjv].forEach((_ajv) => {
29 const validate = _ajv.compile(schema)
30
31 test({}, true, [["object root"]])
32 test({foo: 1}, true, [["object root"], ["property foo"]])
33 test({foo: 1, bar: 2}, true, [["object root"], ["property foo"], ["property bar"]])
34 test({foo: 1, bar: "baz"}, false, [["object root"], ["property foo"], ["property bar"]])
35
36 function test(data, valid, expectedLogCalls) {
37 logCalls = []
38 validate(data).should.equal(valid)
39 logCalls.should.eql(expectedLogCalls)
40 }
41 })
42 })
43 })
44
45 describe("function hook", () => {
46 let hookCalls
47
48 function hook(...args: any[]) {
49 hookCalls.push(Array.prototype.slice.call(args))
50 }
51
52 it("should pass the text from $comment keyword to the hook", () => {
53 const schema = {
54 $comment: "object root",
55 type: "object",
56 properties: {
57 foo: {$comment: "property foo"},
58 bar: {$comment: "property bar", type: "integer"},
59 },
60 }
61
62 const ajv = new _Ajv({$comment: hook})
63 const fullAjv = new _Ajv({allErrors: true, $comment: hook})
64
65 ;[ajv, fullAjv].forEach((_ajv) => {
66 const validate = _ajv.compile(schema)
67
68 test({}, true, [["object root", "#/$comment", schema]])
69 test({foo: 1}, true, [
70 ["object root", "#/$comment", schema],
71 ["property foo", "#/properties/foo/$comment", schema],
72 ])
73 test({foo: 1, bar: 2}, true, [
74 ["object root", "#/$comment", schema],
75 ["property foo", "#/properties/foo/$comment", schema],
76 ["property bar", "#/properties/bar/$comment", schema],
77 ])
78 test({foo: 1, bar: "baz"}, false, [
79 ["object root", "#/$comment", schema],
80 ["property foo", "#/properties/foo/$comment", schema],
81 ["property bar", "#/properties/bar/$comment", schema],
82 ])
83
84 function test(data, valid, expectedHookCalls) {
85 hookCalls = []
86 validate(data).should.equal(valid)
87 hookCalls.should.eql(expectedHookCalls)
88 }
89 })
90 })
91 })
92 })