Merge pull request #694 from igor-savin-ht/master

Test for integer coercion
master
Evgeny Poberezkin 2018-02-12 12:41:48 +00:00 committed by GitHub
commit a6df5867f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 1 deletions

View File

@ -260,7 +260,7 @@ describe('Type coercion', function () {
});
it('should coerce to multiple types in order', function() {
it('should coerce to multiple types in order with number type', function() {
var schema = {
type: 'object',
properties: {
@ -302,6 +302,45 @@ describe('Type coercion', function () {
});
});
it('should coerce to multiple types in order with integer type', function() {
var schema = {
type: 'object',
properties: {
foo: {
type: [ 'integer', 'boolean', 'null' ]
}
}
};
instances.forEach(function (_ajv) {
var 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)', function() {
var schema = {