From 9bd850d11566d98d2e5c1c2dea4ed7bcc5582d65 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sun, 16 Oct 2016 11:57:49 +0100 Subject: [PATCH] feat: optionally throw exception during schema compilation if unknown format is used, #310 --- lib/dot/format.jst | 21 +++++++++++++++++++-- spec/options.spec.js | 8 ++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/dot/format.jst b/lib/dot/format.jst index 59d63a2..cb97b68 100644 --- a/lib/dot/format.jst +++ b/lib/dot/format.jst @@ -49,8 +49,25 @@ } if ({{# def.$dataCheckFormat }}) { {{??}} - {{ var $format = it.formats[$schema]; }} - {{? !$format }}{{# def.skipFormat }}{{?}} + {{ + var $format = it.formats[$schema] + , $unknownFormats = it.opts.unknownFormats + , $allowUnknown = Array.isArray($unknownFormats); + }} + {{? !$format }} + {{? $unknownFormats === true || ($allowUnknown && $unknownFormats.indexOf($schema) == -1) }} + {{ throw new Error('unknown format "' + $schema + '" is used in schema at path "' + it.errSchemaPath + '"'); }} + {{??}} + {{ + if (!$allowUnknown) { + console.warn('unknown format "' + $schema + '" ignored in schema at path "' + it.errSchemaPath + '"'); + if ($unknownFormats !== 'ignore') + console.warn('In the next major version it will throw exception. See option unknownFormats for more information'); + } + }} + {{# def.skipFormat }} + {{?}} + {{?}} {{ var $isObject = typeof $format == 'object' && !($format instanceof RegExp) diff --git a/spec/options.spec.js b/spec/options.spec.js index 070f8f0..617d9e4 100644 --- a/spec/options.spec.js +++ b/spec/options.spec.js @@ -870,7 +870,7 @@ describe('Ajv Options', function () { describe('unknownFormats', function() { - describe.skip('= true (will be default in 5.0.0)', function() { + describe('= true (will be default in 5.0.0)', function() { it('should fail schema compilation if unknown format is used', function() { test(new Ajv({unknownFormats: true})); @@ -881,7 +881,7 @@ describe('Ajv Options', function () { } }); - it('should throw during validation if unknown format is used via $data', function() { + it.skip('should throw during validation if unknown format is used via $data', function() { test(new Ajv({v5: true, unknownFormats: true})); function test(ajv) { @@ -935,7 +935,7 @@ describe('Ajv Options', function () { }); }); - describe.skip('= [String]', function() { + describe('= [String]', function() { it('should pass schema compilation and be valid if whitelisted unknown format is used', function() { test(new Ajv({unknownFormats: ['allowed']})); @@ -949,7 +949,7 @@ describe('Ajv Options', function () { } }); - it('should be valid if whitelisted unknown format is used via $data', function() { + it.skip('should be valid if whitelisted unknown format is used via $data', function() { test(new Ajv({v5: true, unknownFormats: ['allowed']})); function test(ajv) {