/
any.spec.ts
47 lines (44 loc) · 1.02 KB
/
any.spec.ts
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
import Schema from '../src';
const testNoErrorsFor = value => done => {
new Schema({
v: {
type: 'any',
},
}).validate(
{
v: value,
},
errors => {
expect(errors).toBe(null);
done();
},
);
};
const testRequiredErrorFor = value => done => {
new Schema({
v: {
required: true,
type: 'string',
},
}).validate(
{
v: value,
},
errors => {
expect(errors.length).toBe(1);
expect(errors[0].message).toBe('v is required');
done();
},
);
};
describe('any', () => {
it('allows null', testNoErrorsFor(null));
it('allows undefined', testNoErrorsFor(undefined));
it('allows strings', testNoErrorsFor('foo'));
it('allows numbers', testNoErrorsFor(1));
it('allows booleans', testNoErrorsFor(false));
it('allows arrays', testNoErrorsFor([]));
it('allows objects', testNoErrorsFor({}));
it('rejects undefined when required', testRequiredErrorFor(undefined));
it('rejects null when required', testRequiredErrorFor(null));
});