5.0.1-beta.0
Pre-releaseIf you are migrating from 4.x.x also see 5.0.0-beta.1 and Migration section below
Changes
Keyword changes
Support for all keywords included in draft 6 (it is not published yet and can change).
-
const (previously available as
constant
). To simplify migration you can define an aliasconstant
as a custom macro keyword:ajv.addKeyword('constant', { macro: x => ({ const: x }) });
-
contains. It is re-implemented as a standard keyword, if none of the items is valid, errors for all items will be reported now.
-
NEW propertyNames keyword added (previously it was a part of ajv-keywords package). It allows to validate all property names rather than values.
-
exclusiveMaximum/Minimum in draft 6 must be numbers, Ajv supports boolean form too for backwards compatibility.
Moved/deprecated:
- switch, patternRequired and formatMinimum/Maximum moved to ajv-keywords (^2.0.0-beta).
- patternGroups is deprecated, it can be still used with option
patternRequired
.
Draft 6 support
- Support for boolean schemas: wherever a schema is required,
true
/false
can be used in order to always pass/fail validation. - Meta-schema - by default Ajv adds backwards compatible draft 6 meta-schema. Its URI used in Ajv is
http://json-schema.org/draft-06/schema#
(it can change when draft 6 is published).
If your $schema keyword points to draft 4 or v5 meta-schema you need to add it manually:
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
// or
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-v5.json'));
Options
v5
is no longer used, for $data reference support use option$data
beautify: true
is no longer supported.processCode: function() {}
can be used to beautify/transpile generated code. See Options
Asynchronous validation
- Auto-detection of async mode and
transpile
option support require ajv-async package. - Default async mode is
"co*"
(co-wrapped generator functions). - If you need to transpile code without ajv-async package, you can pass transpilation function in
processCode
option. See Options. - In case of validation success, returned Promise resolves with validated data to simplify chaining (previously it resolved with
true
).
Related packages
Compatible versions are:
- ajv-keywords:
^2.0.0-beta
- ajv-merge-patch:
^3.0.0-beta
- ajv-i18n:
^2.0.0-beta
Migration from 4.x.x
If you use draft-04 schemas
var ajv = new Ajv({
meta: false, // optional, to prevent adding draft-06 meta-schema
extendRefs: true, // optional, current default is to 'fail', spec behaviour is to 'ignore'
unknownFormats: 'ignore', // optional, current default is true (fail)
// ...
});
var metaSchema = require('ajv/lib/refs/json-schema-draft-04.json');
ajv.addMetaSchema(metaSchema);
ajv._opts.defaultMeta = metaSchema.id;
// optional, using unversioned URI is out of spec, see https://github.com/json-schema-org/json-schema-spec/issues/216
ajv._refs['http://json-schema.org/schema'] = 'http://json-schema.org/draft-04/schema';
// Optionally you can also disable keywords defined in draft-06
ajv.removeKeyword('propertyNames');
ajv.removeKeyword('contains');
ajv.removeKeyword('const');
If you use schemas requiring v5 mode of Ajv
var ajv = new Ajv({
$data: true,
patternGroups: true,
meta: false, // optional, to prevent adding draft-06 meta-schema
extendRefs: true, // optional, current default is to 'fail', spec behaviour is to 'ignore'
unknownFormats: 'ignore', // optional, current default is true (fail)
// ...
});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
ajv._refs['http://json-schema.org/schema'] = 'http://json-schema.org/draft-04/schema'; // optional, using unversioned URI is out of spec
var metaSchema = require('ajv/lib/refs/json-schema-v5.json');
ajv.addMetaSchema(metaSchema);
ajv._opts.defaultMeta = metaSchema.id;
// optional - to avoid changing the schemas
ajv.addKeyword('constant', { macro: x => ({ const: x }) }); // this keyword is renamed to const in draft-06
// you need to use version "^2.0.0-beta" of ajv-keywords
require('ajv-keywords')(ajv, ['switch', 'patternRequired', 'formatMinimum', 'formatMaximum']);
// Optionally you can also disable propertyNames keyword defined in draft-06
ajv.removeKeyword('propertyNames');