Debian Codegraph @

spec/discriminator.spec.ts

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