Skip to content

Commit

Permalink
feat: metaSchema and dependencies for formatExclusiveMaximum/Minimum, c…
Browse files Browse the repository at this point in the history
…lose #63
  • Loading branch information
epoberezkin committed Feb 9, 2019
1 parent bdcf858 commit f261c0c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
42 changes: 26 additions & 16 deletions keywords/_formatLimit.js
Expand Up @@ -9,6 +9,21 @@ var COMPARE_FORMATS = {
'date-time': compareDateTime
};

var $dataMetaSchema = {
type: 'object',
required: [ '$data' ],
properties: {
$data: {
type: 'string',
anyOf: [
{ format: 'relative-json-pointer' },
{ format: 'json-pointer' }
]
}
},
additionalProperties: false
};

module.exports = function (minMax) {
var keyword = 'format' + minMax;
return function defFunc(ajv) {
Expand All @@ -20,27 +35,22 @@ module.exports = function (minMax) {
dependencies: ['format'],
metaSchema: {
anyOf: [
{ type: 'string' },
{
type: 'object',
required: [ '$data' ],
properties: {
$data: {
type: 'string',
anyOf: [
{ format: 'relative-json-pointer' },
{ format: 'json-pointer' }
]
}
},
additionalProperties: false
}
{type: 'string'},
$dataMetaSchema
]
}
};

ajv.addKeyword(keyword, defFunc.definition);
ajv.addKeyword('formatExclusive' + minMax);
ajv.addKeyword('formatExclusive' + minMax, {
dependencies: ['format' + minMax],
metaSchema: {
anyOf: [
{type: 'boolean'},
$dataMetaSchema
]
}
});
extendFormats(ajv);
return ajv;
};
Expand Down
16 changes: 16 additions & 0 deletions spec/formatLimit.spec.js
Expand Up @@ -33,6 +33,22 @@ describe('keywords "formatMinimum" and "formatMaximum"', function() {
});
});

ajvs.forEach(function (ajv, i) {
it('formatExclusiveMaximum should throw if not boolean #' + i, function() {
should.throw(function() {
ajv.compile({ formatMaximum: '2015-08-01', formatExclusiveMaximum: 1 });
});
});
});

ajvs.forEach(function (ajv, i) {
it('formatExclusiveMaximum should throw when "formatMaximum" is absent #' + i, function() {
should.throw(function() {
ajv.compile({ formatExclusiveMaximum: true });
});
});
});

function getAjv(format) {
return new Ajv({ allErrors: true, format: format });
}
Expand Down

0 comments on commit f261c0c

Please sign in to comment.