From 81f442b325d26f0e74e95e8dbded84c697f6c022 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 16 May 2016 22:02:29 +0100 Subject: [PATCH] NaN validation, closes #182 --- lib/compile/util.js | 3 ++- lib/dot/_limit.jst | 7 +++++-- spec/issues.spec.js | 29 ++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/compile/util.js b/lib/compile/util.js index ead1c72..0350af5 100644 --- a/lib/compile/util.js +++ b/lib/compile/util.js @@ -45,7 +45,8 @@ function checkDataType(dataType, data, negate) { 'typeof ' + data + EQUAL + '"object"' + AND + NOT + 'Array.isArray(' + data + '))'; case 'integer': return '(typeof ' + data + EQUAL + '"number"' + AND + - NOT + '(' + data + ' % 1))'; + NOT + '(' + data + ' % 1)' + + AND + data + EQUAL + data + ')'; default: return 'typeof ' + data + EQUAL + '"' + dataType + '"'; } } diff --git a/lib/dot/_limit.jst b/lib/dot/_limit.jst index 4b9c3a1..21793d8 100644 --- a/lib/dot/_limit.jst +++ b/lib/dot/_limit.jst @@ -29,7 +29,8 @@ } else if({{# def.$dataNotType:'number' }} ((exclusive{{=$lvl}} = {{=$schemaValueExcl}} === true) ? {{=$data}} {{=$notOp}}= {{=$schemaValue}} - : {{=$data}} {{=$notOp}} {{=$schemaValue}})) { + : {{=$data}} {{=$notOp}} {{=$schemaValue}}) + || {{=$data}} !== {{=$data}}) { var op{{=$lvl}} = exclusive{{=$lvl}} ? '{{=$op}}' : '{{=$op}}='; {{??}} {{ @@ -39,7 +40,9 @@ var $opExpr = '\'' + $opStr + '\''; /*used in error*/ }} - if ({{# def.$dataNotType:'number' }} {{=$data}} {{=$notOp}}{{?$exclusive}}={{?}} {{=$schemaValue}}) { + if ({{# def.$dataNotType:'number' }} + {{=$data}} {{=$notOp}}{{?$exclusive}}={{?}} {{=$schemaValue}} + || {{=$data}} !== {{=$data}}) { {{?}} {{ var $errorKeyword = $keyword; }} {{# def.error:'_limit' }} diff --git a/spec/issues.spec.js b/spec/issues.spec.js index 9fcf307..55a1240 100644 --- a/spec/issues.spec.js +++ b/spec/issues.spec.js @@ -7,7 +7,7 @@ var Ajv = require('./ajv') describe('issue #8: schema with shared references', function() { it('should be supported by addSchema', spec('addSchema')); - it('should be supported by compile', spec('addSchema')); + it('should be supported by compile', spec('compile')); function spec(method) { return function() { @@ -82,3 +82,30 @@ describe('issue #50: references with "definitions"', function () { }; } }); + + +describe('issue #182, NaN validation', function() { + var ajv; + + before(function(){ + ajv = Ajv(); + }); + + it('should not pass minimum/maximum validation', function() { + testNaN({ minimum: 1 }, false); + testNaN({ maximum: 1 }, false); + }); + + it('should pass type: number validation', function() { + testNaN({ type: 'number' }, true); + }); + + it('should not pass type: integer validation', function() { + testNaN({ type: 'integer' }, false); + }); + + function testNaN(schema, NaNisValid) { + var validate = Ajv().compile(schema); + validate(NaN) .should.equal(NaNisValid); + } +});