-
Notifications
You must be signed in to change notification settings - Fork 1
/
array-fixed.test.ts
61 lines (49 loc) · 1.51 KB
/
array-fixed.test.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {
Type,
isArray,
isArrayElement,
isFixedArrayElement,
isString,
number,
string,
} from '../src/type';
it('Test isString', () => {
const Person = new Type([[isString]]);
const me = 'Harry Porter';
const errors = Person.validate(me);
expect(isArray(errors)).toBe(true);
expect(errors.length).toBe(0);
});
it('Test fixed array', () => {
const isEndsWith = (value: string, endString: string) => isString(value) && value.endsWith(endString);
const Person = new Type({
books: [[isFixedArrayElement(string, number, [[isEndsWith, ['@gmail.com']]])]],
});
const me = {
books: ['Harry Porter', 1984, 'harry@gmail.com'],
};
const errors = Person.validate(me);
expect(isArray(errors)).toBe(true);
expect(errors.length).toBe(0);
});
it('Test fixed array', () => {
const isEndsWith = (value: string, endString: string) => isString(value) && value.endsWith(endString);
const isCustomArray = (_: null, __: null, arrayValue: Array<any>) => {
return (
arrayValue.length === 3
&& new Type(string).check(arrayValue[0])
&& new Type(number).check(arrayValue[1])
&& new Type([[isEndsWith, ['@gmail.com']]]).check(arrayValue[2])
);
};
const Person = new Type({
books: [[isArrayElement], [isCustomArray]],
});
const me = {
books: ['Harry Porter', 1984, 'harry@gmail.com'],
};
const errors = Person.validate(me);
console.log(errors);
expect(isArray(errors)).toBe(true);
expect(errors.length).toBe(0);
});