Skip to content

Commit

Permalink
fix: support multiple types for format, #291
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Feb 10, 2017
1 parent 1d84987 commit c21cbee
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 52 deletions.
1 change: 0 additions & 1 deletion lib/dot/definitions.def
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions lib/dot/format.jst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
}}

{{? $isData }}
{{? $ruleType != 'string' }}
{{# def.skipFormat }}
{{?}}
{{ var $format = 'format' + $lvl; }}
var {{=$format}} = formats[{{=$schemaValue}}];
var isObject{{=$lvl}} = typeof {{=$format}} == 'object'
Expand Down Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion lib/dot/validate.jst
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion scripts/compile-dots.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 29 additions & 47 deletions spec/ajv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
});

Expand Down

0 comments on commit c21cbee

Please sign in to comment.