Skip to content

Commit

Permalink
feat: remove support for booleam exclusiveMaximum/Minimum (draft-04)
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Jul 22, 2020
1 parent 166d2df commit 197a9e8
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 280 deletions.
12 changes: 4 additions & 8 deletions KEYWORDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ Most other keywords apply only to a particular type of data. If the data is of d

The value of keyword `maximum` (`minimum`) should be a number. This value is the maximum (minimum) allowed value for the data to be valid.

Draft-04: The value of keyword `exclusiveMaximum` (`exclusiveMinimum`) should be a boolean value. These keyword cannot be used without `maximum` (`minimum`). If this keyword value is equal to `true`, the data should not be equal to the value in `maximum` (`minimum`) keyword to be valid.
The value of keyword `exclusiveMaximum` (`exclusiveMinimum`) should be a number. This value is the exclusive maximum (minimum) allowed value for the data to be valid (the data equal to this keyword value is invalid).

Draft-06/07: The value of keyword `exclusiveMaximum` (`exclusiveMinimum`) should be a number. This value is the exclusive maximum (minimum) allowed value for the data to be valid (the data equal to this keyword value is invalid).

Ajv supports both draft-04 and draft-06/07 syntaxes.
**Please note**: Boolean value for keywords `exclusiveMaximum` (`exclusiveMinimum`) is no longer supported.

**Examples**

Expand All @@ -90,15 +88,13 @@ Ajv supports both draft-04 and draft-06/07 syntaxes.

_invalid_: `6`, `7`

2) _schema_: `{ "minimum": 5 }`
2. _schema_: `{ "minimum": 5 }`

_valid_: `5`, `6`, any non-number (`"abc"`, `[]`, `{}`, `null`, `true`)

_invalid_: `4`, `4.5`

3. _schema_:
draft-04: `{ "minimum": 5, "exclusiveMinimum": true }`
draft-06/07: `{ "exclusiveMinimum": 5 }`
3. _schema_: `{ "exclusiveMinimum": 5 }`

_valid_: `6`, `7`, any non-number (`"abc"`, `[]`, `{}`, `null`, `true`)

