Skip to content

Commit

Permalink
Merge ssh://github.com/nimerritt/ajv into nimerritt-master
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Feb 10, 2017
2 parents f4f8078 + ac20d32 commit 1d84987
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/compile/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function rules() {
var RULES = [
{ type: 'number',
rules: [ { 'maximum': ['exclusiveMaximum'] },
{ 'minimum': ['exclusiveMinimum'] }, 'multipleOf'] },
{ 'minimum': ['exclusiveMinimum'] }, 'multipleOf', 'format'] },
{ type: 'string',
rules: [ 'maxLength', 'minLength', 'pattern', 'format' ] },
{ type: 'array',
Expand Down
1 change: 1 addition & 0 deletions lib/dot/definitions.def
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{{
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
4 changes: 4 additions & 0 deletions lib/dot/format.jst
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@
var $isObject = typeof $format == 'object'
&& !($format instanceof RegExp)
&& $format.validate;
var $types = $isObject && $format.types || ['string'];
if ($isObject) {
var $async = $format.async === true;
$format = $format.validate;
}
}}
{{? $types.indexOf($type) === -1 }}
{{# def.skipFormat }}
{{?}}
{{? $async }}
{{
if (!it.async) throw new Error('async format in sync schema');
Expand Down
78 changes: 61 additions & 17 deletions spec/ajv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,27 +386,71 @@ describe('Ajv', function () {


describe('addFormat method', function() {
it('should add format as regular expression', function() {
ajv.addFormat('identifier', /^[a-z_$][a-z0-9_$]*$/i);
testFormat();
});
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 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();
});

function testFormat() {
var validate = ajv.compile({ format: 'identifier' });
validate('Abc1') .should.equal(true);
validate('123') .should.equal(false);
validate(123) .should.equal(true);
}
});

function testFormat() {
var validate = ajv.compile({ format: 'identifier' });
validate('Abc1') .should.equal(true);
validate('123') .should.equal(false);
validate(123) .should.equal(true);
}
describe('with type restrictions specified', function() {
it('should apply the format to the type specified', 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;
}
});

var validate = ajv.compile({
type: 'string',
format: 'positive'
});
validate('Abc1') .should.equal(true);
});

});
});


Expand Down

0 comments on commit 1d84987

Please sign in to comment.