forked from ajv-validator/ajv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.spec.ts
385 lines (355 loc) · 10.8 KB
/
async.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import _Ajv from "./ajv"
import type {SchemaObject, AnyValidateFunction} from "../dist/types"
import chai from "./chai"
const should = chai.should()
describe("compileAsync method", () => {
let ajv, loadCallCount
const SCHEMAS = {
"http://example.com/object.json": {
$id: "http://example.com/object.json",
type: "object",
properties: {
a: {type: "string"},
b: {$ref: "int2plus.json"},
},
},
"http://example.com/int2plus.json": {
$id: "http://example.com/int2plus.json",
type: "integer",
minimum: 2,
},
"http://example.com/tree.json": {
$id: "http://example.com/tree.json",
type: "array",
items: {$ref: "leaf.json"},
},
"http://example.com/leaf.json": {
$id: "http://example.com/leaf.json",
type: "object",
properties: {
name: {type: "string"},
subtree: {$ref: "tree.json"},
},
},
"http://example.com/recursive.json": {
$id: "http://example.com/recursive.json",
type: "object",
properties: {
b: {$ref: "parent.json"},
},
required: ["b"],
},
"http://example.com/invalid.json": {
$id: "http://example.com/recursive.json",
type: "object",
properties: {
invalid: {type: "number"},
},
required: "invalid",
},
"http://example.com/foobar.json": {
$id: "http://example.com/foobar.json",
$schema: "http://example.com/foobar_meta.json",
type: "string",
myFooBar: "foo",
},
"http://example.com/foobar_meta.json": {
$id: "http://example.com/foobar_meta.json",
type: "object",
properties: {
myFooBar: {
enum: ["foo", "bar"],
},
},
},
"http://example.com/foo.json": {
$id: "http://example.com/foo.json",
type: "object",
properties: {
bar: {$ref: "bar.json"},
other: {$ref: "other.json"},
},
},
"http://example.com/bar.json": {
$id: "http://example.com/bar.json",
type: "object",
properties: {
foo: {$ref: "foo.json"},
},
},
"http://example.com/other.json": {
$id: "http://example.com/other.json",
},
}
beforeEach(() => {
loadCallCount = 0
ajv = new _Ajv({loadSchema})
})
it("should compile schemas loading missing schemas with options.loadSchema function", () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {$ref: "object.json"},
},
}
return ajv.compileAsync(schema).then((validate) => {
should.equal(loadCallCount, 2)
validate.should.be.a("function")
validate({a: {b: 2}}).should.equal(true)
validate({a: {b: 1}}).should.equal(false)
})
})
it("should compile schemas loading missing schemas and return promise with function", () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {$ref: "object.json"},
},
}
return ajv.compileAsync(schema).then((validate) => {
should.equal(loadCallCount, 2)
validate.should.be.a("function")
validate({a: {b: 2}}).should.equal(true)
validate({a: {b: 1}}).should.equal(false)
})
})
it("should correctly load schemas when missing reference has JSON path", () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {$ref: "object.json#/properties/b"},
},
}
return ajv.compileAsync(schema).then((validate) => {
should.equal(loadCallCount, 2)
validate.should.be.a("function")
validate({a: 2}).should.equal(true)
validate({a: 1}).should.equal(false)
})
})
it("should correctly compile with remote schemas that have mutual references", () => {
const schema = {
$id: "http://example.com/root.json",
type: "object",
properties: {
tree: {$ref: "tree.json"},
},
}
return ajv.compileAsync(schema).then((validate) => {
validate.should.be.a("function")
const validData = {
tree: [{name: "a", subtree: [{name: "a.a"}]}, {name: "b"}],
}
const invalidData = {tree: [{name: "a", subtree: [{name: 1}]}]}
validate(validData).should.equal(true)
validate(invalidData).should.equal(false)
})
})
it("should correctly compile with remote schemas that reference the compiled schema", () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {$ref: "recursive.json"},
},
}
return ajv.compileAsync(schema).then((validate) => {
should.equal(loadCallCount, 1)
validate.should.be.a("function")
const validData = {a: {b: {a: {b: {}}}}}
const invalidData = {a: {b: {a: {}}}}
validate(validData).should.equal(true)
validate(invalidData).should.equal(false)
})
})
it('should resolve reference containing "properties" segment with the same property (issue #220)', () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {
$ref: "object.json#/properties/a",
},
},
}
return ajv.compileAsync(schema).then((validate) => {
should.equal(loadCallCount, 2)
validate.should.be.a("function")
validate({a: "foo"}).should.equal(true)
validate({a: 42}).should.equal(false)
})
})
describe("loading metaschemas (#334)", () => {
it("should load metaschema if not available", () => {
return test(SCHEMAS["http://example.com/foobar.json"], 1)
})
it("should load metaschema of referenced schema if not available", () => {
return test({$ref: "http://example.com/foobar.json"}, 2)
})
function test(schema, expectedLoadCallCount) {
ajv.addKeyword({
keyword: "myFooBar",
type: "string",
validate: function (sch, data) {
return sch === data
},
})
return ajv.compileAsync(schema).then((validate) => {
should.equal(loadCallCount, expectedLoadCallCount)
validate.should.be.a("function")
validate("foo").should.equal(true)
validate("bar").should.equal(false)
})
}
})
it("should return compiled schema on the next tick if there are no references (#51)", () => {
const schema = {
$id: "http://example.com/int2plus.json",
type: "integer",
minimum: 2,
}
let beforeCallback1: any = false
const p1 = ajv.compileAsync(schema).then((validate) => {
beforeCallback1.should.equal(true)
spec(validate)
let beforeCallback2: any = false
const p2 = ajv.compileAsync(schema).then((_validate) => {
beforeCallback2.should.equal(true)
spec(_validate)
})
beforeCallback2 = true
return p2
})
beforeCallback1 = true
return p1
function spec(validate) {
should.equal(loadCallCount, 0)
validate.should.be.a("function")
const validData = 2
const invalidData = 1
validate(validData).should.equal(true)
validate(invalidData).should.equal(false)
}
})
it("should queue calls so only one compileAsync executes at a time (#52)", () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {$ref: "object.json"},
},
}
return Promise.all([
ajv.compileAsync(schema).then(spec),
ajv.compileAsync(schema).then(spec),
ajv.compileAsync(schema).then(spec),
])
function spec(validate) {
should.equal(loadCallCount, 2)
validate.should.be.a("function")
validate({a: {b: 2}}).should.equal(true)
validate({a: {b: 1}}).should.equal(false)
}
})
it("should throw exception if loadSchema is not passed", () => {
const schema = {
$id: "http://example.com/int2plus.json",
type: "integer",
minimum: 2,
}
ajv = new _Ajv()
should.throw(() => {
ajv.compileAsync(schema)
}, "options.loadSchema should be a function")
})
describe("should return error via promise", () => {
it("if passed schema is invalid", () => {
const invalidSchema = {
$id: "http://example.com/int2plus.json",
type: "integer",
minimum: "invalid",
}
return shouldReject(ajv.compileAsync(invalidSchema), /schema is invalid/)
})
it("if loaded schema is invalid", () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {$ref: "invalid.json"},
},
}
return shouldReject(ajv.compileAsync(schema), /schema is invalid/)
})
it("if required schema is loaded but the reference cannot be resolved", () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {$ref: "object.json#/definitions/not_found"},
},
}
return shouldReject(ajv.compileAsync(schema), /is loaded but/)
})
it("if loadSchema returned error", () => {
const schema = {
$id: "http://example.com/parent.json",
type: "object",
properties: {
a: {$ref: "object.json"},
},
}
ajv = new _Ajv({loadSchema: badLoadSchema})
return shouldReject(ajv.compileAsync(schema), /cant load/)
function badLoadSchema() {
return Promise.reject(new Error("cant load"))
}
})
it("if schema compilation throws some other exception", () => {
ajv.addKeyword({keyword: "badkeyword", compile: badCompile})
const schema = {badkeyword: true}
return shouldReject(ajv.compileAsync(schema), /cant compile keyword schema/)
function badCompile(/* schema */) {
throw new Error("cant compile keyword schema")
}
})
function shouldReject(p: Promise<AnyValidateFunction>, rx: RegExp) {
return p.then(
(validate) => {
should.not.exist(validate)
throw new Error("Promise has resolved; it should have rejected")
},
(err) => {
should.exist(err)
err.message.should.match(rx)
}
)
}
})
describe("schema with multiple remote properties, the first is recursive schema (#801)", () => {
it("should validate data", () => {
const schema = {
$id: "http://example.com/list.json",
type: "object",
properties: {
foo: {$ref: "foo.json"},
},
}
return ajv.compileAsync(schema).then((validate) => {
validate({foo: {}}).should.equal(true)
})
})
})
function loadSchema(uri: string): Promise<SchemaObject> {
loadCallCount++
return new Promise((resolve, reject) => {
setTimeout(() => {
if (SCHEMAS[uri]) resolve(SCHEMAS[uri])
else reject(new Error("404"))
}, 10)
})
}
})