Skip to content

Commit 65b2f7d

Browse files
committed
validate numbers in schemas during schema compilation
1 parent 24d4f8f commit 65b2f7d

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

lib/dot/_limit.jst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
, $op = $isMax ? '<' : '>'
1818
, $notOp = $isMax ? '>' : '<'
1919
, $errorKeyword = undefined;
20+
21+
if (!($isData || typeof $schema == 'number' || $schema === undefined)) {
22+
throw new Error($keyword + ' must be number');
23+
}
24+
if (!($isDataExcl || $schemaExcl === undefined
25+
|| typeof $schemaExcl == 'number'
26+
|| typeof $schemaExcl == 'boolean')) {
27+
throw new Error($exclusiveKeyword + ' must be number or boolean');
28+
}
2029
}}
2130

2231
{{? $isDataExcl }}

lib/dot/_limitItems.jst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{{# def.setupKeyword }}
44
{{# def.$data }}
55

6+
{{# def.numberKeyword }}
7+
68
{{ var $op = $keyword == 'maxItems' ? '>' : '<'; }}
79
if ({{# def.$dataNotType:'number' }} {{=$data}}.length {{=$op}} {{=$schemaValue}}) {
810
{{ var $errorKeyword = $keyword; }}

lib/dot/_limitLength.jst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{{# def.setupKeyword }}
44
{{# def.$data }}
55

6+
{{# def.numberKeyword }}
7+
68
{{ var $op = $keyword == 'maxLength' ? '>' : '<'; }}
79
if ({{# def.$dataNotType:'number' }} {{# def.strLength }} {{=$op}} {{=$schemaValue}}) {
810
{{ var $errorKeyword = $keyword; }}

lib/dot/_limitProperties.jst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{{# def.setupKeyword }}
44
{{# def.$data }}
55

6+
{{# def.numberKeyword }}
7+
68
{{ var $op = $keyword == 'maxProperties' ? '>' : '<'; }}
79
if ({{# def.$dataNotType:'number' }} Object.keys({{=$data}}).length {{=$op}} {{=$schemaValue}}) {
810
{{ var $errorKeyword = $keyword; }}

lib/dot/definitions.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@
138138
#}}
139139

140140

141+
{{## def.numberKeyword:
142+
{{? !($isData || typeof $schema == 'number') }}
143+
{{ throw new Error($keyword + ' must be number'); }}
144+
{{?}}
145+
#}}
146+
147+
141148
{{## def.beginDefOut:
142149
{{
143150
var $$outStack = $$outStack || [];

spec/ajv.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,5 +512,49 @@ describe('Ajv', function () {
512512
});
513513
});
514514
});
515+
516+
describe('sub-schema validation outside of definitions during compilation', function() {
517+
it('maximum', function() {
518+
passValidationThrowCompile({
519+
$ref: '#/foo',
520+
foo: {maximum: 'bar'}
521+
});
522+
});
523+
524+
it('exclusiveMaximum', function() {
525+
passValidationThrowCompile({
526+
$ref: '#/foo',
527+
foo: {exclusiveMaximum: 'bar'}
528+
});
529+
});
530+
531+
it('maxItems', function() {
532+
passValidationThrowCompile({
533+
$ref: '#/foo',
534+
foo: {maxItems: 'bar'}
535+
});
536+
});
537+
538+
it('maxLength', function() {
539+
passValidationThrowCompile({
540+
$ref: '#/foo',
541+
foo: {maxLength: 'bar'}
542+
});
543+
});
544+
545+
it('maxProperties', function() {
546+
passValidationThrowCompile({
547+
$ref: '#/foo',
548+
foo: {maxProperties: 'bar'}
549+
});
550+
});
551+
552+
function passValidationThrowCompile(schema) {
553+
ajv.validateSchema(schema) .should.equal(true);
554+
should.throw(function() {
555+
ajv.compile(schema);
556+
});
557+
}
558+
});
515559
});
516560
});

0 commit comments

Comments
 (0)