Debian Codegraph @

spec/discriminator.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 AjvPack from "../dist/standalone/instance"
3 import type AjvCore from "../dist/core"
4 import type {SchemaObject} from ".."
5 import _Ajv from "./ajv"
6 import _Ajv2019 from "./ajv2019"
7 import getAjvInstances from "./ajv_instances"
8 import {withStandalone} from "./ajv_standalone"
9 import options from "./ajv_options"
10 import * as assert from "assert"
11
12 describe("discriminator keyword", function () {
13 let ajvs: (Ajv | AjvPack)[]
14
15 this.timeout(10000)
16
17 before(() => {
18 ajvs = [...getAjvs(_Ajv), ...getAjvs(_Ajv2019)]
19 })
20
21 function getAjvs(AjvClass: typeof AjvCore) {
22 return withStandalone(getAjvInstances(AjvClass, options, {discriminator: true}))
23 }
24
25 describe("validation", () => {
26 const schema1 = {
27 type: "object",
28 discriminator: {propertyName: "foo"},
29 oneOf: [
30 {
31 properties: {
32 foo: {const: "x"},
33 a: {type: "string"},
34 },
35 required: ["foo", "a"],
36 },
37 {
38 properties: {
39 foo: {enum: ["y", "z"]},
40 b: {type: "string"},
41 },
42 required: ["foo", "b"],
43 },
44 ],
45 }
46
47 const schema2 = {
48 type: "object",
49 discriminator: {propertyName: "foo"},
50 required: ["foo"],
51 oneOf: [
52 {
53 properties: {
54 foo: {const: "x"},
55 a: {type: "string"},
56 },
57 required: ["a"],
58 },
59 {
60 properties: {
61 foo: {enum: ["y", "z"]},
62 b: {type: "string"},
63 },
64 required: ["b"],
65 },
66 ],
67 }
68
69 const schemas = [schema1, schema2]
70
71 it("should validate data", () => {
72 assertValid(schemas, {foo: "x", a: "a"})
73 assertValid(schemas, {foo: "y", b: "b"})
74 assertValid(schemas, {foo: "z", b: "b"})
75 assertInvalid(schemas, {})
76 assertInvalid(schemas, {foo: 1})
77 assertInvalid(schemas, {foo: "bar"})
78 assertInvalid(schemas, {foo: "x", b: "b"})
79 assertInvalid(schemas, {foo: "y", a: "a"})
80 assertInvalid(schemas, {foo: "z", a: "a"})
81 })
82 })
83
84 describe("validation with referenced schemas", () => {
85 const definitions1 = {
86 schema1: {
87 properties: {
88 foo: {const: "x"},
89 a: {type: "string"},
90 },
91 required: ["foo", "a"],
92 },
93 schema2: {
94 properties: {
95 foo: {enum: ["y", "z"]},
96 b: {type: "string"},
97 },
98 required: ["foo", "b"],
99 },
100 }
101 const mainSchema1 = {
102 type: "object",
103 discriminator: {propertyName: "foo"},
104 oneOf: [
105 {
106 $ref: "#/definitions/schema1",
107 },
108 {
109 $ref: "#/definitions/schema2",
110 },
111 ],
112 }
113
114 const definitions2 = {
115 schema1: {
116 properties: {
117 foo: {const: "x"},
118 a: {type: "string"},
119 },
120 required: ["a"],
121 },
122 schema2: {
123 properties: {
124 foo: {enum: ["y", "z"]},
125 b: {type: "string"},
126 },
127 required: ["b"],
128 },
129 }
130 const mainSchema2 = {
131 type: "object",
132 discriminator: {propertyName: "foo"},
133 required: ["foo"],
134 oneOf: [
135 {
136 $ref: "#/definitions/schema1",
137 },
138 {
139 $ref: "#/definitions/schema2",
140 },
141 ],
142 }
143
144 const schema = [
145 {definitions: definitions1, ...mainSchema1},
146 {definitions: definitions2, ...mainSchema2},
147 ]
148
149 it("should validate data", () => {
150 assertValid(schema, {foo: "x", a: "a"})
151 assertValid(schema, {foo: "y", b: "b"})
152 assertValid(schema, {foo: "z", b: "b"})
153 assertInvalid(schema, {})
154 assertInvalid(schema, {foo: 1})
155 assertInvalid(schema, {foo: "bar"})
156 assertInvalid(schema, {foo: "x", b: "b"})
157 assertInvalid(schema, {foo: "y", a: "a"})
158 assertInvalid(schema, {foo: "z", a: "a"})
159 })
160 })
161
162 describe("schema with external $refs", () => {
163 const schemas = {
164 main: {
165 type: "object",
166 discriminator: {propertyName: "foo"},
167 required: ["foo"],
168 oneOf: [
169 {
170 $ref: "schema1",
171 },
172 {
173 $ref: "schema2",
174 },
175 ],
176 },
177 schema1: {
178 type: "object",
179 properties: {
180 foo: {const: "x"},
181 },
182 },
183 schema2: {
184 type: "object",
185 properties: {
186 foo: {enum: ["y", "z"]},
187 },
188 },
189 }
190
191 const data = {foo: "x"}
192 const badData = {foo: "w"}
193
194 it("compile should resolve each $ref to a schema that was added with addSchema", () => {
195 const opts = {
196 discriminator: true,
197 }
198 const ajv = new _Ajv(opts)
199 ajv.addSchema(schemas.main, "https://host/main")
200 ajv.addSchema(schemas.schema1, "https://host/schema1")
201 ajv.addSchema(schemas.schema2, "https://host/schema2")
202
203 const validate = ajv.compile({$ref: "https://host/main"})
204 assert.strictEqual(validate(data), true)
205 assert.strictEqual(validate(badData), false)
206 })
207 it("compileAsync should loadSchema each $ref", async () => {
208 const opts = {
209 discriminator: true,
210 loadSchema(url) {
211 if (!url.startsWith("https://host/")) return undefined
212 const name = url.substring("https://host/".length)
213 return schemas[name]
214 },
215 }
216 const ajv = new _Ajv(opts)
217 const validate = await ajv.compileAsync({$ref: "https://host/main"})
218 assert.strictEqual(validate(data), true)
219 assert.strictEqual(validate(badData), false)
220 })
221 })
222
223 describe("validation with deeply referenced schemas", () => {
224 const schema = [
225 {
226 type: "object",
227 properties: {
228 container: {
229 $ref: "#/definitions/Container",
230 },
231 },
232 definitions: {
233 BlockA: {
234 type: "object",
235 properties: {
236 _type: {
237 type: "string",
238 enum: ["a"],
239 },
240 a: {type: "string"},
241 },
242 additionalProperties: false,
243 required: ["_type"],
244 title: "BlockA",
245 },
246 BlockB: {
247 type: "object",
248 properties: {
249 _type: {
250 type: "string",
251 enum: ["b"],
252 },
253 b: {type: "string"},
254 },
255 additionalProperties: false,
256 required: ["_type"],
257 title: "BlockB",
258 },
259 Container: {
260 type: "object",
261 properties: {
262 list: {
263 type: "array",
264 items: {
265 oneOf: [
266 {
267 $ref: "#/definitions/BlockA",
268 },
269 {
270 $ref: "#/definitions/BlockB",
271 },
272 ],
273 discriminator: {
274 propertyName: "_type",
275 },
276 },
277 },
278 },
279 },
280 },
281 },
282 ]
283
284 it("should validate data", () => {
285 assertValid(schema, {
286 container: {
287 list: [
288 {_type: "a", a: "foo"},
289 {_type: "b", b: "bar"},
290 ],
291 },
292 })
293
294 assertInvalid(schema, {
295 container: {
296 list: [
297 {_type: "a", b: "foo"},
298 {_type: "b", b: "bar"},
299 ],
300 },
301 })
302 })
303 })
304
305 describe("valid schemas", () => {
306 it("should have oneOf", () => {
307 invalidSchema(
308 {type: "object", discriminator: {propertyName: "foo"}},
309 /discriminator: requires oneOf/
310 )
311 })
312
313 it("should have schema for tag", () => {
314 invalidSchema(
315 {
316 type: "object",
317 discriminator: {propertyName: "foo"},
318 required: ["foo"],
319 oneOf: [{properties: {}}],
320 },
321 /discriminator: oneOf subschemas \(or referenced schemas\) must have "properties\/foo"/
322 )
323 })
324
325 it("should have enum or const in schema for tag", () => {
326 invalidSchema(
327 {
328 type: "object",
329 discriminator: {propertyName: "foo"},
330 required: ["foo"],
331 oneOf: [{properties: {foo: {}}}],
332 },
333 /discriminator: "properties\/foo" must have "const" or "enum"/
334 )
335 })
336
337 it("tag value should be string", () => {
338 invalidSchema(
339 {
340 type: "object",
341 discriminator: {propertyName: "foo"},
342 required: ["foo"],
343 oneOf: [{properties: {foo: {const: 1}}}],
344 },
345 /discriminator: "foo" values must be unique strings/
346 )
347 })
348
349 it("tag values should be unique", () => {
350 invalidSchema(
351 {
352 type: "object",
353 discriminator: {propertyName: "foo"},
354 required: ["foo"],
355 oneOf: [{properties: {foo: {const: "a"}}}, {properties: {foo: {const: "a"}}}],
356 },
357 /discriminator: "foo" values must be unique strings/
358 )
359 })
360
361 it("tag should be required", () => {
362 invalidSchema(
363 {
364 type: "object",
365 discriminator: {propertyName: "foo"},
366 oneOf: [
367 {properties: {foo: {const: "a"}}, required: ["foo"]},
368 {properties: {foo: {const: "b"}}},
369 ],
370 },
371 /discriminator: "foo" must be required/
372 )
373 })
374 })
375
376 function assertValid(schemas: SchemaObject[], data: unknown): void {
377 schemas.forEach((schema) =>
378 ajvs.forEach((ajv) => assert.strictEqual(ajv.validate(schema, data), true))
379 )
380 }
381
382 function assertInvalid(schemas: SchemaObject[], data: unknown): void {
383 schemas.forEach((schema) =>
384 ajvs.forEach((ajv) => assert.strictEqual(ajv.validate(schema, data), false))
385 )
386 }
387
388 function invalidSchema(schema: SchemaObject, error: any): void {
389 ajvs.forEach((ajv) => assert.throws(() => ajv.compile(schema), error))
390 }
391 })