Skip to content

Commit

Permalink
validate numbers in schemas during schema compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Jun 30, 2020
1 parent 24d4f8f commit 65b2f7d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/dot/_limit.jst
Expand Up @@ -17,6 +17,15 @@
, $op = $isMax ? '<' : '>'
, $notOp = $isMax ? '>' : '<'
, $errorKeyword = undefined;

if (!($isData || typeof $schema == 'number' || $schema === undefined)) {
throw new Error($keyword + ' must be number');
}
if (!($isDataExcl || $schemaExcl === undefined
|| typeof $schemaExcl == 'number'
|| typeof $schemaExcl == 'boolean')) {
throw new Error($exclusiveKeyword + ' must be number or boolean');
}
}}

{{? $isDataExcl }}
Expand Down
2 changes: 2 additions & 0 deletions lib/dot/_limitItems.jst
Expand Up @@ -3,6 +3,8 @@
{{# def.setupKeyword }}
{{# def.$data }}

{{# def.numberKeyword }}

{{ var $op = $keyword == 'maxItems' ? '>' : '<'; }}
if ({{# def.$dataNotType:'number' }} {{=$data}}.length {{=$op}} {{=$schemaValue}}) {
{{ var $errorKeyword = $keyword; }}
Expand Down
2 changes: 2 additions & 0 deletions lib/dot/_limitLength.jst
Expand Up @@ -3,6 +3,8 @@
{{# def.setupKeyword }}
{{# def.$data }}

{{# def.numberKeyword }}

{{ var $op = $keyword == 'maxLength' ? '>' : '<'; }}
if ({{# def.$dataNotType:'number' }} {{# def.strLength }} {{=$op}} {{=$schemaValue}}) {
{{ var $errorKeyword = $keyword; }}
Expand Down
2 changes: 2 additions & 0 deletions lib/dot/_limitProperties.jst
Expand Up @@ -3,6 +3,8 @@
{{# def.setupKeyword }}
{{# def.$data }}

{{# def.numberKeyword }}

{{ var $op = $keyword == 'maxProperties' ? '>' : '<'; }}
if ({{# def.$dataNotType:'number' }} Object.keys({{=$data}}).length {{=$op}} {{=$schemaValue}}) {
{{ var $errorKeyword = $keyword; }}
Expand Down
7 changes: 7 additions & 0 deletions lib/dot/definitions.def
Expand Up @@ -138,6 +138,13 @@
#}}


{{## def.numberKeyword:
{{? !($isData || typeof $schema == 'number') }}
{{ throw new Error($keyword + ' must be number'); }}
{{?}}
#}}


{{## def.beginDefOut:
{{
var $$outStack = $$outStack || [];
Expand Down
44 changes: 44 additions & 0 deletions spec/ajv.spec.js
Expand Up @@ -512,5 +512,49 @@ describe('Ajv', function () {
});
});
});

describe('sub-schema validation outside of definitions during compilation', function() {
it('maximum', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {maximum: 'bar'}
});
});

it('exclusiveMaximum', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {exclusiveMaximum: 'bar'}
});
});

it('maxItems', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {maxItems: 'bar'}
});
});

it('maxLength', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {maxLength: 'bar'}
});
});

it('maxProperties', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {maxProperties: 'bar'}
});
});

function passValidationThrowCompile(schema) {
ajv.validateSchema(schema) .should.equal(true);
should.throw(function() {
ajv.compile(schema);
});
}
});
});
});

0 comments on commit 65b2f7d

Please sign in to comment.