From db56b51fff7834df60925c8166bddf6dab4a27c8 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 3 Mar 2018 13:41:50 +0000 Subject: [PATCH] fix: error messages for exclusiveMaximum/Minimum wint $data, closes #722 --- lib/dot/_limit.jst | 8 ++++++++ spec/errors.spec.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/dot/_limit.jst b/lib/dot/_limit.jst index 13e7649..e10806f 100644 --- a/lib/dot/_limit.jst +++ b/lib/dot/_limit.jst @@ -50,6 +50,14 @@ ) || {{=$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' diff --git a/spec/errors.spec.js b/spec/errors.spec.js index e0b5783..6291b15 100644 --- a/spec/errors.spec.js +++ b/spec/errors.spec.js @@ -711,6 +711,42 @@ describe('Validation errors', function () { } }); }); + + it('should include limits in error message with $data', function() { + var schema = { + "properties": { + "smaller": { + "type": "number", + "exclusiveMaximum": { "$data": "1/larger" } + }, + "larger": { "type": "number" } + } + }; + + ajv = new Ajv({$data: true}); + fullAjv = new Ajv({$data: true, allErrors: true, verbose: true, jsonPointers: true}); + + [ajv, fullAjv].forEach(function (_ajv) { + var validate = _ajv.compile(schema); + shouldBeValid(validate, {smaller: 2, larger: 4}); + shouldBeValid(validate, {smaller: 3, larger: 4}); + + shouldBeInvalid(validate, {smaller: 4, larger: 4}); + testError(); + + shouldBeInvalid(validate, {smaller: 5, larger: 4}); + testError(); + + function testError() { + var err = validate.errors[0]; + shouldBeError(err, 'exclusiveMaximum', + '#/properties/smaller/exclusiveMaximum', + _ajv._opts.jsonPointers ? '/smaller' : '.smaller', + 'should be < 4', + {comparison: '<', limit: 4, exclusive: true}); + } + }); + }); });