1
|
'use strict';
|
2
|
|
3
|
module.exports = function defFunc(ajv) {
|
4
|
defFunc.definition = {
|
5
|
type: 'number',
|
6
|
macro: function (schema, parentSchema) {
|
7
|
var min = schema[0]
|
8
|
, max = schema[1]
|
9
|
, exclusive = parentSchema.exclusiveRange;
|
10
|
|
11
|
validateRangeSchema(min, max, exclusive);
|
12
|
|
13
|
return exclusive === true
|
14
|
? {exclusiveMinimum: min, exclusiveMaximum: max}
|
15
|
: {minimum: min, maximum: max};
|
16
|
},
|
17
|
metaSchema: {
|
18
|
type: 'array',
|
19
|
minItems: 2,
|
20
|
maxItems: 2,
|
21
|
items: { type: 'number' }
|
22
|
}
|
23
|
};
|
24
|
|
25
|
ajv.addKeyword('range', defFunc.definition);
|
26
|
ajv.addKeyword('exclusiveRange');
|
27
|
return ajv;
|
28
|
|
29
|
function validateRangeSchema(min, max, exclusive) {
|
30
|
if (exclusive !== undefined && typeof exclusive != 'boolean')
|
31
|
throw new Error('Invalid schema for exclusiveRange keyword, should be boolean');
|
32
|
|
33
|
if (min > max || (exclusive && min == max))
|
34
|
throw new Error('There are no numbers in range');
|
35
|
}
|
36
|
};
|