generated from sapphiredev/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ArrayConstraints.ts
119 lines (107 loc) · 4.36 KB
/
ArrayConstraints.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
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
118
119
import { ExpectedConstraintError } from '../lib/errors/ExpectedConstraintError';
import { Result } from '../lib/Result';
import type { ValidatorOptions } from '../lib/util-types';
import type { IConstraint } from './base/IConstraint';
import { isUnique } from './util/isUnique';
import { equal, greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual, notEqual, type Comparator } from './util/operators';
export type ArrayConstraintName = `s.array(T).${
| 'unique'
| `length${
| 'LessThan'
| 'LessThanOrEqual'
| 'GreaterThan'
| 'GreaterThanOrEqual'
| 'Equal'
| 'NotEqual'
| 'Range'
| 'RangeInclusive'
| 'RangeExclusive'}`}()`;
function arrayLengthComparator<T>(
comparator: Comparator,
name: ArrayConstraintName,
expected: string,
length: number,
options?: ValidatorOptions
): IConstraint<T[]> {
return {
run(input: T[]) {
return comparator(input.length, length) //
? Result.ok(input)
: Result.err(new ExpectedConstraintError(name, options?.message ?? 'Invalid Array length', input, expected));
}
};
}
export function arrayLengthLessThan<T>(value: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length < ${value}`;
return arrayLengthComparator(lessThan, 's.array(T).lengthLessThan()', expected, value, options);
}
export function arrayLengthLessThanOrEqual<T>(value: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length <= ${value}`;
return arrayLengthComparator(lessThanOrEqual, 's.array(T).lengthLessThanOrEqual()', expected, value, options);
}
export function arrayLengthGreaterThan<T>(value: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length > ${value}`;
return arrayLengthComparator(greaterThan, 's.array(T).lengthGreaterThan()', expected, value, options);
}
export function arrayLengthGreaterThanOrEqual<T>(value: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length >= ${value}`;
return arrayLengthComparator(greaterThanOrEqual, 's.array(T).lengthGreaterThanOrEqual()', expected, value, options);
}
export function arrayLengthEqual<T>(value: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length === ${value}`;
return arrayLengthComparator(equal, 's.array(T).lengthEqual()', expected, value, options);
}
export function arrayLengthNotEqual<T>(value: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length !== ${value}`;
return arrayLengthComparator(notEqual, 's.array(T).lengthNotEqual()', expected, value, options);
}
export function arrayLengthRange<T>(start: number, endBefore: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length >= ${start} && expected.length < ${endBefore}`;
return {
run(input: T[]) {
return input.length >= start && input.length < endBefore //
? Result.ok(input)
: Result.err(new ExpectedConstraintError('s.array(T).lengthRange()', options?.message ?? 'Invalid Array length', input, expected));
}
};
}
export function arrayLengthRangeInclusive<T>(start: number, end: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length >= ${start} && expected.length <= ${end}`;
return {
run(input: T[]) {
return input.length >= start && input.length <= end //
? Result.ok(input)
: Result.err(
new ExpectedConstraintError('s.array(T).lengthRangeInclusive()', options?.message ?? 'Invalid Array length', input, expected)
);
}
};
}
export function arrayLengthRangeExclusive<T>(startAfter: number, endBefore: number, options?: ValidatorOptions): IConstraint<T[]> {
const expected = `expected.length > ${startAfter} && expected.length < ${endBefore}`;
return {
run(input: T[]) {
return input.length > startAfter && input.length < endBefore //
? Result.ok(input)
: Result.err(
new ExpectedConstraintError('s.array(T).lengthRangeExclusive()', options?.message ?? 'Invalid Array length', input, expected)
);
}
};
}
export function uniqueArray(options?: ValidatorOptions): IConstraint<unknown[]> {
return {
run(input: unknown[]) {
return isUnique(input) //
? Result.ok(input)
: Result.err(
new ExpectedConstraintError(
's.array(T).unique()',
options?.message ?? 'Array values are not unique',
input,
'Expected all values to be unique'
)
);
}
};
}