generated from sapphiredev/sapphire-template
/
TypedArrayValidator.ts
117 lines (95 loc) · 3.84 KB
/
TypedArrayValidator.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
import type { IConstraint } from '../constraints/base/IConstraint';
import {
typedArrayByteLengthEqual,
typedArrayByteLengthGreaterThan,
typedArrayByteLengthGreaterThanOrEqual,
typedArrayByteLengthLessThan,
typedArrayByteLengthLessThanOrEqual,
typedArrayByteLengthNotEqual,
typedArrayByteLengthRange,
typedArrayByteLengthRangeExclusive,
typedArrayByteLengthRangeInclusive,
typedArrayLengthEqual,
typedArrayLengthGreaterThan,
typedArrayLengthGreaterThanOrEqual,
typedArrayLengthLessThan,
typedArrayLengthLessThanOrEqual,
typedArrayLengthNotEqual,
typedArrayLengthRange,
typedArrayLengthRangeExclusive,
typedArrayLengthRangeInclusive
} from '../constraints/TypedArrayLengthConstraints';
import { aOrAn } from '../constraints/util/common/vowels';
import { TypedArrays, type TypedArray, type TypedArrayName } from '../constraints/util/typedArray';
import { ValidationError } from '../lib/errors/ValidationError';
import { Result } from '../lib/Result';
import { BaseValidator } from './imports';
export class TypedArrayValidator<T extends TypedArray> extends BaseValidator<T> {
private readonly type: TypedArrayName;
public constructor(type: TypedArrayName, constraints: readonly IConstraint<T>[] = []) {
super(constraints);
this.type = type;
}
public byteLengthLessThan(length: number) {
return this.addConstraint(typedArrayByteLengthLessThan(length));
}
public byteLengthLessThanOrEqual(length: number) {
return this.addConstraint(typedArrayByteLengthLessThanOrEqual(length));
}
public byteLengthGreaterThan(length: number) {
return this.addConstraint(typedArrayByteLengthGreaterThan(length));
}
public byteLengthGreaterThanOrEqual(length: number) {
return this.addConstraint(typedArrayByteLengthGreaterThanOrEqual(length));
}
public byteLengthEqual(length: number) {
return this.addConstraint(typedArrayByteLengthEqual(length));
}
public byteLengthNotEqual(length: number) {
return this.addConstraint(typedArrayByteLengthNotEqual(length));
}
public byteLengthRange(start: number, endBefore: number) {
return this.addConstraint(typedArrayByteLengthRange(start, endBefore));
}
public byteLengthRangeInclusive(startAt: number, endAt: number) {
return this.addConstraint(typedArrayByteLengthRangeInclusive(startAt, endAt) as IConstraint<T>);
}
public byteLengthRangeExclusive(startAfter: number, endBefore: number) {
return this.addConstraint(typedArrayByteLengthRangeExclusive(startAfter, endBefore));
}
public lengthLessThan(length: number) {
return this.addConstraint(typedArrayLengthLessThan(length));
}
public lengthLessThanOrEqual(length: number) {
return this.addConstraint(typedArrayLengthLessThanOrEqual(length));
}
public lengthGreaterThan(length: number) {
return this.addConstraint(typedArrayLengthGreaterThan(length));
}
public lengthGreaterThanOrEqual(length: number) {
return this.addConstraint(typedArrayLengthGreaterThanOrEqual(length));
}
public lengthEqual(length: number) {
return this.addConstraint(typedArrayLengthEqual(length));
}
public lengthNotEqual(length: number) {
return this.addConstraint(typedArrayLengthNotEqual(length));
}
public lengthRange(start: number, endBefore: number) {
return this.addConstraint(typedArrayLengthRange(start, endBefore));
}
public lengthRangeInclusive(startAt: number, endAt: number) {
return this.addConstraint(typedArrayLengthRangeInclusive(startAt, endAt));
}
public lengthRangeExclusive(startAfter: number, endBefore: number) {
return this.addConstraint(typedArrayLengthRangeExclusive(startAfter, endBefore));
}
protected override clone(): this {
return Reflect.construct(this.constructor, [this.type, this.constraints]);
}
protected handle(value: unknown): Result<T, ValidationError> {
return TypedArrays[this.type](value)
? Result.ok(value as T)
: Result.err(new ValidationError('s.typedArray', `Expected ${aOrAn(this.type)}`, value));
}
}