forked from ajv-validator/ajv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoercion.spec.ts
487 lines (431 loc) · 13.4 KB
/
coercion.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import type Ajv from ".."
import _Ajv from "./ajv"
import chai from "./chai"
chai.should()
const coercionRules = {
string: {
number: [
{from: 1, to: "1"},
{from: 1.5, to: "1.5"},
{from: 2e100, to: "2e+100"},
],
boolean: [
{from: false, to: "false"},
{from: true, to: "true"},
],
null: [{from: null, to: ""}],
object: [{from: {}, to: undefined}],
array: [
{from: [], to: undefined},
{from: [1], to: undefined},
],
},
number: {
string: [
{from: "1", to: 1},
{from: "1.5", to: 1.5},
{from: "2e10", to: 2e10},
{from: "1a", to: undefined},
{from: "abc", to: undefined},
{from: "", to: undefined},
],
boolean: [
{from: false, to: 0},
{from: true, to: 1},
],
null: [{from: null, to: 0}],
object: [{from: {}, to: undefined}],
array: [
{from: [], to: undefined},
{from: [true], to: undefined},
],
},
integer: {
string: [
{from: "1", to: 1},
{from: "1.5", to: undefined},
{from: "2e10", to: 2e10},
{from: "1a", to: undefined},
{from: "abc", to: undefined},
{from: "", to: undefined},
],
boolean: [
{from: false, to: 0},
{from: true, to: 1},
],
null: [{from: null, to: 0}],
object: [{from: {}, to: undefined}],
array: [
{from: [], to: undefined},
{from: ["1"], to: undefined},
],
},
boolean: {
string: [
{from: "false", to: false},
{from: "true", to: true},
{from: "", to: undefined},
{from: "abc", to: undefined},
],
number: [
{from: 0, to: false},
{from: 1, to: true},
{from: 2, to: undefined},
{from: 2.5, to: undefined},
],
null: [{from: null, to: false}],
object: [{from: {}, to: undefined}],
array: [
{from: [], to: undefined},
{from: [0], to: undefined},
],
},
null: {
string: [
{from: "", to: null},
{from: "abc", to: undefined},
{from: "null", to: undefined},
],
number: [
{from: 0, to: null},
{from: 1, to: undefined},
],
boolean: [
{from: false, to: null},
{from: true, to: undefined},
],
object: [{from: {}, to: undefined}],
array: [
{from: [], to: undefined},
{from: [null], to: undefined},
],
},
array: {
all: [
{type: "string", from: "abc", to: undefined},
{type: "number", from: 1, to: undefined},
{type: "boolean", from: true, to: undefined},
{type: "null", from: null, to: undefined},
{type: "object", from: {}, to: undefined},
],
},
object: {
all: [
{type: "string", from: "abc", to: undefined},
{type: "number", from: 1, to: undefined},
{type: "boolean", from: true, to: undefined},
{type: "null", from: null, to: undefined},
{type: "array", from: [], to: undefined},
],
},
}
const coercionArrayRules = JSON.parse(JSON.stringify(coercionRules))
coercionArrayRules.string.array = [
{from: ["abc"], to: "abc"},
{from: [123], to: "123"},
{from: [true], to: "true"},
{from: [null], to: ""},
{from: [{}], to: undefined},
{from: ["abc", "def"], to: undefined},
{from: [], to: undefined},
]
coercionArrayRules.number.array = [
{from: [1.5], to: 1.5},
{from: ["1.5"], to: 1.5},
{from: [true], to: 1},
{from: [null], to: 0},
{from: ["abc"], to: undefined},
{from: [{}], to: undefined},
]
coercionArrayRules.integer.array = [
{from: [1], to: 1},
{from: ["1"], to: 1},
{from: [true], to: 1},
{from: [null], to: 0},
{from: [1.5], to: undefined},
{from: ["abc"], to: undefined},
{from: [{}], to: undefined},
]
coercionArrayRules.boolean.array = [
{from: [true], to: true},
{from: ["true"], to: true},
{from: [1], to: true},
{from: [null], to: false},
{from: ["abc"], to: undefined},
{from: [2], to: undefined},
{from: [{}], to: undefined},
]
coercionArrayRules.null.array = [
{from: [null], to: null},
{from: [""], to: null},
{from: [0], to: null},
{from: [false], to: null},
{from: ["abc"], to: undefined},
{from: [1], to: undefined},
{from: [true], to: undefined},
{from: [{}], to: undefined},
]
coercionArrayRules.object.array = [{from: [{}], to: undefined}]
coercionArrayRules.array = {
string: [{from: "abc", to: ["abc"]}],
number: [{from: 1, to: [1]}],
boolean: [{from: true, to: [true]}],
null: [{from: null, to: [null]}],
object: [{from: {}, to: undefined}],
}
describe("Type coercion", () => {
let ajv: Ajv, fullAjv: Ajv, instances: Ajv[]
beforeEach(() => {
ajv = new _Ajv({coerceTypes: true, verbose: true, allowUnionTypes: true})
fullAjv = new _Ajv({coerceTypes: true, verbose: true, allErrors: true, allowUnionTypes: true})
instances = [ajv, fullAjv]
})
it("should coerce scalar values", () => {
testRules(coercionRules, (test, schema, canCoerce /*, toType, fromType*/) => {
instances.forEach((_ajv) => {
const valid = _ajv.validate(schema, test.from)
//if (valid !== canCoerce) console.log('true', toType, fromType, test, ajv.errors);
valid.should.equal(canCoerce)
})
})
})
it("should coerce scalar values (coerceTypes = array)", () => {
ajv = new _Ajv({coerceTypes: "array", verbose: true})
fullAjv = new _Ajv({coerceTypes: "array", verbose: true, allErrors: true})
instances = [ajv, fullAjv]
testRules(coercionArrayRules, (test, schema, canCoerce, toType, fromType) => {
instances.forEach((_ajv) => {
const valid = _ajv.validate(schema, test.from)
if (valid !== canCoerce) console.log(toType, ".", fromType, test, schema, ajv.errors)
valid.should.equal(canCoerce)
})
})
})
it("should coerce values in objects/arrays and update properties/items", () => {
testRules(coercionRules, (test, schema, canCoerce /*, toType, fromType*/) => {
const schemaObject = {
type: "object",
properties: {
foo: schema,
},
}
const schemaArray = {
type: "array",
items: schema,
}
const schemaArrObj = {
type: "array",
items: schemaObject,
}
instances.forEach((_ajv) => {
testCoercion(_ajv, schemaArray, [test.from], [test.to])
testCoercion(_ajv, schemaObject, {foo: test.from}, {foo: test.to})
testCoercion(_ajv, schemaArrObj, [{foo: test.from}], [{foo: test.to}])
})
function testCoercion(_ajv, _schema, fromData, toData) {
const valid = _ajv.validate(_schema, fromData)
//if (valid !== canCoerce) console.log(schema, fromData, toData);
valid.should.equal(canCoerce)
if (valid) fromData.should.eql(toData)
}
})
})
it("should coerce to multiple types in order with number type", () => {
const schema = {
type: "object",
properties: {
foo: {
type: ["number", "boolean", "null"],
},
},
}
instances.forEach((_ajv) => {
let data
_ajv.validate(schema, (data = {foo: "1"})).should.equal(true)
data.should.eql({foo: 1})
_ajv.validate(schema, (data = {foo: "1.5"})).should.equal(true)
data.should.eql({foo: 1.5})
_ajv.validate(schema, (data = {foo: "false"})).should.equal(true)
data.should.eql({foo: false})
_ajv.validate(schema, (data = {foo: 1})).should.equal(true)
data.should.eql({foo: 1}) // no coercion
_ajv.validate(schema, (data = {foo: true})).should.equal(true)
data.should.eql({foo: true}) // no coercion
_ajv.validate(schema, (data = {foo: null})).should.equal(true)
data.should.eql({foo: null}) // no coercion
_ajv.validate(schema, (data = {foo: "abc"})).should.equal(false)
data.should.eql({foo: "abc"}) // can't coerce
_ajv.validate(schema, (data = {foo: {}})).should.equal(false)
data.should.eql({foo: {}}) // can't coerce
_ajv.validate(schema, (data = {foo: []})).should.equal(false)
data.should.eql({foo: []}) // can't coerce
})
})
it("should coerce to multiple types in order with integer type", () => {
const schema = {
type: "object",
properties: {
foo: {
type: ["integer", "boolean", "null"],
},
},
}
instances.forEach((_ajv) => {
let data
_ajv.validate(schema, (data = {foo: "1"})).should.equal(true)
data.should.eql({foo: 1})
_ajv.validate(schema, (data = {foo: "false"})).should.equal(true)
data.should.eql({foo: false})
_ajv.validate(schema, (data = {foo: 1})).should.equal(true)
data.should.eql({foo: 1}) // no coercion
_ajv.validate(schema, (data = {foo: true})).should.equal(true)
data.should.eql({foo: true}) // no coercion
_ajv.validate(schema, (data = {foo: null})).should.equal(true)
data.should.eql({foo: null}) // no coercion
_ajv.validate(schema, (data = {foo: "abc"})).should.equal(false)
data.should.eql({foo: "abc"}) // can't coerce
_ajv.validate(schema, (data = {foo: {}})).should.equal(false)
data.should.eql({foo: {}}) // can't coerce
_ajv.validate(schema, (data = {foo: []})).should.equal(false)
data.should.eql({foo: []}) // can't coerce
})
})
it("should fail to coerce non-number if multiple properties/items are coerced (issue #152)", () => {
const schema = {
type: "object",
properties: {
foo: {type: "number"},
bar: {type: "number"},
},
}
const schema2 = {
type: "array",
items: {type: "number"},
}
instances.forEach((_ajv) => {
const data: any = {foo: "123", bar: "bar"}
_ajv.validate(schema, data).should.equal(false)
data.should.eql({foo: 123, bar: "bar"})
const data2: any = ["123", "bar"]
_ajv.validate(schema2, data2).should.equal(false)
data2.should.eql([123, "bar"])
})
})
it("should update data if the schema is in ref that is not inlined", () => {
instances.push(new _Ajv({coerceTypes: true, inlineRefs: false, allowUnionTypes: true}))
const schema = {
type: "object",
definitions: {
foo: {type: "number"},
},
properties: {
foo: {$ref: "#/definitions/foo"},
},
}
const schema2 = {
type: "object",
definitions: {
foo: {
// allOf is needed to make sure that "foo" is compiled to a separate function
// and not simply passed through (as it would be if it were only $ref)
allOf: [{$ref: "#/definitions/bar"}],
},
bar: {type: "number"},
},
properties: {
foo: {$ref: "#/definitions/foo"},
},
}
const schemaRecursive = {
type: ["object", "number"],
properties: {
foo: {$ref: "#"},
},
}
const schemaRecursive2 = {
$id: "http://e.com/schema.json#",
definitions: {
foo: {
$id: "http://e.com/foo.json#",
type: ["object", "number"],
properties: {
foo: {$ref: "#"},
},
},
},
type: "object",
properties: {
foo: {$ref: "http://e.com/foo.json#"},
},
}
instances.forEach((_ajv) => {
testCoercion(schema, {foo: "1"}, {foo: 1})
testCoercion(schema2, {foo: "1"}, {foo: 1})
testCoercion(schemaRecursive, {foo: {foo: "1"}}, {foo: {foo: 1}})
testCoercion(schemaRecursive2, {foo: {foo: {foo: "1"}}}, {foo: {foo: {foo: 1}}})
function testCoercion(_schema, fromData, toData) {
const valid = _ajv.validate(_schema, fromData)
// if (!valid) console.log(schema, fromData, toData);
valid.should.equal(true)
fromData.should.eql(toData)
}
})
})
it("should generate one error for type with coerceTypes option (issue #469)", () => {
const schema = {
type: "number",
minimum: 10,
}
instances.forEach((_ajv) => {
const validate = _ajv.compile(schema)
validate(9).should.equal(false)
validate.errors?.length.should.equal(1)
validate(11).should.equal(true)
validate("foo").should.equal(false)
validate.errors?.length.should.equal(1)
})
})
it('should check "uniqueItems" after coercion', () => {
const schema = {
type: "array",
items: {type: "number"},
uniqueItems: true,
}
instances.forEach((_ajv) => {
const validate = _ajv.compile(schema)
validate([1, "2", 3]).should.equal(true)
validate([1, "2", 2]).should.equal(false)
validate.errors?.length.should.equal(1)
validate.errors?.[0].keyword.should.equal("uniqueItems")
})
})
it('should check "contains" after coercion', () => {
const schema = {
type: "array",
items: {type: "number"},
contains: {const: 2},
}
instances.forEach((_ajv) => {
const validate = _ajv.compile(schema)
validate([1, "2", 3]).should.equal(true)
validate([1, "3", 4]).should.equal(false)
validate.errors?.pop()?.keyword.should.equal("contains")
})
})
function testRules(rules, cb) {
for (const toType in rules) {
for (const fromType in rules[toType]) {
const tests = rules[toType][fromType]
tests.forEach((test) => {
const canCoerce = test.to !== undefined
const schema = canCoerce
? Array.isArray(test.to)
? {type: toType, items: {type: fromType, enum: [test.to[0]]}}
: {type: toType, enum: [test.to]}
: {type: toType}
cb(test, schema, canCoerce, toType, fromType)
})
}
}
}
})