Expand Down
6 changes: 4 additions & 2 deletions lib/compile/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ module.exports = function rules() {
{
type: "number",
rules: [
{maximum: ["exclusiveMaximum"]},
{minimum: ["exclusiveMinimum"]},
"maximum",
"minimum",
"exclusiveMaximum",
"exclusiveMinimum",
"multipleOf",
"format",
],
Expand Down
115 changes: 12 additions & 103 deletions lib/dot/_limit.jst
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,20 @@
{{# def.setupKeyword }}
{{# def.$data }}

{{## def.setExclusiveLimit:
$exclusive = true;
$errorKeyword = $exclusiveKeyword;
$errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;
#}}
{{# def.numberKeyword }}

{{
var $isMax = $keyword == 'maximum'
, $exclusiveKeyword = $isMax ? 'exclusiveMaximum' : 'exclusiveMinimum'
, $schemaExcl = it.schema[$exclusiveKeyword]
, $isDataExcl = it.opts.$data && $schemaExcl && $schemaExcl.$data
, $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');
var $op /* used in errors */, $notOp;
switch ($keyword) {
case 'maximum': $op = '<='; $notOp = '>'; break;
case 'minimum': $op = '>='; $notOp = '<'; break;
case 'exclusiveMaximum': $op = '<'; $notOp = '>='; break;
case 'exclusiveMinimum': $op = '>'; $notOp = '<='; break;
default: throw Error('not _limit keyword ' + $keyword);
}
}}

{{? $isDataExcl }}
{{
var $schemaValueExcl = it.util.getData($schemaExcl.$data, $dataLvl, it.dataPathArr)
, $exclusive = 'exclusive' + $lvl
, $exclType = 'exclType' + $lvl
, $exclIsNumber = 'exclIsNumber' + $lvl
, $opExpr = 'op' + $lvl
, $opStr = '\' + ' + $opExpr + ' + \'';
}}
var schemaExcl{{=$lvl}} = {{=$schemaValueExcl}};
{{ $schemaValueExcl = 'schemaExcl' + $lvl; }}

var {{=$exclusive}};
var {{=$exclType}} = typeof {{=$schemaValueExcl}};
if ({{=$exclType}} != 'boolean' && {{=$exclType}} != 'undefined' && {{=$exclType}} != 'number') {
{{ var $errorKeyword = $exclusiveKeyword; }}
{{# def.error:'_exclusiveLimit' }}
} else if ({{# def.$dataNotType:'number' }}
{{=$exclType}} == 'number'
? (
({{=$exclusive}} = {{=$schemaValue}} === undefined || {{=$schemaValueExcl}} {{=$op}}= {{=$schemaValue}})
? {{=$data}} {{=$notOp}}= {{=$schemaValueExcl}}
: {{=$data}} {{=$notOp}} {{=$schemaValue}}
)
: (
({{=$exclusive}} = {{=$schemaValueExcl}} === true)
? {{=$data}} {{=$notOp}}= {{=$schemaValue}}
: {{=$data}} {{=$notOp}} {{=$schemaValue}}
)
|| {{=$data}} !== {{=$data}}) {
var op{{=$lvl}} = {{=$exclusive}} ? '{{=$op}}' : '{{=$op}}=';
{{
if ($schema === undefined) {
$errorKeyword = $exclusiveKeyword;
$errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;
$schemaValue = $schemaValueExcl;
$isData = $isDataExcl;
}
}}
{{??}}
{{
var $exclIsNumber = typeof $schemaExcl == 'number'
, $opStr = $op; /*used in error*/
}}

{{? $exclIsNumber && $isData }}
{{ var $opExpr = '\'' + $opStr + '\''; /*used in error*/ }}
if ({{# def.$dataNotType:'number' }}
( {{=$schemaValue}} === undefined
|| {{=$schemaExcl}} {{=$op}}= {{=$schemaValue}}
? {{=$data}} {{=$notOp}}= {{=$schemaExcl}}
: {{=$data}} {{=$notOp}} {{=$schemaValue}} )
|| {{=$data}} !== {{=$data}}) {
{{??}}
{{
if ($exclIsNumber && $schema === undefined) {
{{# def.setExclusiveLimit }}
$schemaValue = $schemaExcl;
$notOp += '=';
} else {
if ($exclIsNumber)
$schemaValue = Math[$isMax ? 'min' : 'max']($schemaExcl, $schema);

if ($schemaExcl === ($exclIsNumber ? $schemaValue : true)) {
{{# def.setExclusiveLimit }}
$notOp += '=';
} else {
$exclusive = false;
$opStr += '=';
}
}

var $opExpr = '\'' + $opStr + '\''; /*used in error*/
}}

if ({{# def.$dataNotType:'number' }}
{{=$data}} {{=$notOp}} {{=$schemaValue}}
|| {{=$data}} !== {{=$data}}) {
{{?}}
{{?}}
{{ $errorKeyword = $errorKeyword || $keyword; }}
{{# def.error:'_limit' }}
} {{? $breakOnError }} else { {{?}}
if ({{# def.$dataNotType:'number' }} {{=$data}} {{=$notOp}} {{=$schemaValue}} || {{=$data}} !== {{=$data}}) {
{{ var $errorKeyword = $keyword; }}
{{# def.error:'_limit' }}
} {{? $breakOnError }} else { {{?}}
8 changes: 3 additions & 5 deletions lib/dot/errors.def
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
'enum': "'should be equal to one of the allowed values'",
format: "'should match format \"{{#def.concatSchemaEQ}}\"'",
'if': "'should match \"' + {{=$ifClause}} + '\" schema'",
_limit: "'should be {{=$opStr}} {{#def.appendSchema}}",
_exclusiveLimit: "'{{=$exclusiveKeyword}} should be boolean'",
_limit: "'should be {{=$op}} {{#def.appendSchema}}",
_exclusiveLimit: "'should be {{=$op}} {{#def.appendSchema}}",
_limitItems: "'should NOT have {{?$keyword=='maxItems'}}more{{??}}fewer{{?}} than {{#def.concatSchema}} items'",
_limitLength: "'should NOT be {{?$keyword=='maxLength'}}longer{{??}}shorter{{?}} than {{#def.concatSchema}} characters'",
_limitProperties:"'should NOT have {{?$keyword=='maxProperties'}}more{{??}}fewer{{?}} than {{#def.concatSchema}} properties'",
Expand Down Expand Up @@ -139,7 +139,6 @@
format: "{{#def.schemaRefOrQS}}",
'if': "validate.schema{{=$schemaPath}}",
_limit: "{{#def.schemaRefOrVal}}",
_exclusiveLimit: "validate.schema{{=$schemaPath}}",
_limitItems: "{{#def.schemaRefOrVal}}",
_limitLength: "{{#def.schemaRefOrVal}}",
_limitProperties:"{{#def.schemaRefOrVal}}",
Expand Down Expand Up @@ -173,8 +172,7 @@
'enum': "{ allowedValues: schema{{=$lvl}} }",
format: "{ format: {{#def.schemaValueQS}} }",
'if': "{ failingKeyword: {{=$ifClause}} }",
_limit: "{ comparison: {{=$opExpr}}, limit: {{=$schemaValue}}, exclusive: {{=$exclusive}} }",
_exclusiveLimit: "{}",
_limit: "{ comparison: '{{=$op}}', limit: {{=$schemaValue}} }",
_limitItems: "{ limit: {{=$schemaValue}} }",
_limitLength: "{ limit: {{=$schemaValue}} }",
_limitProperties:"{ limit: {{=$schemaValue}} }",
Expand Down
2 changes: 2 additions & 0 deletions lib/dotjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module.exports = {
contains: require("./contains"),
dependencies: require("./dependencies"),
enum: require("./enum"),
exclusiveMaximum: require("./_limit"),
exclusiveMinimum: require("./_limit"),
format: require("./format"),
if: require("./if"),
items: require("./items"),
Expand Down
4 changes: 1 addition & 3 deletions spec/errors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,14 +984,12 @@ describe("Validation errors", function () {
testError("exclusiveMinimum", "should be > 2", {
comparison: ">",
limit: 2,
exclusive: true,
})

shouldBeInvalid(validate, 5)
testError("exclusiveMaximum", "should be < 5", {
comparison: "<",
limit: 5,
exclusive: true,
})

function testError(keyword, message, params) {
Expand Down Expand Up @@ -1038,7 +1036,7 @@ describe("Validation errors", function () {
"#/properties/smaller/exclusiveMaximum",
_ajv._opts.jsonPointers ? "/smaller" : ".smaller",
"should be < 4",
{comparison: "<", limit: 4, exclusive: true}
{comparison: "<", limit: 4}
)
}
})
Expand Down
Loading

0 comments on commit 197a9e8

Please sign in to comment.