NaN validation, closes #182

master
Evgeny Poberezkin 2016-05-16 22:02:29 +01:00
parent dc551094a8
commit 81f442b325
3 changed files with 35 additions and 4 deletions

View File

@ -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 + '"';
}
}

View File

@ -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' }}

View File

@ -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);
}
});