| 1 |
import type Ajv from ".." |
| 2 |
import _Ajv from "./ajv" |
| 3 |
import chai from "./chai" |
| 4 |
chai.should() |
| 5 |
|
| 6 |
const coercionRules = { |
| 7 |
string: { |
| 8 |
number: [ |
| 9 |
{from: 1, to: "1"}, |
| 10 |
{from: 1.5, to: "1.5"}, |
| 11 |
{from: 2e100, to: "2e+100"}, |
| 12 |
], |
| 13 |
boolean: [ |
| 14 |
{from: false, to: "false"}, |
| 15 |
{from: true, to: "true"}, |
| 16 |
], |
| 17 |
null: [{from: null, to: ""}], |
| 18 |
object: [{from: {}, to: undefined}], |
| 19 |
array: [ |
| 20 |
{from: [], to: undefined}, |
| 21 |
{from: [1], to: undefined}, |
| 22 |
], |
| 23 |
}, |
| 24 |
number: { |
| 25 |
string: [ |
| 26 |
{from: "1", to: 1}, |
| 27 |
{from: "1.5", to: 1.5}, |
| 28 |
{from: "2e10", to: 2e10}, |
| 29 |
{from: "1a", to: undefined}, |
| 30 |
{from: "abc", to: undefined}, |
| 31 |
{from: "", to: undefined}, |
| 32 |
], |
| 33 |
boolean: [ |
| 34 |
{from: false, to: 0}, |
| 35 |
{from: true, to: 1}, |
| 36 |
], |
| 37 |
null: [{from: null, to: 0}], |
| 38 |
object: [{from: {}, to: undefined}], |
| 39 |
array: [ |
| 40 |
{from: [], to: undefined}, |
| 41 |
{from: [true], to: undefined}, |
| 42 |
], |
| 43 |
}, |
| 44 |
integer: { |
| 45 |
string: [ |
| 46 |
{from: "1", to: 1}, |
| 47 |
{from: "1.5", to: undefined}, |
| 48 |
{from: "2e10", to: 2e10}, |
| 49 |
{from: "1a", to: undefined}, |
| 50 |
{from: "abc", to: undefined}, |
| 51 |
{from: "", to: undefined}, |
| 52 |
], |
| 53 |
boolean: [ |
| 54 |
{from: false, to: 0}, |
| 55 |
{from: true, to: 1}, |
| 56 |
], |
| 57 |
null: [{from: null, to: 0}], |
| 58 |
object: [{from: {}, to: undefined}], |
| 59 |
array: [ |
| 60 |
{from: [], to: undefined}, |
| 61 |
{from: ["1"], to: undefined}, |
| 62 |
], |
| 63 |
}, |
| 64 |
boolean: { |
| 65 |
string: [ |
| 66 |
{from: "false", to: false}, |
| 67 |
{from: "true", to: true}, |
| 68 |
{from: "", to: undefined}, |
| 69 |
{from: "abc", to: undefined}, |
| 70 |
], |
| 71 |
number: [ |
| 72 |
{from: 0, to: false}, |
| 73 |
{from: 1, to: true}, |
| 74 |
{from: 2, to: undefined}, |
| 75 |
{from: 2.5, to: undefined}, |
| 76 |
], |
| 77 |
null: [{from: null, to: false}], |
| 78 |
object: [{from: {}, to: undefined}], |
| 79 |
array: [ |
| 80 |
{from: [], to: undefined}, |
| 81 |
{from: [0], to: undefined}, |
| 82 |
], |
| 83 |
}, |
| 84 |
null: { |
| 85 |
string: [ |
| 86 |
{from: "", to: null}, |
| 87 |
{from: "abc", to: undefined}, |
| 88 |
{from: "null", to: undefined}, |
| 89 |
], |
| 90 |
number: [ |
| 91 |
{from: 0, to: null}, |
| 92 |
{from: 1, to: undefined}, |
| 93 |
], |
| 94 |
boolean: [ |
| 95 |
{from: false, to: null}, |
| 96 |
{from: true, to: undefined}, |
| 97 |
], |
| 98 |
object: [{from: {}, to: undefined}], |
| 99 |
array: [ |
| 100 |
{from: [], to: undefined}, |
| 101 |
{from: [null], to: undefined}, |
| 102 |
], |
| 103 |
}, |
| 104 |
array: { |
| 105 |
all: [ |
| 106 |
{type: "string", from: "abc", to: undefined}, |
| 107 |
{type: "number", from: 1, to: undefined}, |
| 108 |
{type: "boolean", from: true, to: undefined}, |
| 109 |
{type: "null", from: null, to: undefined}, |
| 110 |
{type: "object", from: {}, to: undefined}, |
| 111 |
], |
| 112 |
}, |
| 113 |
object: { |
| 114 |
all: [ |
| 115 |
{type: "string", from: "abc", to: undefined}, |
| 116 |
{type: "number", from: 1, to: undefined}, |
| 117 |
{type: "boolean", from: true, to: undefined}, |
| 118 |
{type: "null", from: null, to: undefined}, |
| 119 |
{type: "array", from: [], to: undefined}, |
| 120 |
], |
| 121 |
}, |
| 122 |
} |
| 123 |
|
| 124 |
const coercionArrayRules = JSON.parse(JSON.stringify(coercionRules)) |
| 125 |
coercionArrayRules.string.array = [ |
| 126 |
{from: ["abc"], to: "abc"}, |
| 127 |
{from: [123], to: "123"}, |
| 128 |
{from: [true], to: "true"}, |
| 129 |
{from: [null], to: ""}, |
| 130 |
{from: [{}], to: undefined}, |
| 131 |
{from: ["abc", "def"], to: undefined}, |
| 132 |
{from: [], to: undefined}, |
| 133 |
] |
| 134 |
coercionArrayRules.number.array = [ |
| 135 |
{from: [1.5], to: 1.5}, |
| 136 |
{from: ["1.5"], to: 1.5}, |
| 137 |
{from: [true], to: 1}, |
| 138 |
{from: [null], to: 0}, |
| 139 |
{from: ["abc"], to: undefined}, |
| 140 |
{from: [{}], to: undefined}, |
| 141 |
] |
| 142 |
coercionArrayRules.integer.array = [ |
| 143 |
{from: [1], to: 1}, |
| 144 |
{from: ["1"], to: 1}, |
| 145 |
{from: [true], to: 1}, |
| 146 |
{from: [null], to: 0}, |
| 147 |
{from: [1.5], to: undefined}, |
| 148 |
{from: ["abc"], to: undefined}, |
| 149 |
{from: [{}], to: undefined}, |
| 150 |
] |
| 151 |
coercionArrayRules.boolean.array = [ |
| 152 |
{from: [true], to: true}, |
| 153 |
{from: ["true"], to: true}, |
| 154 |
{from: [1], to: true}, |
| 155 |
{from: [null], to: false}, |
| 156 |
{from: ["abc"], to: undefined}, |
| 157 |
{from: [2], to: undefined}, |
| 158 |
{from: [{}], to: undefined}, |
| 159 |
] |
| 160 |
coercionArrayRules.null.array = [ |
| 161 |
{from: [null], to: null}, |
| 162 |
{from: [""], to: null}, |
| 163 |
{from: [0], to: null}, |
| 164 |
{from: [false], to: null}, |
| 165 |
{from: ["abc"], to: undefined}, |
| 166 |
{from: [1], to: undefined}, |
| 167 |
{from: [true], to: undefined}, |
| 168 |
{from: [{}], to: undefined}, |
| 169 |
] |
| 170 |
coercionArrayRules.object.array = [{from: [{}], to: undefined}] |
| 171 |
|
| 172 |
coercionArrayRules.array = { |
| 173 |
string: [{from: "abc", to: ["abc"]}], |
| 174 |
number: [{from: 1, to: [1]}], |
| 175 |
boolean: [{from: true, to: [true]}], |
| 176 |
null: [{from: null, to: [null]}], |
| 177 |
object: [{from: {}, to: undefined}], |
| 178 |
} |
| 179 |
|
| 180 |
describe("Type coercion", () => { |
| 181 |
let ajv: Ajv, fullAjv: Ajv, instances: Ajv[] |
| 182 |
|
| 183 |
beforeEach(() => { |
| 184 |
ajv = new _Ajv({coerceTypes: true, verbose: true, allowUnionTypes: true}) |
| 185 |
fullAjv = new _Ajv({coerceTypes: true, verbose: true, allErrors: true, allowUnionTypes: true}) |
| 186 |
instances = [ajv, fullAjv] |
| 187 |
}) |
| 188 |
|
| 189 |
it("should coerce scalar values", () => { |
| 190 |
testRules(coercionRules, (test, schema, canCoerce /*, toType, fromType*/) => { |
| 191 |
instances.forEach((_ajv) => { |
| 192 |
const valid = _ajv.validate(schema, test.from) |
| 193 |
//if (valid !== canCoerce) console.log('true', toType, fromType, test, ajv.errors); |
| 194 |
valid.should.equal(canCoerce) |
| 195 |
}) |
| 196 |
}) |
| 197 |
}) |
| 198 |
|
| 199 |
it("should coerce scalar values (coerceTypes = array)", () => { |
| 200 |
ajv = new _Ajv({coerceTypes: "array", verbose: true}) |
| 201 |
fullAjv = new _Ajv({coerceTypes: "array", verbose: true, allErrors: true}) |
| 202 |
instances = [ajv, fullAjv] |
| 203 |
|
| 204 |
testRules(coercionArrayRules, (test, schema, canCoerce, toType, fromType) => { |
| 205 |
instances.forEach((_ajv) => { |
| 206 |
const valid = _ajv.validate(schema, test.from) |
| 207 |
if (valid !== canCoerce) console.log(toType, ".", fromType, test, schema, ajv.errors) |
| 208 |
valid.should.equal(canCoerce) |
| 209 |
}) |
| 210 |
}) |
| 211 |
}) |
| 212 |
|
| 213 |
it("should coerce values in objects/arrays and update properties/items", () => { |
| 214 |
testRules(coercionRules, (test, schema, canCoerce /*, toType, fromType*/) => { |
| 215 |
const schemaObject = { |
| 216 |
type: "object", |
| 217 |
properties: { |
| 218 |
foo: schema, |
| 219 |
}, |
| 220 |
} |
| 221 |
|
| 222 |
const schemaArray = { |
| 223 |
type: "array", |
| 224 |
items: schema, |
| 225 |
} |
| 226 |
|
| 227 |
const schemaArrObj = { |
| 228 |
type: "array", |
| 229 |
items: schemaObject, |
| 230 |
} |
| 231 |
|
| 232 |
instances.forEach((_ajv) => { |
| 233 |
testCoercion(_ajv, schemaArray, [test.from], [test.to]) |
| 234 |
testCoercion(_ajv, schemaObject, {foo: test.from}, {foo: test.to}) |
| 235 |
testCoercion(_ajv, schemaArrObj, [{foo: test.from}], [{foo: test.to}]) |
| 236 |
}) |
| 237 |
|
| 238 |
function testCoercion(_ajv, _schema, fromData, toData) { |
| 239 |
const valid = _ajv.validate(_schema, fromData) |
| 240 |
//if (valid !== canCoerce) console.log(schema, fromData, toData); |
| 241 |
valid.should.equal(canCoerce) |
| 242 |
if (valid) fromData.should.eql(toData) |
| 243 |
} |
| 244 |
}) |
| 245 |
}) |
| 246 |
|
| 247 |
it("should coerce to multiple types in order with number type", () => { |
| 248 |
const schema = { |
| 249 |
type: "object", |
| 250 |
properties: { |
| 251 |
foo: { |
| 252 |
type: ["number", "boolean", "null"], |
| 253 |
}, |
| 254 |
}, |
| 255 |
} |
| 256 |
|
| 257 |
instances.forEach((_ajv) => { |
| 258 |
let data |
| 259 |
|
| 260 |
_ajv.validate(schema, (data = {foo: "1"})).should.equal(true) |
| 261 |
data.should.eql({foo: 1}) |
| 262 |
|
| 263 |
_ajv.validate(schema, (data = {foo: "1.5"})).should.equal(true) |
| 264 |
data.should.eql({foo: 1.5}) |
| 265 |
|
| 266 |
_ajv.validate(schema, (data = {foo: "false"})).should.equal(true) |
| 267 |
data.should.eql({foo: false}) |
| 268 |
|
| 269 |
_ajv.validate(schema, (data = {foo: 1})).should.equal(true) |
| 270 |
data.should.eql({foo: 1}) // no coercion |
| 271 |
|
| 272 |
_ajv.validate(schema, (data = {foo: true})).should.equal(true) |
| 273 |
data.should.eql({foo: true}) // no coercion |
| 274 |
|
| 275 |
_ajv.validate(schema, (data = {foo: null})).should.equal(true) |
| 276 |
data.should.eql({foo: null}) // no coercion |
| 277 |
|
| 278 |
_ajv.validate(schema, (data = {foo: "abc"})).should.equal(false) |
| 279 |
data.should.eql({foo: "abc"}) // can't coerce |
| 280 |
|
| 281 |
_ajv.validate(schema, (data = {foo: {}})).should.equal(false) |
| 282 |
data.should.eql({foo: {}}) // can't coerce |
| 283 |
|
| 284 |
_ajv.validate(schema, (data = {foo: []})).should.equal(false) |
| 285 |
data.should.eql({foo: []}) // can't coerce |
| 286 |
}) |
| 287 |
}) |
| 288 |
|
| 289 |
it("should coerce to multiple types in order with integer type", () => { |
| 290 |
const schema = { |
| 291 |
type: "object", |
| 292 |
properties: { |
| 293 |
foo: { |
| 294 |
type: ["integer", "boolean", "null"], |
| 295 |
}, |
| 296 |
}, |
| 297 |
} |
| 298 |
|
| 299 |
instances.forEach((_ajv) => { |
| 300 |
let data |
| 301 |
|
| 302 |
_ajv.validate(schema, (data = {foo: "1"})).should.equal(true) |
| 303 |
data.should.eql({foo: 1}) |
| 304 |
|
| 305 |
_ajv.validate(schema, (data = {foo: "false"})).should.equal(true) |
| 306 |
data.should.eql({foo: false}) |
| 307 |
|
| 308 |
_ajv.validate(schema, (data = {foo: 1})).should.equal(true) |
| 309 |
data.should.eql({foo: 1}) // no coercion |
| 310 |
|
| 311 |
_ajv.validate(schema, (data = {foo: true})).should.equal(true) |
| 312 |
data.should.eql({foo: true}) // no coercion |
| 313 |
|
| 314 |
_ajv.validate(schema, (data = {foo: null})).should.equal(true) |
| 315 |
data.should.eql({foo: null}) // no coercion |
| 316 |
|
| 317 |
_ajv.validate(schema, (data = {foo: "abc"})).should.equal(false) |
| 318 |
data.should.eql({foo: "abc"}) // can't coerce |
| 319 |
|
| 320 |
_ajv.validate(schema, (data = {foo: {}})).should.equal(false) |
| 321 |
data.should.eql({foo: {}}) // can't coerce |
| 322 |
|
| 323 |
_ajv.validate(schema, (data = {foo: []})).should.equal(false) |
| 324 |
data.should.eql({foo: []}) // can't coerce |
| 325 |
}) |
| 326 |
}) |
| 327 |
|
| 328 |
it("should fail to coerce non-number if multiple properties/items are coerced (issue #152)", () => { |
| 329 |
const schema = { |
| 330 |
type: "object", |
| 331 |
properties: { |
| 332 |
foo: {type: "number"}, |
| 333 |
bar: {type: "number"}, |
| 334 |
}, |
| 335 |
} |
| 336 |
|
| 337 |
const schema2 = { |
| 338 |
type: "array", |
| 339 |
items: {type: "number"}, |
| 340 |
} |
| 341 |
|
| 342 |
instances.forEach((_ajv) => { |
| 343 |
const data: any = {foo: "123", bar: "bar"} |
| 344 |
_ajv.validate(schema, data).should.equal(false) |
| 345 |
data.should.eql({foo: 123, bar: "bar"}) |
| 346 |
|
| 347 |
const data2: any = ["123", "bar"] |
| 348 |
_ajv.validate(schema2, data2).should.equal(false) |
| 349 |
data2.should.eql([123, "bar"]) |
| 350 |
}) |
| 351 |
}) |
| 352 |
|
| 353 |
it("should update data if the schema is in ref that is not inlined", () => { |
| 354 |
instances.push(new _Ajv({coerceTypes: true, inlineRefs: false, allowUnionTypes: true})) |
| 355 |
|
| 356 |
const schema = { |
| 357 |
type: "object", |
| 358 |
definitions: { |
| 359 |
foo: {type: "number"}, |
| 360 |
}, |
| 361 |
properties: { |
| 362 |
foo: {$ref: "#/definitions/foo"}, |
| 363 |
}, |
| 364 |
} |
| 365 |
|
| 366 |
const schema2 = { |
| 367 |
type: "object", |
| 368 |
definitions: { |
| 369 |
foo: { |
| 370 |
// allOf is needed to make sure that "foo" is compiled to a separate function |
| 371 |
// and not simply passed through (as it would be if it were only $ref) |
| 372 |
allOf: [{$ref: "#/definitions/bar"}], |
| 373 |
}, |
| 374 |
bar: {type: "number"}, |
| 375 |
}, |
| 376 |
properties: { |
| 377 |
foo: {$ref: "#/definitions/foo"}, |
| 378 |
}, |
| 379 |
} |
| 380 |
|
| 381 |
const schemaRecursive = { |
| 382 |
type: ["object", "number"], |
| 383 |
properties: { |
| 384 |
foo: {$ref: "#"}, |
| 385 |
}, |
| 386 |
} |
| 387 |
|
| 388 |
const schemaRecursive2 = { |
| 389 |
$id: "http://e.com/schema.json#", |
| 390 |
definitions: { |
| 391 |
foo: { |
| 392 |
$id: "http://e.com/foo.json#", |
| 393 |
type: ["object", "number"], |
| 394 |
properties: { |
| 395 |
foo: {$ref: "#"}, |
| 396 |
}, |
| 397 |
}, |
| 398 |
}, |
| 399 |
type: "object", |
| 400 |
properties: { |
| 401 |
foo: {$ref: "http://e.com/foo.json#"}, |
| 402 |
}, |
| 403 |
} |
| 404 |
|
| 405 |
instances.forEach((_ajv) => { |
| 406 |
testCoercion(schema, {foo: "1"}, {foo: 1}) |
| 407 |
testCoercion(schema2, {foo: "1"}, {foo: 1}) |
| 408 |
testCoercion(schemaRecursive, {foo: {foo: "1"}}, {foo: {foo: 1}}) |
| 409 |
testCoercion(schemaRecursive2, {foo: {foo: {foo: "1"}}}, {foo: {foo: {foo: 1}}}) |
| 410 |
|
| 411 |
function testCoercion(_schema, fromData, toData) { |
| 412 |
const valid = _ajv.validate(_schema, fromData) |
| 413 |
// if (!valid) console.log(schema, fromData, toData); |
| 414 |
valid.should.equal(true) |
| 415 |
fromData.should.eql(toData) |
| 416 |
} |
| 417 |
}) |
| 418 |
}) |
| 419 |
|
| 420 |
it("should generate one error for type with coerceTypes option (issue #469)", () => { |
| 421 |
const schema = { |
| 422 |
type: "number", |
| 423 |
minimum: 10, |
| 424 |
} |
| 425 |
|
| 426 |
instances.forEach((_ajv) => { |
| 427 |
const validate = _ajv.compile(schema) |
| 428 |
validate(9).should.equal(false) |
| 429 |
validate.errors?.length.should.equal(1) |
| 430 |
|
| 431 |
validate(11).should.equal(true) |
| 432 |
|
| 433 |
validate("foo").should.equal(false) |
| 434 |
validate.errors?.length.should.equal(1) |
| 435 |
}) |
| 436 |
}) |
| 437 |
|
| 438 |
it('should check "uniqueItems" after coercion', () => { |
| 439 |
const schema = { |
| 440 |
type: "array", |
| 441 |
items: {type: "number"}, |
| 442 |
uniqueItems: true, |
| 443 |
} |
| 444 |
|
| 445 |
instances.forEach((_ajv) => { |
| 446 |
const validate = _ajv.compile(schema) |
| 447 |
validate([1, "2", 3]).should.equal(true) |
| 448 |
|
| 449 |
validate([1, "2", 2]).should.equal(false) |
| 450 |
validate.errors?.length.should.equal(1) |
| 451 |
validate.errors?.[0].keyword.should.equal("uniqueItems") |
| 452 |
}) |
| 453 |
}) |
| 454 |
|
| 455 |
it('should check "contains" after coercion', () => { |
| 456 |
const schema = { |
| 457 |
type: "array", |
| 458 |
items: {type: "number"}, |
| 459 |
contains: {const: 2}, |
| 460 |
} |
| 461 |
|
| 462 |
instances.forEach((_ajv) => { |
| 463 |
const validate = _ajv.compile(schema) |
| 464 |
validate([1, "2", 3]).should.equal(true) |
| 465 |
|
| 466 |
validate([1, "3", 4]).should.equal(false) |
| 467 |
validate.errors?.pop()?.keyword.should.equal("contains") |
| 468 |
}) |
| 469 |
}) |
| 470 |
|
| 471 |
function testRules(rules, cb) { |
| 472 |
for (const toType in rules) { |
| 473 |
for (const fromType in rules[toType]) { |
| 474 |
const tests = rules[toType][fromType] |
| 475 |
tests.forEach((test) => { |
| 476 |
const canCoerce = test.to !== undefined |
| 477 |
const schema = canCoerce |
| 478 |
? Array.isArray(test.to) |
| 479 |
? {type: toType, items: {type: fromType, enum: [test.to[0]]}} |
| 480 |
: {type: toType, enum: [test.to]} |
| 481 |
: {type: toType} |
| 482 |
cb(test, schema, canCoerce, toType, fromType) |
| 483 |
}) |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
}) |