Merge branch 'not-an-aardvark-invalidDefaults-option'

master
Evgeny Poberezkin 2019-03-03 09:16:17 +00:00
commit 4b76519deb
6 changed files with 126 additions and 13 deletions

View File

@ -805,6 +805,8 @@ console.log(data); // [ 1, "foo" ]
- in `if` subschema of `switch` keyword
- in schemas generated by custom macro keywords
The [`strictDefaults` option](#options) customizes Ajv's behavior for the defaults that Ajv ignores (`true` raises an error, and `"log"` outputs a warning).
## Coercing data types
@ -1070,6 +1072,7 @@ Defaults:
removeAdditional: false,
useDefaults: false,
coerceTypes: false,
strictDefaults: false,
// asynchronous validation options:
transpile: undefined, // requires ajv-async package
// advanced options:
@ -1151,6 +1154,10 @@ Defaults:
- `false` (default) - no type coercion.
- `true` - coerce scalar data types.
- `"array"` - in addition to coercions between scalar types, coerce scalar data to an array with one element and vice versa (as required by the schema).
- _strictDefaults_: specify behavior for ignored `default` keywords in schemas. Option values:
- `false` (default) - ignored defaults are not reported
- `true` - if an ignored default is present, throw an error
- `"log"` - if an ignored default is present, log warning
##### Asynchronous validation options

1
lib/ajv.d.ts vendored
View File

@ -180,6 +180,7 @@ declare namespace ajv {
removeAdditional?: boolean | 'all' | 'failing';
useDefaults?: boolean | 'shared';
coerceTypes?: boolean | 'array';
strictDefaults?: boolean | 'log';
async?: boolean | string;
transpile?: string | ((code: string) => string);
meta?: boolean | object;

View File

@ -39,7 +39,7 @@ Ajv.$dataMetaSchema = $dataMetaSchema;
var META_SCHEMA_ID = 'http://json-schema.org/draft-07/schema';
var META_IGNORE_OPTIONS = [ 'removeAdditional', 'useDefaults', 'coerceTypes' ];
var META_IGNORE_OPTIONS = [ 'removeAdditional', 'useDefaults', 'coerceTypes', 'strictDefaults' ];
var META_SUPPORT_DATA = ['/properties'];
/**

View File

@ -1,15 +1,26 @@
{{## def.assignDefault:
if ({{=$passData}} === undefined
{{? it.opts.useDefaults == 'empty' }}
|| {{=$passData}} === null
|| {{=$passData}} === ''
{{? it.compositeRule }}
{{? it.opts.strictDefaults }}
{{ var $defaultMsg = 'default is ignored for: ' + $passData; }}
{{? it.opts.strictDefaults === 'log' }}
{{ it.logger.warn($defaultMsg); }}
{{??}}
{{ throw new Error($defaultMsg); }}
{{?}}
{{?}}
)
{{=$passData}} = {{? it.opts.useDefaults == 'shared' }}
{{= it.useDefault($sch.default) }}
{{??}}
{{= JSON.stringify($sch.default) }}
{{?}};
{{??}}
if ({{=$passData}} === undefined
{{? it.opts.useDefaults == 'empty' }}
|| {{=$passData}} === null
|| {{=$passData}} === ''
{{?}}
)
{{=$passData}} = {{? it.opts.useDefaults == 'shared' }}
{{= it.useDefault($sch.default) }}
{{??}}
{{= JSON.stringify($sch.default) }}
{{?}};
{{?}}
#}}

View File

@ -72,6 +72,14 @@
it.dataPathArr = [undefined];
}}
{{? it.schema.default !== undefined && it.opts.useDefaults && it.opts.strictDefaults }}
{{ var $defaultMsg = 'default is ignored in the schema root'; }}
{{? it.opts.strictDefaults === 'log' }}
{{ it.logger.warn($defaultMsg); }}
{{??}}
{{ throw new Error($defaultMsg); }}
{{?}}
{{?}}
var vErrors = null; {{ /* don't edit, used in replace */ }}
var errors = 0; {{ /* don't edit, used in replace */ }}
@ -177,7 +185,7 @@
{{? $rulesGroup.type }}
if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
{{?}}
{{? it.opts.useDefaults && !it.compositeRule }}
{{? it.opts.useDefaults }}
{{? $rulesGroup.type == 'object' && it.schema.properties }}
{{# def.defaultProperties }}
{{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}

View File

@ -2,7 +2,7 @@
var Ajv = require('../ajv');
var getAjvInstances = require('../ajv_instances');
require('../chai').should();
var should = require('../chai').should();
describe('useDefaults options', function() {
@ -220,4 +220,90 @@ describe('useDefaults options', function() {
});
});
});
describe('strictDefaults option', function() {
it('should throw an error given an ignored default in the schema root when strictDefaults is true', function() {
var ajv = new Ajv({useDefaults: true, strictDefaults: true});
var schema = {
default: 5,
properties: {}
};
should.throw(function() { ajv.compile(schema); });
});
it('should throw an error given an ignored default in oneOf when strictDefaults is true', function() {
var ajv = new Ajv({useDefaults: true, strictDefaults: true});
var schema = {
oneOf: [
{ enum: ['foo', 'bar'] },
{
properties: {
foo: {
default: true
}
}
}
]
};
should.throw(function() { ajv.compile(schema); });
});
it('should log a warning given an ignored default in the schema root when strictDefaults is "log"', function() {
var warnArg = null;
var ajv = new Ajv({
useDefaults: true,
strictDefaults: 'log',
logger: {
log: function() {
throw new Error('should not be called');
},
warn: function(warning) {
warnArg = warning;
},
error: function() {
throw new Error('should not be called');
}
}
});
var schema = {
default: 5,
properties: {}
};
ajv.compile(schema);
should.equal(warnArg, 'default is ignored in the schema root');
});
it('should log a warning given an ignored default in oneOf when strictDefaults is "log"', function() {
var warnArg = null;
var ajv = new Ajv({
useDefaults: true,
strictDefaults: 'log',
logger: {
log: function() {
throw new Error('should not be called');
},
warn: function(warning) {
warnArg = warning;
},
error: function() {
throw new Error('should not be called');
}
}
});
var schema = {
oneOf: [
{ enum: ['foo', 'bar'] },
{
properties: {
foo: {
default: true
}
}
}
]
};
ajv.compile(schema);
should.equal(warnArg, 'default is ignored for: data.foo');
});
});
});