feat: optionally throw exception during schema compilation if unknown format is used, #310

master
Evgeny Poberezkin 2016-10-16 11:57:49 +01:00
parent 0944949b08
commit 9bd850d115
2 changed files with 23 additions and 6 deletions

View File

@ -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)

View File

@ -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) {