use new with Ajv constructor in tests and examples
parent
048f2139d4
commit
6a14e347c4
26
README.md
26
README.md
|
@ -1,8 +1,6 @@
|
||||||
# Ajv: Another JSON Schema Validator
|
# Ajv: Another JSON Schema Validator
|
||||||
|
|
||||||
The fastest JSON Schema validator for node.js and browser.
|
The fastest JSON Schema validator for node.js and browser. Supports [v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals).
|
||||||
|
|
||||||
Supports [v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals).
|
|
||||||
|
|
||||||
|
|
||||||
[![Build Status](https://travis-ci.org/epoberezkin/ajv.svg?branch=master)](https://travis-ci.org/epoberezkin/ajv)
|
[![Build Status](https://travis-ci.org/epoberezkin/ajv.svg?branch=master)](https://travis-ci.org/epoberezkin/ajv)
|
||||||
|
@ -101,7 +99,7 @@ The fastest validation call:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var Ajv = require('ajv');
|
var Ajv = require('ajv');
|
||||||
var ajv = Ajv(); // options can be passed, e.g. {allErrors: true}
|
var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
|
||||||
var validate = ajv.compile(schema);
|
var validate = ajv.compile(schema);
|
||||||
var valid = validate(data);
|
var valid = validate(data);
|
||||||
if (!valid) console.log(validate.errors);
|
if (!valid) console.log(validate.errors);
|
||||||
|
@ -316,7 +314,7 @@ During asynchronous compilation remote references are loaded using supplied func
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var ajv = Ajv({ loadSchema: loadSchema });
|
var ajv = new Ajv({ loadSchema: loadSchema });
|
||||||
|
|
||||||
ajv.compileAsync(schema, function (err, validate) {
|
ajv.compileAsync(schema, function (err, validate) {
|
||||||
if (err) return;
|
if (err) return;
|
||||||
|
@ -368,7 +366,7 @@ Example:
|
||||||
* 3. es7 async functions transpiled with regenerator
|
* 3. es7 async functions transpiled with regenerator
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var ajv = Ajv();
|
var ajv = new Ajv();
|
||||||
|
|
||||||
ajv.addKeyword('idExists', {
|
ajv.addKeyword('idExists', {
|
||||||
async: true,
|
async: true,
|
||||||
|
@ -425,7 +423,7 @@ Ajv npm package includes minified browser bundles of regenerator and nodent in d
|
||||||
#### Using nodent
|
#### Using nodent
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var ajv = Ajv({ /* async: 'es7', */ transpile: 'nodent' });
|
var ajv = new Ajv({ /* async: 'es7', */ transpile: 'nodent' });
|
||||||
var validate = ajv.compile(schema); // transpiled es7 async function
|
var validate = ajv.compile(schema); // transpiled es7 async function
|
||||||
validate(data).then(successFunc).catch(errorFunc);
|
validate(data).then(successFunc).catch(errorFunc);
|
||||||
```
|
```
|
||||||
|
@ -436,7 +434,7 @@ validate(data).then(successFunc).catch(errorFunc);
|
||||||
#### Using regenerator
|
#### Using regenerator
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var ajv = Ajv({ /* async: 'es7', */ transpile: 'regenerator' });
|
var ajv = new Ajv({ /* async: 'es7', */ transpile: 'regenerator' });
|
||||||
var validate = ajv.compile(schema); // transpiled es7 async function
|
var validate = ajv.compile(schema); // transpiled es7 async function
|
||||||
validate(data).then(successFunc).catch(errorFunc);
|
validate(data).then(successFunc).catch(errorFunc);
|
||||||
```
|
```
|
||||||
|
@ -447,7 +445,7 @@ validate(data).then(successFunc).catch(errorFunc);
|
||||||
#### Using other transpilers
|
#### Using other transpilers
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var ajv = Ajv({ async: 'es7', transpile: transpileFunc });
|
var ajv = new Ajv({ async: 'es7', transpile: transpileFunc });
|
||||||
var validate = ajv.compile(schema); // transpiled es7 async function
|
var validate = ajv.compile(schema); // transpiled es7 async function
|
||||||
validate(data).then(successFunc).catch(errorFunc);
|
validate(data).then(successFunc).catch(errorFunc);
|
||||||
```
|
```
|
||||||
|
@ -485,7 +483,7 @@ This option modifies original data.
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var ajv = Ajv({ removeAdditional: true });
|
var ajv = new Ajv({ removeAdditional: true });
|
||||||
var schema = {
|
var schema = {
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
"properties": {
|
"properties": {
|
||||||
|
@ -582,7 +580,7 @@ Inserting defaults by reference can be faster (in case you have an object in `de
|
||||||
Example 1 (`default` in `properties`):
|
Example 1 (`default` in `properties`):
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var ajv = Ajv({ useDefaults: true });
|
var ajv = new Ajv({ useDefaults: true });
|
||||||
var schema = {
|
var schema = {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
@ -622,7 +620,7 @@ console.log(data); // [ 1, "foo" ]
|
||||||
Example 3 (inserting "defaults" by reference):
|
Example 3 (inserting "defaults" by reference):
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var ajv = Ajv({ useDefaults: 'shared' });
|
var ajv = new Ajv({ useDefaults: 'shared' });
|
||||||
|
|
||||||
var schema = {
|
var schema = {
|
||||||
properties: {
|
properties: {
|
||||||
|
@ -665,7 +663,7 @@ __Please note__: if you pass a scalar value to the validating function its type
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var ajv = Ajv({ coerceTypes: true });
|
var ajv = new Ajv({ coerceTypes: true });
|
||||||
var schema = {
|
var schema = {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
@ -690,7 +688,7 @@ See [Coercion rules](https://github.com/epoberezkin/ajv/blob/master/COERCION.md)
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
##### Ajv(Object options) -> Object
|
##### new Ajv(Object options) -> Object
|
||||||
|
|
||||||
Create Ajv instance.
|
Create Ajv instance.
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ describe('Ajv', function () {
|
||||||
var ajv;
|
var ajv;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ajv = Ajv();
|
ajv = new Ajv();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create instance', function() {
|
it('should create instance', function() {
|
||||||
|
|
|
@ -98,5 +98,5 @@ function getAjvInstances(opts) {
|
||||||
|
|
||||||
|
|
||||||
function getAjv(opts){
|
function getAjv(opts){
|
||||||
try { return Ajv(opts); } catch(e) {}
|
try { return new Ajv(opts); } catch(e) {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ function _getAjvInstances(opts, useOpts) {
|
||||||
var instances = _getAjvInstances(opts, useOpts)
|
var instances = _getAjvInstances(opts, useOpts)
|
||||||
, instances1 = _getAjvInstances(opts, useOpts1);
|
, instances1 = _getAjvInstances(opts, useOpts1);
|
||||||
return instances.concat(instances1);
|
return instances.concat(instances1);
|
||||||
} else return [ Ajv(useOpts) ];
|
} else return [ new Ajv(useOpts) ];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ describe('compileAsync method', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
loadCallCount = 0;
|
loadCallCount = 0;
|
||||||
ajv = Ajv({ loadSchema: loadSchema });
|
ajv = new Ajv({ loadSchema: loadSchema });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -201,7 +201,7 @@ describe('compileAsync method', function() {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"minimum": 2
|
"minimum": 2
|
||||||
};
|
};
|
||||||
var ajv = Ajv();
|
var ajv = new Ajv();
|
||||||
should.throw(function() {
|
should.throw(function() {
|
||||||
ajv.compileAsync(schema, function() {
|
ajv.compileAsync(schema, function() {
|
||||||
done(new Error('it should have thrown exception'));
|
done(new Error('it should have thrown exception'));
|
||||||
|
@ -263,7 +263,7 @@ describe('compileAsync method', function() {
|
||||||
"a": { "$ref": "object.json" }
|
"a": { "$ref": "object.json" }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var ajv = Ajv({ loadSchema: badLoadSchema });
|
var ajv = new Ajv({ loadSchema: badLoadSchema });
|
||||||
ajv.compileAsync(schema, function (err, validate) {
|
ajv.compileAsync(schema, function (err, validate) {
|
||||||
should.exist(err);
|
should.exist(err);
|
||||||
should.not.exist(validate);
|
should.not.exist(validate);
|
||||||
|
|
|
@ -371,27 +371,27 @@ describe('async schemas, formats and keywords', function() {
|
||||||
describe('async/transpile option', function() {
|
describe('async/transpile option', function() {
|
||||||
it('should throw error with unknown async option', function() {
|
it('should throw error with unknown async option', function() {
|
||||||
shouldThrowFunc('bad async mode: es8', function() {
|
shouldThrowFunc('bad async mode: es8', function() {
|
||||||
Ajv({ async: 'es8' });
|
new Ajv({ async: 'es8' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should throw error with unknown transpile option', function() {
|
it('should throw error with unknown transpile option', function() {
|
||||||
shouldThrowFunc('bad transpiler: babel', function() {
|
shouldThrowFunc('bad transpiler: babel', function() {
|
||||||
Ajv({ transpile: 'babel' });
|
new Ajv({ transpile: 'babel' });
|
||||||
});
|
});
|
||||||
|
|
||||||
shouldThrowFunc('bad transpiler: [object Object]', function() {
|
shouldThrowFunc('bad transpiler: [object Object]', function() {
|
||||||
Ajv({ transpile: {} });
|
new Ajv({ transpile: {} });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should set async option to es7 if tranpiler is nodent', function() {
|
it('should set async option to es7 if tranpiler is nodent', function() {
|
||||||
var ajv1 = Ajv({ transpile: 'nodent' });
|
var ajv1 = new Ajv({ transpile: 'nodent' });
|
||||||
ajv1._opts.async .should.equal('es7');
|
ajv1._opts.async .should.equal('es7');
|
||||||
|
|
||||||
var ajv2 = Ajv({ async: '*', transpile: 'nodent' });
|
var ajv2 = new Ajv({ async: '*', transpile: 'nodent' });
|
||||||
ajv2._opts.async .should.equal('es7');
|
ajv2._opts.async .should.equal('es7');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -140,8 +140,8 @@ describe('Type coercion', function () {
|
||||||
var ajv, fullAjv, instances;
|
var ajv, fullAjv, instances;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ajv = Ajv({ coerceTypes: true, verbose: true });
|
ajv = new Ajv({ coerceTypes: true, verbose: true });
|
||||||
fullAjv = Ajv({ coerceTypes: true, verbose: true, allErrors: true });
|
fullAjv = new Ajv({ coerceTypes: true, verbose: true, allErrors: true });
|
||||||
instances = [ ajv, fullAjv ];
|
instances = [ ajv, fullAjv ];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -263,7 +263,7 @@ describe('Type coercion', function () {
|
||||||
|
|
||||||
|
|
||||||
it('should update data if the schema is in ref that is not inlined', function () {
|
it('should update data if the schema is in ref that is not inlined', function () {
|
||||||
instances.push(Ajv({ coerceTypes: true, inlineRefs: false }));
|
instances.push(new Ajv({ coerceTypes: true, inlineRefs: false }));
|
||||||
|
|
||||||
var schema = {
|
var schema = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
|
|
|
@ -13,9 +13,9 @@ describe('Validation errors', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
function createInstances(errorDataPath) {
|
function createInstances(errorDataPath) {
|
||||||
ajv = Ajv({ errorDataPath: errorDataPath, loopRequired: 21 });
|
ajv = new Ajv({ errorDataPath: errorDataPath, loopRequired: 21 });
|
||||||
ajvJP = Ajv({ errorDataPath: errorDataPath, jsonPointers: true, loopRequired: 21 });
|
ajvJP = new Ajv({ errorDataPath: errorDataPath, jsonPointers: true, loopRequired: 21 });
|
||||||
fullAjv = Ajv({ errorDataPath: errorDataPath, allErrors: true, jsonPointers: true, loopRequired: 21 });
|
fullAjv = new Ajv({ errorDataPath: errorDataPath, allErrors: true, jsonPointers: true, loopRequired: 21 });
|
||||||
}
|
}
|
||||||
|
|
||||||
it('error should include dataPath', function() {
|
it('error should include dataPath', function() {
|
||||||
|
@ -265,7 +265,7 @@ describe('Validation errors', function () {
|
||||||
|
|
||||||
|
|
||||||
it('should not validate required twice in large schemas with loopRequired option', function() {
|
it('should not validate required twice in large schemas with loopRequired option', function() {
|
||||||
var ajv = Ajv({ loopRequired: 1, allErrors: true });
|
var ajv = new Ajv({ loopRequired: 1, allErrors: true });
|
||||||
|
|
||||||
var schema = {
|
var schema = {
|
||||||
properties: {
|
properties: {
|
||||||
|
@ -283,7 +283,7 @@ describe('Validation errors', function () {
|
||||||
|
|
||||||
|
|
||||||
it('should not validate required twice with $data ref', function() {
|
it('should not validate required twice with $data ref', function() {
|
||||||
var ajv = Ajv({ v5: true, allErrors: true });
|
var ajv = new Ajv({ v5: true, allErrors: true });
|
||||||
|
|
||||||
var schema = {
|
var schema = {
|
||||||
properties: {
|
properties: {
|
||||||
|
|
|
@ -11,7 +11,7 @@ describe('issue #8: schema with shared references', function() {
|
||||||
|
|
||||||
function spec(method) {
|
function spec(method) {
|
||||||
return function() {
|
return function() {
|
||||||
var ajv = Ajv();
|
var ajv = new Ajv();
|
||||||
|
|
||||||
var propertySchema = {
|
var propertySchema = {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
|
@ -48,7 +48,7 @@ describe('issue #50: references with "definitions"', function () {
|
||||||
return function() {
|
return function() {
|
||||||
var result;
|
var result;
|
||||||
|
|
||||||
var ajv = Ajv();
|
var ajv = new Ajv();
|
||||||
|
|
||||||
ajv[method]({
|
ajv[method]({
|
||||||
id: 'http://example.com/test/person.json#',
|
id: 'http://example.com/test/person.json#',
|
||||||
|
@ -88,7 +88,7 @@ describe('issue #182, NaN validation', function() {
|
||||||
var ajv;
|
var ajv;
|
||||||
|
|
||||||
before(function(){
|
before(function(){
|
||||||
ajv = Ajv();
|
ajv = new Ajv();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not pass minimum/maximum validation', function() {
|
it('should not pass minimum/maximum validation', function() {
|
||||||
|
@ -105,7 +105,7 @@ describe('issue #182, NaN validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
function testNaN(schema, NaNisValid) {
|
function testNaN(schema, NaNisValid) {
|
||||||
var validate = Ajv().compile(schema);
|
var validate = new Ajv().compile(schema);
|
||||||
validate(NaN) .should.equal(NaNisValid);
|
validate(NaN) .should.equal(NaNisValid);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -113,7 +113,7 @@ describe('issue #182, NaN validation', function() {
|
||||||
|
|
||||||
describe('issue #204, options schemas and v5 used together', function() {
|
describe('issue #204, options schemas and v5 used together', function() {
|
||||||
it('should use v5 metaschemas by default', function() {
|
it('should use v5 metaschemas by default', function() {
|
||||||
var ajv = Ajv({
|
var ajv = new Ajv({
|
||||||
v5: true,
|
v5: true,
|
||||||
schemas: [{id: 'str', type: 'string'}],
|
schemas: [{id: 'str', type: 'string'}],
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,7 +9,7 @@ var Ajv = require('./ajv')
|
||||||
describe('Ajv Options', function () {
|
describe('Ajv Options', function () {
|
||||||
describe('removeAdditional', function() {
|
describe('removeAdditional', function() {
|
||||||
it('should remove all additional properties', function() {
|
it('should remove all additional properties', function() {
|
||||||
var ajv = Ajv({ removeAdditional: 'all' });
|
var ajv = new Ajv({ removeAdditional: 'all' });
|
||||||
|
|
||||||
ajv.addSchema({
|
ajv.addSchema({
|
||||||
id: '//test/fooBar',
|
id: '//test/fooBar',
|
||||||
|
@ -28,7 +28,7 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
|
|
||||||
it('should remove properties that would error when `additionalProperties = false`', function() {
|
it('should remove properties that would error when `additionalProperties = false`', function() {
|
||||||
var ajv = Ajv({ removeAdditional: true });
|
var ajv = new Ajv({ removeAdditional: true });
|
||||||
|
|
||||||
ajv.addSchema({
|
ajv.addSchema({
|
||||||
id: '//test/fooBar',
|
id: '//test/fooBar',
|
||||||
|
@ -48,7 +48,7 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
|
|
||||||
it('should remove properties that would error when `additionalProperties` is a schema', function() {
|
it('should remove properties that would error when `additionalProperties` is a schema', function() {
|
||||||
var ajv = Ajv({ removeAdditional: 'failing' });
|
var ajv = new Ajv({ removeAdditional: 'failing' });
|
||||||
|
|
||||||
ajv.addSchema({
|
ajv.addSchema({
|
||||||
id: '//test/fooBar',
|
id: '//test/fooBar',
|
||||||
|
@ -87,7 +87,7 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('ownProperties', function() {
|
describe('ownProperties', function() {
|
||||||
it('should only validate against own properties of data if specified', function() {
|
it('should only validate against own properties of data if specified', function() {
|
||||||
var ajv = Ajv({ ownProperties: true });
|
var ajv = new Ajv({ ownProperties: true });
|
||||||
var validate = ajv.compile({
|
var validate = ajv.compile({
|
||||||
properties: { c: { type: 'number' } },
|
properties: { c: { type: 'number' } },
|
||||||
additionalProperties: false
|
additionalProperties: false
|
||||||
|
@ -103,7 +103,7 @@ describe('Ajv Options', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should only validate against own properties when using patternProperties', function() {
|
it('should only validate against own properties when using patternProperties', function() {
|
||||||
var ajv = Ajv({ allErrors: true, ownProperties: true });
|
var ajv = new Ajv({ allErrors: true, ownProperties: true });
|
||||||
var validate = ajv.compile({
|
var validate = ajv.compile({
|
||||||
patternProperties: { 'f.*o': { type: 'integer' } },
|
patternProperties: { 'f.*o': { type: 'integer' } },
|
||||||
});
|
});
|
||||||
|
@ -118,7 +118,7 @@ describe('Ajv Options', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should only validate against own properties when using patternGroups', function() {
|
it('should only validate against own properties when using patternGroups', function() {
|
||||||
var ajv = Ajv({ v5: true, allErrors: true, ownProperties: true });
|
var ajv = new Ajv({ v5: true, allErrors: true, ownProperties: true });
|
||||||
var validate = ajv.compile({
|
var validate = ajv.compile({
|
||||||
patternGroups: {
|
patternGroups: {
|
||||||
'f.*o': { schema: { type: 'integer' } }
|
'f.*o': { schema: { type: 'integer' } }
|
||||||
|
@ -135,7 +135,7 @@ describe('Ajv Options', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should only validate against own properties when using patternRequired', function() {
|
it('should only validate against own properties when using patternRequired', function() {
|
||||||
var ajv = Ajv({ v5: true, allErrors: true, ownProperties: true });
|
var ajv = new Ajv({ v5: true, allErrors: true, ownProperties: true });
|
||||||
var validate = ajv.compile({
|
var validate = ajv.compile({
|
||||||
patternRequired: [ 'f.*o' ]
|
patternRequired: [ 'f.*o' ]
|
||||||
});
|
});
|
||||||
|
@ -152,8 +152,8 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('meta and validateSchema', function() {
|
describe('meta and validateSchema', function() {
|
||||||
it('should add draft-4 meta schema by default', function() {
|
it('should add draft-4 meta schema by default', function() {
|
||||||
testOptionMeta(Ajv());
|
testOptionMeta(new Ajv());
|
||||||
testOptionMeta(Ajv({ meta: true }));
|
testOptionMeta(new Ajv({ meta: true }));
|
||||||
|
|
||||||
function testOptionMeta(ajv) {
|
function testOptionMeta(ajv) {
|
||||||
ajv.getSchema('http://json-schema.org/draft-04/schema') .should.be.a('function');
|
ajv.getSchema('http://json-schema.org/draft-04/schema') .should.be.a('function');
|
||||||
|
@ -165,19 +165,19 @@ describe('Ajv Options', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw if meta: false and validateSchema: true', function() {
|
it('should throw if meta: false and validateSchema: true', function() {
|
||||||
var ajv = Ajv({ meta: false });
|
var ajv = new Ajv({ meta: false });
|
||||||
should.not.exist(ajv.getSchema('http://json-schema.org/draft-04/schema'));
|
should.not.exist(ajv.getSchema('http://json-schema.org/draft-04/schema'));
|
||||||
should.throw(function() { ajv.addSchema({ type: 'integer' }, 'integer') });
|
should.throw(function() { ajv.addSchema({ type: 'integer' }, 'integer') });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should skip schema validation with validateSchema: false', function() {
|
it('should skip schema validation with validateSchema: false', function() {
|
||||||
var ajv = Ajv();
|
var ajv = new Ajv();
|
||||||
should.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
should.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
||||||
|
|
||||||
var ajv = Ajv({ validateSchema: false });
|
var ajv = new Ajv({ validateSchema: false });
|
||||||
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
||||||
|
|
||||||
var ajv = Ajv({ validateSchema: false, meta: false });
|
var ajv = new Ajv({ validateSchema: false, meta: false });
|
||||||
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -186,21 +186,21 @@ describe('Ajv Options', function () {
|
||||||
var loggedError = false;
|
var loggedError = false;
|
||||||
console.error = function() { loggedError = true; logError.apply(console, arguments); }
|
console.error = function() { loggedError = true; logError.apply(console, arguments); }
|
||||||
|
|
||||||
var ajv = Ajv({ validateSchema: 'log' });
|
var ajv = new Ajv({ validateSchema: 'log' });
|
||||||
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
||||||
loggedError .should.equal(true);
|
loggedError .should.equal(true);
|
||||||
console.error = logError;
|
console.error = logError;
|
||||||
|
|
||||||
var ajv = Ajv({ validateSchema: 'log', meta: false });
|
var ajv = new Ajv({ validateSchema: 'log', meta: false });
|
||||||
should.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
should.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should validate v5 schema', function() {
|
it('should validate v5 schema', function() {
|
||||||
var ajv = Ajv({ v5: true });
|
var ajv = new Ajv({ v5: true });
|
||||||
ajv.validateSchema({ contains: { minimum: 2 } }) .should.equal(true);
|
ajv.validateSchema({ contains: { minimum: 2 } }) .should.equal(true);
|
||||||
ajv.validateSchema({ contains: 2 }). should.equal(false);
|
ajv.validateSchema({ contains: 2 }). should.equal(false);
|
||||||
|
|
||||||
var ajv = Ajv();
|
var ajv = new Ajv();
|
||||||
ajv.validateSchema({ contains: 2 }). should.equal(true);
|
ajv.validateSchema({ contains: 2 }). should.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -211,7 +211,7 @@ describe('Ajv Options', function () {
|
||||||
myKeyword: { type: 'boolean' }
|
myKeyword: { type: 'boolean' }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var ajv = Ajv({ meta: meta });
|
var ajv = new Ajv({ meta: meta });
|
||||||
ajv.validateSchema({ myKeyword: true }) .should.equal(true);
|
ajv.validateSchema({ myKeyword: true }) .should.equal(true);
|
||||||
ajv.validateSchema({ myKeyword: 2 }) .should.equal(false);
|
ajv.validateSchema({ myKeyword: 2 }) .should.equal(false);
|
||||||
ajv.validateSchema({
|
ajv.validateSchema({
|
||||||
|
@ -219,7 +219,7 @@ describe('Ajv Options', function () {
|
||||||
myKeyword: 2
|
myKeyword: 2
|
||||||
}) .should.equal(true);
|
}) .should.equal(true);
|
||||||
|
|
||||||
var ajv = Ajv();
|
var ajv = new Ajv();
|
||||||
ajv.validateSchema({ myKeyword: true }) .should.equal(true);
|
ajv.validateSchema({ myKeyword: true }) .should.equal(true);
|
||||||
ajv.validateSchema({ myKeyword: 2 }) .should.equal(true);
|
ajv.validateSchema({ myKeyword: 2 }) .should.equal(true);
|
||||||
});
|
});
|
||||||
|
@ -228,7 +228,7 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('schemas', function() {
|
describe('schemas', function() {
|
||||||
it('should add schemas from object', function() {
|
it('should add schemas from object', function() {
|
||||||
var ajv = Ajv({ schemas: {
|
var ajv = new Ajv({ schemas: {
|
||||||
int: { type: 'integer' },
|
int: { type: 'integer' },
|
||||||
str: { type: 'string' }
|
str: { type: 'string' }
|
||||||
}});
|
}});
|
||||||
|
@ -240,7 +240,7 @@ describe('Ajv Options', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add schemas from array', function() {
|
it('should add schemas from array', function() {
|
||||||
var ajv = Ajv({ schemas: [
|
var ajv = new Ajv({ schemas: [
|
||||||
{ id: 'int', type: 'integer' },
|
{ id: 'int', type: 'integer' },
|
||||||
{ id: 'str', type: 'string' },
|
{ id: 'str', type: 'string' },
|
||||||
{ id: 'obj', properties: { int: { $ref: 'int' }, str: { $ref: 'str' } } }
|
{ id: 'obj', properties: { int: { $ref: 'int' }, str: { $ref: 'str' } } }
|
||||||
|
@ -255,8 +255,8 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('format', function() {
|
describe('format', function() {
|
||||||
it('should not validate formats if option format == false', function() {
|
it('should not validate formats if option format == false', function() {
|
||||||
var ajv = Ajv()
|
var ajv = new Ajv()
|
||||||
, ajvFF = Ajv({ format: false });
|
, ajvFF = new Ajv({ format: false });
|
||||||
|
|
||||||
var schema = { format: 'date-time' };
|
var schema = { format: 'date-time' };
|
||||||
var invalideDateTime = '06/19/1963 08:30:06 PST';
|
var invalideDateTime = '06/19/1963 08:30:06 PST';
|
||||||
|
@ -266,8 +266,8 @@ describe('Ajv Options', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not validate formatMaximum/Minimum if option format == false', function() {
|
it('should not validate formatMaximum/Minimum if option format == false', function() {
|
||||||
var ajv = Ajv({ v5: true, allErrors: true })
|
var ajv = new Ajv({ v5: true, allErrors: true })
|
||||||
, ajvFF = Ajv({ v5: true, allErrors: true, format: false });
|
, ajvFF = new Ajv({ v5: true, allErrors: true, format: false });
|
||||||
|
|
||||||
var schema = {
|
var schema = {
|
||||||
format: 'date',
|
format: 'date',
|
||||||
|
@ -283,7 +283,7 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('formats', function() {
|
describe('formats', function() {
|
||||||
it('should add formats from options', function() {
|
it('should add formats from options', function() {
|
||||||
var ajv = Ajv({ formats: {
|
var ajv = new Ajv({ formats: {
|
||||||
identifier: /^[a-z_$][a-z0-9_$]*$/i
|
identifier: /^[a-z_$][a-z0-9_$]*$/i
|
||||||
}});
|
}});
|
||||||
|
|
||||||
|
@ -297,15 +297,15 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('missingRefs', function() {
|
describe('missingRefs', function() {
|
||||||
it('should throw if ref is missing without this option', function() {
|
it('should throw if ref is missing without this option', function() {
|
||||||
var ajv = Ajv();
|
var ajv = new Ajv();
|
||||||
should.throw(function() {
|
should.throw(function() {
|
||||||
ajv.compile({ $ref: 'missing_reference' });
|
ajv.compile({ $ref: 'missing_reference' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not throw and pass validation with missingRef == "ignore"', function() {
|
it('should not throw and pass validation with missingRef == "ignore"', function() {
|
||||||
testMissingRefsIgnore(Ajv({ missingRefs: 'ignore' }));
|
testMissingRefsIgnore(new Ajv({ missingRefs: 'ignore' }));
|
||||||
testMissingRefsIgnore(Ajv({ missingRefs: 'ignore', allErrors: true }));
|
testMissingRefsIgnore(new Ajv({ missingRefs: 'ignore', allErrors: true }));
|
||||||
|
|
||||||
function testMissingRefsIgnore(ajv) {
|
function testMissingRefsIgnore(ajv) {
|
||||||
var validate = ajv.compile({ $ref: 'missing_reference' });
|
var validate = ajv.compile({ $ref: 'missing_reference' });
|
||||||
|
@ -314,10 +314,10 @@ describe('Ajv Options', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not throw and fail validation with missingRef == "fail" if the ref is used', function() {
|
it('should not throw and fail validation with missingRef == "fail" if the ref is used', function() {
|
||||||
testMissingRefsFail(Ajv({ missingRefs: 'fail' }));
|
testMissingRefsFail(new Ajv({ missingRefs: 'fail' }));
|
||||||
testMissingRefsFail(Ajv({ missingRefs: 'fail', verbose: true }));
|
testMissingRefsFail(new Ajv({ missingRefs: 'fail', verbose: true }));
|
||||||
testMissingRefsFail(Ajv({ missingRefs: 'fail', allErrors: true }));
|
testMissingRefsFail(new Ajv({ missingRefs: 'fail', allErrors: true }));
|
||||||
testMissingRefsFail(Ajv({ missingRefs: 'fail', allErrors: true, verbose: true }));
|
testMissingRefsFail(new Ajv({ missingRefs: 'fail', allErrors: true, verbose: true }));
|
||||||
|
|
||||||
function testMissingRefsFail(ajv) {
|
function testMissingRefsFail(ajv) {
|
||||||
var validate = ajv.compile({
|
var validate = ajv.compile({
|
||||||
|
@ -338,8 +338,8 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('uniqueItems', function() {
|
describe('uniqueItems', function() {
|
||||||
it('should not validate uniqueItems with uniqueItems option == false', function() {
|
it('should not validate uniqueItems with uniqueItems option == false', function() {
|
||||||
testUniqueItems(Ajv({ uniqueItems: false }));
|
testUniqueItems(new Ajv({ uniqueItems: false }));
|
||||||
testUniqueItems(Ajv({ uniqueItems: false, allErrors: true }));
|
testUniqueItems(new Ajv({ uniqueItems: false, allErrors: true }));
|
||||||
|
|
||||||
function testUniqueItems(ajv) {
|
function testUniqueItems(ajv) {
|
||||||
var validate = ajv.compile({ uniqueItems: true });
|
var validate = ajv.compile({ uniqueItems: true });
|
||||||
|
@ -352,9 +352,9 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('unicode', function() {
|
describe('unicode', function() {
|
||||||
it('should use String.prototype.length with unicode option == false', function() {
|
it('should use String.prototype.length with unicode option == false', function() {
|
||||||
var ajvUnicode = Ajv();
|
var ajvUnicode = new Ajv();
|
||||||
testUnicode(Ajv({ unicode: false }));
|
testUnicode(new Ajv({ unicode: false }));
|
||||||
testUnicode(Ajv({ unicode: false, allErrors: true }));
|
testUnicode(new Ajv({ unicode: false, allErrors: true }));
|
||||||
|
|
||||||
function testUnicode(ajv) {
|
function testUnicode(ajv) {
|
||||||
var validateWithUnicode = ajvUnicode.compile({ minLength: 2 });
|
var validateWithUnicode = ajvUnicode.compile({ minLength: 2 });
|
||||||
|
@ -375,8 +375,8 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('verbose', function() {
|
describe('verbose', function() {
|
||||||
it('should add schema, parentSchema and data to errors with verbose option == true', function() {
|
it('should add schema, parentSchema and data to errors with verbose option == true', function() {
|
||||||
testVerbose(Ajv({ verbose: true }));
|
testVerbose(new Ajv({ verbose: true }));
|
||||||
testVerbose(Ajv({ verbose: true, allErrors: true }));
|
testVerbose(new Ajv({ verbose: true, allErrors: true }));
|
||||||
|
|
||||||
function testVerbose(ajv) {
|
function testVerbose(ajv) {
|
||||||
var schema = { properties: { foo: { minimum: 5 } } };
|
var schema = { properties: { foo: { minimum: 5 } } };
|
||||||
|
@ -398,8 +398,8 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('multipleOfPrecision', function() {
|
describe('multipleOfPrecision', function() {
|
||||||
it('should allow for some deviation from 0 when validating multipleOf with value < 1', function() {
|
it('should allow for some deviation from 0 when validating multipleOf with value < 1', function() {
|
||||||
test(Ajv({ multipleOfPrecision: 7 }));
|
test(new Ajv({ multipleOfPrecision: 7 }));
|
||||||
test(Ajv({ multipleOfPrecision: 7, allErrors: true }));
|
test(new Ajv({ multipleOfPrecision: 7, allErrors: true }));
|
||||||
|
|
||||||
function test(ajv) {
|
function test(ajv) {
|
||||||
var schema = { multipleOf: 0.01 };
|
var schema = { multipleOf: 0.01 };
|
||||||
|
@ -456,8 +456,8 @@ describe('Ajv Options', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should replace undefined item with default value', function() {
|
it('should replace undefined item with default value', function() {
|
||||||
test(Ajv({ useDefaults: true }));
|
test(new Ajv({ useDefaults: true }));
|
||||||
test(Ajv({ useDefaults: true, allErrors: true }));
|
test(new Ajv({ useDefaults: true, allErrors: true }));
|
||||||
|
|
||||||
function test(ajv) {
|
function test(ajv) {
|
||||||
var schema = {
|
var schema = {
|
||||||
|
@ -490,15 +490,15 @@ describe('Ajv Options', function () {
|
||||||
describe('useDefaults: by value / by reference', function() {
|
describe('useDefaults: by value / by reference', function() {
|
||||||
describe('using by value', function() {
|
describe('using by value', function() {
|
||||||
it('should NOT modify underlying defaults when modifying validated data', function() {
|
it('should NOT modify underlying defaults when modifying validated data', function() {
|
||||||
test('value', Ajv({ useDefaults: true }));
|
test('value', new Ajv({ useDefaults: true }));
|
||||||
test('value', Ajv({ useDefaults: true, allErrors: true }));
|
test('value', new Ajv({ useDefaults: true, allErrors: true }));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('using by reference', function() {
|
describe('using by reference', function() {
|
||||||
it('should modify underlying defaults when modifying validated data', function() {
|
it('should modify underlying defaults when modifying validated data', function() {
|
||||||
test('reference', Ajv({ useDefaults: 'shared' }));
|
test('reference', new Ajv({ useDefaults: 'shared' }));
|
||||||
test('reference', Ajv({ useDefaults: 'shared', allErrors: true }));
|
test('reference', new Ajv({ useDefaults: 'shared', allErrors: true }));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -541,7 +541,7 @@ describe('Ajv Options', function () {
|
||||||
var ajv;
|
var ajv;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ajv = Ajv({ addUsedSchema: optionValue });
|
ajv = new Ajv({ addUsedSchema: optionValue });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('compile and validate', function() {
|
describe('compile and validate', function() {
|
||||||
|
@ -579,7 +579,7 @@ describe('Ajv Options', function () {
|
||||||
var ajv;
|
var ajv;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ajv = Ajv({ addUsedSchema: false });
|
ajv = new Ajv({ addUsedSchema: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -647,7 +647,7 @@ describe('Ajv Options', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
function getValidate(passContext) {
|
function getValidate(passContext) {
|
||||||
ajv = Ajv({ passContext: passContext, inlineRefs: false });
|
ajv = new Ajv({ passContext: passContext, inlineRefs: false });
|
||||||
ajv.addKeyword('testValidate', { validate: storeContext });
|
ajv.addKeyword('testValidate', { validate: storeContext });
|
||||||
ajv.addKeyword('testCompile', { compile: compileTestValidate });
|
ajv.addKeyword('testCompile', { compile: compileTestValidate });
|
||||||
|
|
||||||
|
@ -683,8 +683,8 @@ describe('Ajv Options', function () {
|
||||||
|
|
||||||
describe('allErrors', function() {
|
describe('allErrors', function() {
|
||||||
it('should be disabled inside "not" keyword', function() {
|
it('should be disabled inside "not" keyword', function() {
|
||||||
test(Ajv(), false);
|
test(new Ajv(), false);
|
||||||
test(Ajv({ allErrors: true }), true);
|
test(new Ajv({ allErrors: true }), true);
|
||||||
|
|
||||||
function test(ajv, allErrors) {
|
function test(ajv, allErrors) {
|
||||||
var format1called = false
|
var format1called = false
|
||||||
|
|
|
@ -165,31 +165,31 @@ describe('resolve', function () {
|
||||||
];
|
];
|
||||||
|
|
||||||
it('by default should inline schema if it doesn\'t contain refs', function() {
|
it('by default should inline schema if it doesn\'t contain refs', function() {
|
||||||
var ajv = Ajv({ schemas: schemas });
|
var ajv = new Ajv({ schemas: schemas });
|
||||||
testSchemas(ajv, true);
|
testSchemas(ajv, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should NOT inline schema if option inlineRefs == false', function() {
|
it('should NOT inline schema if option inlineRefs == false', function() {
|
||||||
var ajv = Ajv({ schemas: schemas, inlineRefs: false });
|
var ajv = new Ajv({ schemas: schemas, inlineRefs: false });
|
||||||
testSchemas(ajv, false);
|
testSchemas(ajv, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should inline schema if option inlineRefs is bigger than number of keys in referenced schema', function() {
|
it('should inline schema if option inlineRefs is bigger than number of keys in referenced schema', function() {
|
||||||
var ajv = Ajv({ schemas: schemas, inlineRefs: 3 });
|
var ajv = new Ajv({ schemas: schemas, inlineRefs: 3 });
|
||||||
testSchemas(ajv, true);
|
testSchemas(ajv, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should NOT inline schema if option inlineRefs is less than number of keys in referenced schema', function() {
|
it('should NOT inline schema if option inlineRefs is less than number of keys in referenced schema', function() {
|
||||||
var ajv = Ajv({ schemas: schemas, inlineRefs: 2 });
|
var ajv = new Ajv({ schemas: schemas, inlineRefs: 2 });
|
||||||
testSchemas(ajv, false);
|
testSchemas(ajv, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should avoid schema substitution when refs are inlined (issue #77)', function() {
|
it('should avoid schema substitution when refs are inlined (issue #77)', function() {
|
||||||
var ajv = Ajv({ verbose: true });
|
var ajv = new Ajv({ verbose: true });
|
||||||
|
|
||||||
var schemaMessage = {
|
var schemaMessage = {
|
||||||
$schema: "http://json-schema.org/draft-04/schema#",
|
$schema: "http://json-schema.org/draft-04/schema#",
|
||||||
|
|
Loading…
Reference in New Issue