2015-09-23 12:20:51 +03:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-27 19:43:56 +03:00
|
|
|
var Ajv = require('./ajv')
|
|
|
|
, should = require('./chai').should();
|
2015-09-23 12:20:51 +03:00
|
|
|
|
2015-09-23 13:22:37 +03:00
|
|
|
|
2016-02-03 00:37:02 +03:00
|
|
|
describe('issue #8: schema with shared references', function() {
|
|
|
|
it('should be supported by addSchema', spec('addSchema'));
|
|
|
|
|
2016-05-17 00:02:29 +03:00
|
|
|
it('should be supported by compile', spec('compile'));
|
2016-02-03 00:37:02 +03:00
|
|
|
|
|
|
|
function spec(method) {
|
|
|
|
return function() {
|
2016-06-04 23:16:11 +03:00
|
|
|
var ajv = new Ajv();
|
2016-02-03 00:37:02 +03:00
|
|
|
|
|
|
|
var propertySchema = {
|
|
|
|
type: 'string',
|
|
|
|
maxLength: 4
|
|
|
|
};
|
|
|
|
|
|
|
|
var schema = {
|
|
|
|
id: 'obj.json#',
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
foo: propertySchema,
|
|
|
|
bar: propertySchema
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ajv[method](schema);
|
|
|
|
|
|
|
|
var result = ajv.validate('obj.json#', { foo: 'abc', bar: 'def' });
|
|
|
|
result .should.equal(true);
|
|
|
|
|
|
|
|
var result = ajv.validate('obj.json#', { foo: 'abcde', bar: 'fghg' });
|
|
|
|
result .should.equal(false);
|
|
|
|
ajv.errors .should.have.length(1);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-23 13:22:37 +03:00
|
|
|
describe('issue #50: references with "definitions"', function () {
|
2016-02-03 00:37:02 +03:00
|
|
|
it('should be supported by addSchema', spec('addSchema'));
|
|
|
|
|
|
|
|
it('should be supported by compile', spec('addSchema'));
|
|
|
|
|
|
|
|
function spec(method) {
|
|
|
|
return function() {
|
|
|
|
var result;
|
|
|
|
|
2016-06-04 23:16:11 +03:00
|
|
|
var ajv = new Ajv();
|
2016-02-03 00:37:02 +03:00
|
|
|
|
|
|
|
ajv[method]({
|
|
|
|
id: 'http://example.com/test/person.json#',
|
|
|
|
definitions: {
|
|
|
|
name: { type: 'string' }
|
|
|
|
},
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
name: { $ref: '#/definitions/name'}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ajv[method]({
|
|
|
|
id: 'http://example.com/test/employee.json#',
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
person: { $ref: '/test/person.json#' },
|
|
|
|
role: { type: 'string' }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
result = ajv.validate('http://example.com/test/employee.json#', {
|
|
|
|
person: {
|
|
|
|
name: 'Alice'
|
|
|
|
},
|
|
|
|
role: 'Programmer'
|
|
|
|
});
|
|
|
|
|
|
|
|
result. should.equal(true);
|
|
|
|
should.equal(ajv.errors, null);
|
|
|
|
};
|
|
|
|
}
|
2015-09-23 12:20:51 +03:00
|
|
|
});
|
2016-05-17 00:02:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
describe('issue #182, NaN validation', function() {
|
|
|
|
var ajv;
|
|
|
|
|
|
|
|
before(function(){
|
2016-06-04 23:16:11 +03:00
|
|
|
ajv = new Ajv();
|
2016-05-17 00:02:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not pass minimum/maximum validation', function() {
|
|
|
|
testNaN({ minimum: 1 }, false);
|
|
|
|
testNaN({ maximum: 1 }, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should pass type: number validation', function() {
|
|
|
|
testNaN({ type: 'number' }, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not pass type: integer validation', function() {
|
|
|
|
testNaN({ type: 'integer' }, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
function testNaN(schema, NaNisValid) {
|
2016-06-04 23:16:11 +03:00
|
|
|
var validate = new Ajv().compile(schema);
|
2016-05-17 00:02:29 +03:00
|
|
|
validate(NaN) .should.equal(NaNisValid);
|
|
|
|
}
|
|
|
|
});
|
2016-06-01 09:07:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
describe('issue #204, options schemas and v5 used together', function() {
|
|
|
|
it('should use v5 metaschemas by default', function() {
|
2016-06-04 23:16:11 +03:00
|
|
|
var ajv = new Ajv({
|
2016-06-01 09:07:34 +03:00
|
|
|
v5: true,
|
|
|
|
schemas: [{id: 'str', type: 'string'}],
|
|
|
|
});
|
|
|
|
|
|
|
|
var schema = { constant: 42 };
|
|
|
|
var validate = ajv.compile(schema);
|
|
|
|
|
|
|
|
validate(42) .should.equal(true);
|
|
|
|
validate(43) .should.equal(false);
|
|
|
|
|
|
|
|
ajv.validate('str', 'foo') .should.equal(true);
|
|
|
|
ajv.validate('str', 42) .should.equal(false);
|
|
|
|
});
|
|
|
|
});
|