rename option to strictDefaults

master
Evgeny Poberezkin 2019-03-03 09:16:00 +00:00
parent c081061a1e
commit 88199d569c
6 changed files with 29 additions and 26 deletions

View File

@ -798,14 +798,15 @@ console.log(validate(data)); // true
console.log(data); // [ 1, "foo" ] console.log(data); // [ 1, "foo" ]
``` ```
`default` keywords in other cases are invalid: `default` keywords in other cases are ignored:
- not in `properties` or `items` subschemas - not in `properties` or `items` subschemas
- in schemas inside `anyOf`, `oneOf` and `not` (see [#42](https://github.com/epoberezkin/ajv/issues/42)) - in schemas inside `anyOf`, `oneOf` and `not` (see [#42](https://github.com/epoberezkin/ajv/issues/42))
- in `if` subschema of `switch` keyword - in `if` subschema of `switch` keyword
- in schemas generated by custom macro keywords - in schemas generated by custom macro keywords
The [`invalidDefaults` option](#options) customizes Ajv's behavior for invalid defaults (`false` ignores invalid defaults, `true` raises an error, and `"log"` outputs a warning). 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 ## Coercing data types
@ -1071,7 +1072,7 @@ Defaults:
removeAdditional: false, removeAdditional: false,
useDefaults: false, useDefaults: false,
coerceTypes: false, coerceTypes: false,
invalidDefaults: false, strictDefaults: false,
// asynchronous validation options: // asynchronous validation options:
transpile: undefined, // requires ajv-async package transpile: undefined, // requires ajv-async package
// advanced options: // advanced options:
@ -1153,10 +1154,10 @@ Defaults:
- `false` (default) - no type coercion. - `false` (default) - no type coercion.
- `true` - coerce scalar data types. - `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). - `"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).
- _invalidDefaults_: specify behavior for invalid `default` keywords in schemas. Option values: - _strictDefaults_: specify behavior for ignored `default` keywords in schemas. Option values:
- `false` (default) - ignore invalid defaults - `false` (default) - ignored defaults are not reported
- `true` - if an invalid default is present, throw an error - `true` - if an ignored default is present, throw an error
- `"log"` - if an invalid default is present, log warning - `"log"` - if an ignored default is present, log warning
##### Asynchronous validation options ##### Asynchronous validation options

2
lib/ajv.d.ts vendored
View File

@ -180,7 +180,7 @@ declare namespace ajv {
removeAdditional?: boolean | 'all' | 'failing'; removeAdditional?: boolean | 'all' | 'failing';
useDefaults?: boolean | 'shared'; useDefaults?: boolean | 'shared';
coerceTypes?: boolean | 'array'; coerceTypes?: boolean | 'array';
invalidDefaults?: boolean | 'log'; strictDefaults?: boolean | 'log';
async?: boolean | string; async?: boolean | string;
transpile?: string | ((code: string) => string); transpile?: string | ((code: string) => string);
meta?: boolean | object; 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_SCHEMA_ID = 'http://json-schema.org/draft-07/schema';
var META_IGNORE_OPTIONS = [ 'removeAdditional', 'useDefaults', 'coerceTypes', 'invalidDefaults' ]; var META_IGNORE_OPTIONS = [ 'removeAdditional', 'useDefaults', 'coerceTypes', 'strictDefaults' ];
var META_SUPPORT_DATA = ['/properties']; var META_SUPPORT_DATA = ['/properties'];
/** /**

View File

@ -1,10 +1,11 @@
{{## def.assignDefault: {{## def.assignDefault:
{{? it.compositeRule }} {{? it.compositeRule }}
{{? it.opts.invalidDefaults }} {{? it.opts.strictDefaults }}
{{? it.opts.invalidDefaults === 'log' }} {{ var $defaultMsg = 'default is ignored for: ' + $passData; }}
{{ it.logger.warn('default is ignored for: ' + $passData); }} {{? it.opts.strictDefaults === 'log' }}
{{ it.logger.warn($defaultMsg); }}
{{??}} {{??}}
{{ throw new Error('default is ignored for: ' + $passData); }} {{ throw new Error($defaultMsg); }}
{{?}} {{?}}
{{?}} {{?}}
{{??}} {{??}}

View File

@ -72,11 +72,12 @@
it.dataPathArr = [undefined]; it.dataPathArr = [undefined];
}} }}
{{? it.opts.invalidDefaults && it.schema.default !== undefined }} {{? it.schema.default !== undefined && it.opts.useDefaults && it.opts.strictDefaults }}
{{? it.opts.invalidDefaults === 'log' }} {{ var $defaultMsg = 'default is ignored in the schema root'; }}
{{ it.logger.warn('default is ignored in the schema root'); }} {{? it.opts.strictDefaults === 'log' }}
{{ it.logger.warn($defaultMsg); }}
{{??}} {{??}}
{{ throw new Error('default is ignored in the schema root'); }} {{ throw new Error($defaultMsg); }}
{{?}} {{?}}
{{?}} {{?}}

View File

@ -221,9 +221,9 @@ describe('useDefaults options', function() {
}); });
}); });
describe('invalidDefaults option', function() { describe('strictDefaults option', function() {
it('should throw an error given an invalid default in the schema root when invalidDefaults is true', 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, invalidDefaults: true}); var ajv = new Ajv({useDefaults: true, strictDefaults: true});
var schema = { var schema = {
default: 5, default: 5,
properties: {} properties: {}
@ -231,8 +231,8 @@ describe('useDefaults options', function() {
should.throw(function() { ajv.compile(schema); }); should.throw(function() { ajv.compile(schema); });
}); });
it('should throw an error given an invalid default in oneOf when invalidDefaults is true', function() { it('should throw an error given an ignored default in oneOf when strictDefaults is true', function() {
var ajv = new Ajv({useDefaults: true, invalidDefaults: true}); var ajv = new Ajv({useDefaults: true, strictDefaults: true});
var schema = { var schema = {
oneOf: [ oneOf: [
{ enum: ['foo', 'bar'] }, { enum: ['foo', 'bar'] },
@ -248,11 +248,11 @@ describe('useDefaults options', function() {
should.throw(function() { ajv.compile(schema); }); should.throw(function() { ajv.compile(schema); });
}); });
it('should log a warning given an invalid default in the schema root when invalidDefaults is "log"', function() { it('should log a warning given an ignored default in the schema root when strictDefaults is "log"', function() {
var warnArg = null; var warnArg = null;
var ajv = new Ajv({ var ajv = new Ajv({
useDefaults: true, useDefaults: true,
invalidDefaults: 'log', strictDefaults: 'log',
logger: { logger: {
log: function() { log: function() {
throw new Error('should not be called'); throw new Error('should not be called');
@ -273,11 +273,11 @@ describe('useDefaults options', function() {
should.equal(warnArg, 'default is ignored in the schema root'); should.equal(warnArg, 'default is ignored in the schema root');
}); });
it('should log a warning given an invalid default in oneOf when invalidDefaults is "log"', function() { it('should log a warning given an ignored default in oneOf when strictDefaults is "log"', function() {
var warnArg = null; var warnArg = null;
var ajv = new Ajv({ var ajv = new Ajv({
useDefaults: true, useDefaults: true,
invalidDefaults: 'log', strictDefaults: 'log',
logger: { logger: {
log: function() { log: function() {
throw new Error('should not be called'); throw new Error('should not be called');