forked from ajv-validator/ajv
/
options_validation.spec.js
117 lines (89 loc) · 3.45 KB
/
options_validation.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
var Ajv = require('../ajv');
require('../chai').should();
describe('validation options', function() {
describe('format', function() {
it('should not validate formats if option format == false', function() {
var ajv = new Ajv
, ajvFF = new Ajv({ format: false });
var schema = { format: 'date-time' };
var invalideDateTime = '06/19/1963 08:30:06 PST';
ajv.validate(schema, invalideDateTime) .should.equal(false);
ajvFF.validate(schema, invalideDateTime) .should.equal(true);
});
});
describe('formats', function() {
it('should add formats from options', function() {
var ajv = new Ajv({ formats: {
identifier: /^[a-z_$][a-z0-9_$]*$/i
}});
var validate = ajv.compile({ format: 'identifier' });
validate('Abc1') .should.equal(true);
validate('123') .should.equal(false);
validate(123) .should.equal(true);
});
});
describe('keywords', function() {
it('should add keywords from options', function() {
var ajv = new Ajv({ keywords: {
string: {
validate: function (schema, data ) {
console.log(">>", data);
return /^[a-z_$][a-z0-9_$]*$/i.test(data);
}
}
}});
var validate = ajv.compile({ string: true });
validate('Abc1') .should.equal(true);
validate('foo bar').should.equal(false);
validate('123').should.equal(false);
validate(123).should.equal(false);
validate(123).should.equal(false);
});
});
describe('uniqueItems', function() {
it('should not validate uniqueItems with uniqueItems option == false', function() {
testUniqueItems(new Ajv({ uniqueItems: false }));
testUniqueItems(new Ajv({ uniqueItems: false, allErrors: true }));
function testUniqueItems(ajv) {
var validate = ajv.compile({ uniqueItems: true });
validate([1,2,3]) .should.equal(true);
validate([1,1,1]) .should.equal(true);
}
});
});
describe('unicode', function() {
it('should use String.prototype.length with unicode option == false', function() {
var ajvUnicode = new Ajv;
testUnicode(new Ajv({ unicode: false }));
testUnicode(new Ajv({ unicode: false, allErrors: true }));
function testUnicode(ajv) {
var validateWithUnicode = ajvUnicode.compile({ minLength: 2 });
var validate = ajv.compile({ minLength: 2 });
validateWithUnicode('😀') .should.equal(false);
validate('😀') .should.equal(true);
validateWithUnicode = ajvUnicode.compile({ maxLength: 1 });
validate = ajv.compile({ maxLength: 1 });
validateWithUnicode('😀') .should.equal(true);
validate('😀') .should.equal(false);
}
});
});
describe('multipleOfPrecision', function() {
it('should allow for some deviation from 0 when validating multipleOf with value < 1', function() {
test(new Ajv({ multipleOfPrecision: 7 }));
test(new Ajv({ multipleOfPrecision: 7, allErrors: true }));
function test(ajv) {
var schema = { multipleOf: 0.01 };
var validate = ajv.compile(schema);
validate(4.18) .should.equal(true);
validate(4.181) .should.equal(false);
schema = { multipleOf: 0.0000001 };
validate = ajv.compile(schema);
validate(53.198098) .should.equal(true);
validate(53.1980981) .should.equal(true);
validate(53.19809811) .should.equal(false);
}
});
});
});