Debian Codegraph @

spec/options/options_refs.spec.ts

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 import _Ajv from "../ajv"
2 import type {Options} from "../.."
3 import chai from "../chai"
4 const should = chai.should()
5
6 describe("referenced schema options", () => {
7 describe("ignoreKeywordsWithRef", () => {
8 describe("= undefined", () => {
9 it("should allow extending $ref with other keywords", () => {
10 test({}, true)
11 })
12
13 it("should NOT log warning", () => {
14 testWarning()
15 })
16 })
17
18 describe("= true", () => {
19 it("should ignore other keywords when $ref is used", () => {
20 test({ignoreKeywordsWithRef: true, logger: false}, false)
21 })
22
23 it("should log warning when other keywords are used with $ref", () => {
24 testWarning({ignoreKeywordsWithRef: true}, /keywords\signored/)
25 })
26 })
27
28 function test(opts: Options, shouldExtendRef: boolean) {
29 const ajv = new _Ajv(opts)
30 const schema = {
31 definitions: {
32 int: {type: "integer"},
33 },
34 type: "number",
35 $ref: "#/definitions/int",
36 minimum: 10,
37 }
38
39 let validate = ajv.compile(schema)
•40 // @ts-ignore
41 validate(10).should.equal(true)
•42 // @ts-ignore
43 validate(1).should.equal(!shouldExtendRef)
44
45 const schema1 = {
46 definitions: {
47 int: {type: "integer"},
48 },
49 type: "object",
50 properties: {
51 foo: {
52 $ref: "#/definitions/int",
53 type: "number",
54 minimum: 10,
55 },
56 bar: {
57 type: "number",
58 allOf: [{$ref: "#/definitions/int"}, {minimum: 10}],
59 },
60 },
61 }
62
63 validate = ajv.compile(schema1)
•64 // @ts-ignore
65 validate({foo: 10, bar: 10}).should.equal(true)
•66 // @ts-ignore
67 validate({foo: 1, bar: 10}).should.equal(!shouldExtendRef)
•68 // @ts-ignore
69 validate({foo: 10, bar: 1}).should.equal(false)
70 }
71
72 function testWarning(opts: Options = {}, msgPattern?: RegExp) {
73 let oldConsole
74 try {
75 oldConsole = console.warn
76 let consoleMsg
77 console.warn = function (...args: any[]) {
78 consoleMsg = Array.prototype.join.call(args, " ")
79 }
80
81 const ajv = new _Ajv(opts)
82
83 const schema = {
84 definitions: {
85 int: {type: "integer"},
86 },
87 type: "number",
88 $ref: "#/definitions/int",
89 minimum: 10,
90 }
91
92 ajv.compile(schema)
•93 // @ts-ignore
94 if (msgPattern) consoleMsg.should.match(msgPattern)
95 else should.not.exist(consoleMsg)
96 } finally {
97 console.warn = oldConsole
98 }
99 }
100 })
101
102 describe("missingRefs", () => {
103 it("should throw if ref is missing without this option", () => {
104 const ajv = new _Ajv()
105 should.throw(() => {
106 ajv.compile({$ref: "missing_reference"})
107 }, /can't resolve reference missing_reference/)
108 })
109 })
110 })