Skip to content

Commit

Permalink
feat: support formats with $data for numbers, #291
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Feb 10, 2017
1 parent c21cbee commit 901343c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions lib/dot/format.jst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
&& self._opts.unknownFormats.indexOf({{=$schemaValue}}) == -1
{{?}}) ||
{{?}}
({{=$format}} && !(typeof {{=$format}} == 'function'
({{=$format}} && {{=$formatType}} == '{{=$ruleType}}'
&& !(typeof {{=$format}} == 'function'
? {{? it.async}}
(async{{=$lvl}} ? {{=it.yieldAwait}} {{=$format}}({{=$data}}) : {{=$format}}({{=$data}}))
{{??}}
Expand Down Expand Up @@ -49,15 +50,17 @@
}}

{{? $isData }}
{{? $ruleType != 'string' }}
{{# def.skipFormat }}
{{?}}
{{ var $format = 'format' + $lvl; }}
{{
var $format = 'format' + $lvl
, $isObject = 'isObject' + $lvl
, $formatType = 'formatType' + $lvl;
}}
var {{=$format}} = formats[{{=$schemaValue}}];
var isObject{{=$lvl}} = typeof {{=$format}} == 'object'
&& !({{=$format}} instanceof RegExp)
&& {{=$format}}.validate;
if (isObject{{=$lvl}}) {
var {{=$isObject}} = typeof {{=$format}} == 'object'
&& !({{=$format}} instanceof RegExp)
&& {{=$format}}.validate;
var {{=$formatType}} = {{=$isObject}} && {{=$format}}.type || 'string';
if ({{=$isObject}}) {
{{? it.async}}
var async{{=$lvl}} = {{=$format}}.async;
{{?}}
Expand Down
4 changes: 2 additions & 2 deletions spec/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ describe('Ajv Options', function () {
}
});

validate({foo: 1, bar: 'unknown'}) .should.equal(true);
validate({foo: 1, bar: 'unknown'}) .should.equal(false);
validate({foo: '2016-10-16', bar: 'date'}) .should.equal(true);
validate({foo: '20161016', bar: 'date'}) .should.equal(false);
validate({foo: '20161016'}) .should.equal(true);
Expand Down Expand Up @@ -932,7 +932,7 @@ describe('Ajv Options', function () {
});

validate({foo: 1, bar: 'allowed'}) .should.equal(true);
validate({foo: 1, bar: 'unknown'}) .should.equal(true);
validate({foo: 1, bar: 'unknown'}) .should.equal(false);
validate({foo: '2016-10-16', bar: 'date'}) .should.equal(true);
validate({foo: '20161016', bar: 'date'}) .should.equal(false);
validate({foo: '20161016'}) .should.equal(true);
Expand Down

0 comments on commit 901343c

Please sign in to comment.