Skip to content

Commit

Permalink
Merge pull request #694 from igor-savin-ht/master
Browse files Browse the repository at this point in the history
Test for integer coercion
  • Loading branch information
epoberezkin committed Feb 12, 2018
2 parents 77e63cc + 6cdc1b8 commit a6df586
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion spec/coercion.spec.js
Expand Up @@ -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: {
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit a6df586

Please sign in to comment.