From fe53458a6eb5d37e870b1173614cac365564efdc Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 15 Aug 2015 23:59:14 +0100 Subject: [PATCH] missingRefs option tests --- lib/compile/_rules.js | 2 +- lib/dot/{$ref.jst => ref.jst} | 0 lib/dotjs/{$ref.js => ref.js} | 0 spec/options.spec.js | 39 +++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) rename lib/dot/{$ref.jst => ref.jst} (100%) rename lib/dotjs/{$ref.js => ref.js} (100%) diff --git a/lib/compile/_rules.js b/lib/compile/_rules.js index ca0991c..3bdd73c 100644 --- a/lib/compile/_rules.js +++ b/lib/compile/_rules.js @@ -2,7 +2,7 @@ //all requires must be explicit because browserify won't work with dynamic requires module.exports = { - '$ref': require('../dotjs/$ref'), + '$ref': require('../dotjs/ref'), anyOf: require('../dotjs/anyOf'), format: require('../dotjs/format'), maxLength: require('../dotjs/maxLength'), diff --git a/lib/dot/$ref.jst b/lib/dot/ref.jst similarity index 100% rename from lib/dot/$ref.jst rename to lib/dot/ref.jst diff --git a/lib/dotjs/$ref.js b/lib/dotjs/ref.js similarity index 100% rename from lib/dotjs/$ref.js rename to lib/dotjs/ref.js diff --git a/spec/options.spec.js b/spec/options.spec.js index 3d0b3ff..2d03ed5 100644 --- a/spec/options.spec.js +++ b/spec/options.spec.js @@ -155,4 +155,43 @@ describe('Ajv Options', function () { validate(123) .should.equal(true); }); }); + + + describe('missingRefs', function() { + it('should throw if ref is missing without this option', function() { + var ajv = Ajv(); + should.throw(function() { + ajv.compile({ $ref: 'missing_reference' }); + }); + }); + + it('should not throw and pass validation with missingRef == "ignore"', function() { + testMissingRefsIgnore(Ajv({ missingRefs: 'ignore' })); + testMissingRefsIgnore(Ajv({ missingRefs: 'ignore', allErrors: true })); + + function testMissingRefsIgnore(ajv) { + var validate = ajv.compile({ $ref: 'missing_reference' }); + validate({}) .should.equal(true); + } + }); + + it('should not throw and fail validation with missingRef == "fail" if the ref is used', function() { + testMissingRefsFail(Ajv({ missingRefs: 'fail' })); + testMissingRefsFail(Ajv({ missingRefs: 'fail', allErrors: true })); + + function testMissingRefsFail(ajv) { + var validate = ajv.compile({ + anyOf: [ + { type: 'number' }, + { $ref: 'missing_reference' } + ] + }); + validate(123) .should.equal(true); + validate('foo') .should.equal(false); + + validate = ajv.compile({ $ref: 'missing_reference' }); + validate({}) .should.equal(false); + } + }); + }); });