diff --git a/lib/dot/definitions.def b/lib/dot/definitions.def index bb40d18c9..99002bb7d 100644 --- a/lib/dot/definitions.def +++ b/lib/dot/definitions.def @@ -2,7 +2,6 @@ {{ var $lvl = it.level; var $dataLvl = it.dataLevel; - var $type = it.schema.type || ''; var $schema = it.schema[$keyword]; var $schemaPath = it.schemaPath + it.util.getProperty($keyword); var $errSchemaPath = it.errSchemaPath + '/' + $keyword; diff --git a/lib/dot/format.jst b/lib/dot/format.jst index b2894611e..17e783a43 100644 --- a/lib/dot/format.jst +++ b/lib/dot/format.jst @@ -49,6 +49,9 @@ }} {{? $isData }} + {{? $ruleType != 'string' }} + {{# def.skipFormat }} + {{?}} {{ var $format = 'format' + $lvl; }} var {{=$format}} = formats[{{=$schemaValue}}]; var isObject{{=$lvl}} = typeof {{=$format}} == 'object' @@ -77,13 +80,13 @@ var $isObject = typeof $format == 'object' && !($format instanceof RegExp) && $format.validate; - var $types = $isObject && $format.types || ['string']; + var $formatType = $isObject && $format.type || 'string'; if ($isObject) { var $async = $format.async === true; $format = $format.validate; } }} - {{? $types.indexOf($type) === -1 }} + {{? $formatType != $ruleType }} {{# def.skipFormat }} {{?}} {{? $async }} diff --git a/lib/dot/validate.jst b/lib/dot/validate.jst index 42382243a..629a68451 100644 --- a/lib/dot/validate.jst +++ b/lib/dot/validate.jst @@ -173,7 +173,7 @@ {{?}} {{~ $rulesGroup.rules:$rule }} {{? $shouldUseRule($rule) }} - {{ var $code = $rule.code(it, $rule.keyword); }} + {{ var $code = $rule.code(it, $rule.keyword, $rulesGroup.type); }} {{? $code }} {{= $code }} {{? $breakOnError }} diff --git a/scripts/compile-dots.js b/scripts/compile-dots.js index 7150bebfa..92fb5430e 100644 --- a/scripts/compile-dots.js +++ b/scripts/compile-dots.js @@ -36,7 +36,7 @@ files.forEach(function (f) { var code = doT.compile(template, defs); code = code.toString() .replace(OUT_EMPTY_STRING, '') - .replace(FUNCTION_NAME, 'function generate_' + keyword + '(it, $keyword) {') + .replace(FUNCTION_NAME, 'function generate_' + keyword + '(it, $keyword, $ruleType) {') .replace(ISTANBUL, '/* $1 */'); VARS.forEach(removeUnusedVar); code = "'use strict';\nmodule.exports = " + code; diff --git a/spec/ajv.spec.js b/spec/ajv.spec.js index 71785f24e..024005b13 100644 --- a/spec/ajv.spec.js +++ b/spec/ajv.spec.js @@ -386,70 +386,52 @@ describe('Ajv', function () { describe('addFormat method', function() { - describe.skip('without any type restrictions specified', function() { - it('should add format as regular expression', function() { - ajv.addFormat('identifier', /^[a-z_$][a-z0-9_$]*$/i); - testFormat(); - }); + it('should add format as regular expression', function() { + ajv.addFormat('identifier', /^[a-z_$][a-z0-9_$]*$/i); + testFormat(); + }); - it('should add format as string', function() { - ajv.addFormat('identifier', '^[A-Za-z_$][A-Za-z0-9_$]*$'); - testFormat(); - }); + it('should add format as string', function() { + ajv.addFormat('identifier', '^[A-Za-z_$][A-Za-z0-9_$]*$'); + testFormat(); + }); - it('should add format as function', function() { - ajv.addFormat('identifier', function (str) { return /^[a-z_$][a-z0-9_$]*$/i.test(str); }); - testFormat(); - }); + it('should add format as function', function() { + ajv.addFormat('identifier', function (str) { return /^[a-z_$][a-z0-9_$]*$/i.test(str); }); + testFormat(); + }); - it('should add format as object', function() { - ajv.addFormat('identifier', { - validate: function (str) { return /^[a-z_$][a-z0-9_$]*$/i.test(str); }, - }); - testFormat(); + it('should add format as object', function() { + ajv.addFormat('identifier', { + validate: function (str) { return /^[a-z_$][a-z0-9_$]*$/i.test(str); }, }); - - function testFormat() { - var validate = ajv.compile({ format: 'identifier' }); - validate('Abc1') .should.equal(true); - validate('123') .should.equal(false); - validate(123) .should.equal(true); - } + testFormat(); }); - describe('with type restrictions specified', function() { - it('should apply the format to the type specified', function() { + function testFormat() { + var validate = ajv.compile({ format: 'identifier' }); + validate('Abc1') .should.equal(true); + validate('123') .should.equal(false); + validate(123) .should.equal(true); + } + describe('formats for number', function() { + it('should validate only numbers', function() { ajv.addFormat('positive', { - types: ['number'], - validate: function(x) { - return x >= 0; - } - }); - - var validate = ajv.compile({ type: 'number', - format: 'positive' - }); - validate(-12.3) .should.equal(false); - validate(12.3) .should.equal(true); - }); - - it('should not fail types not specified', function() { - ajv.addFormat('positive', { - types: ['number'], validate: function(x) { - return x >= 0; + return x > 0; } }); var validate = ajv.compile({ - type: 'string', format: 'positive' }); - validate('Abc1') .should.equal(true); + validate(-2) .should.equal(false); + validate(0) .should.equal(false); + validate(2) .should.equal(true); + validate('abc') .should.equal(true); }); - }); });