Debian Codegraph @

spec/options/options_refs.spec.ts

raw symbols ◀ ▶
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 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 validate(10).should.equal(true)
41 validate(1).should.equal(!shouldExtendRef)
42
43 const schema1 = {
44 definitions: {
45 int: {type: "integer"},
46 },
47 type: "object",
48 properties: {
49 foo: {
50 $ref: "#/definitions/int",
51 type: "number",
52 minimum: 10,
53 },
54 bar: {
55 type: "number",
56 allOf: [{$ref: "#/definitions/int"}, {minimum: 10}],
57 },
58 },
59 }
60
61 validate = ajv.compile(schema1)
62 validate({foo: 10, bar: 10}).should.equal(true)
63 validate({foo: 1, bar: 10}).should.equal(!shouldExtendRef)
64 validate({foo: 10, bar: 1}).should.equal(false)
65 }
66
67 function testWarning(opts: Options = {}, msgPattern?: RegExp) {
68 let oldConsole
69 try {
70 oldConsole = console.warn
71 let consoleMsg
72 console.warn = function (...args: any[]) {
73 consoleMsg = Array.prototype.join.call(args, " ")
74 }
75
76 const ajv = new _Ajv(opts)
77
78 const schema = {
79 definitions: {
80 int: {type: "integer"},
81 },
82 type: "number",
83 $ref: "#/definitions/int",
84 minimum: 10,
85 }
86
87 ajv.compile(schema)
88 if (msgPattern) consoleMsg.should.match(msgPattern)
89 else should.not.exist(consoleMsg)
90 } finally {
91 console.warn = oldConsole
92 }
93 }
94 })
95
96 describe("missingRefs", () => {
97 it("should throw if ref is missing without this option", () => {
98 const ajv = new _Ajv()
99 should.throw(() => {
100 ajv.compile({$ref: "missing_reference"})
101 }, /can't resolve reference missing_reference/)
102 })
103 })
104 })