This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.ts
263 lines (215 loc) · 7.86 KB
/
string.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { InvalidProperty, MissingProperty } from "../errors.ts";
export function validateString(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) => validateString(value, label, required, true));
}
if (typeof input === "undefined" || input === null) {
// If the input isn't required return false and "escape" the parent function
if (!required) {
return false;
}
// Throw an error if the input is missing
throw new MissingProperty(label);
}
if (typeof input !== "string" || input.length === 0) {
const datatype = array ? "comma-separated list of strings" : "string";
throw new InvalidProperty(label, datatype);
}
return true;
}
export function validateTimestamp(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) =>
validateTimestamp(value, label, required, true)
);
}
if (!validateString(input, label, required)) {
return false;
}
// Copied and modified RegExp from https://stackoverflow.com/a/58878432/9615506
const regex = new RegExp(
/^[0-9]{4}-((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(0[469]|11)-(0[1-9]|[12][0-9]|30)|(02)-(0[1-9]|[12][0-9]))T(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])\.[0-9]{3}Z$/,
);
if (!regex.test(input as string)) {
const datatype = array ? "comma-separated list of dates" : "date";
throw new InvalidProperty(label, datatype);
}
return true;
}
export function validateUUID(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) => validateUUID(value, label, required, true));
}
if (!validateString(input, label, required)) {
return false;
}
// Copied RegExp from https://melvingeorge.me/blog/check-if-string-valid-uuid-regex-javascript
const regex = new RegExp(
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i,
);
if (!regex.test(input as string)) {
const datatype = array ? "comma-separated list of UUIDs" : "UUID";
throw new InvalidProperty(label, datatype);
}
return true;
}
// Check length
export function validateEmail(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) => validateEmail(value, label, required, true));
}
if (!validateString(input, label, required)) {
return false;
}
// Copied RegExp from https://deno.land/x/validasaur@v0.15.0/src/rules/is_email.ts
const regex = new RegExp(
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i,
);
if (!regex.test(input as string)) {
const datatype = array ? "comma-separated list of emails" : "email";
throw new InvalidProperty(label, datatype);
}
return true;
}
export function validateTime(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) => validateTime(value, label, required, true));
}
if (!validateString(input, label, required)) {
return false;
}
// Copied RegExp from https://stackoverflow.com/a/5563222/9615506
const regex = new RegExp(
/^([0-1][0-9]|2[0-3]):([0-5][0-9])$/,
);
if (!regex.test(input as string)) {
const datatype = array ? "comma-separated list of times" : "time";
throw new InvalidProperty(label, datatype);
}
return true;
}
export function validateUrl(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) =>
validateVarchar(value, label, required, true)
);
}
if (!validateString(input, label, required)) {
return false;
}
const string = input as string;
const length = string.length;
// Copied RegExp from https://stackoverflow.com/a/64206393/9615506
const regex = new RegExp(
/^(http|https):\/\/[^ "]+$/,
);
if (!regex.test(string) || length > 2083) {
const datatype = array ? "comma-separated list of URLs" : "URL";
throw new InvalidProperty(label, datatype);
}
return true;
}
export function validateVarchar(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) =>
validateVarchar(value, label, required, true)
);
}
if (!validateString(input, label, required)) {
return true;
}
const string = input as string;
const length = string.length;
if (length < 3 || length > 255) {
const datatype = array ? "comma-separated list of verchars" : "varchar";
throw new InvalidProperty(label, datatype);
}
return true;
}
// TODO: Add validate large testing suite
export function validateLarge(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) => validateLarge(value, label, required, true));
}
if (!validateString(input, label, required)) {
return false;
}
const string = input as string;
const length = string.length;
if (length < 3 || length > 4096) {
const datatype = array ? "comma-separated list of larges" : "large";
throw new InvalidProperty(label, datatype);
}
return true;
}
export function validateIPv64(
input: unknown,
label: string,
required = true,
array = false,
): boolean {
// If the input is an array we'll check if every child is valid recursively
if (Array.isArray(input)) {
return input.every((value) => validateIPv64(value, label, required, true));
}
if (!validateString(input, label, required)) {
return false;
}
// Copied RegExp from https://www.regextester.com/104038
const regex = new RegExp(
/((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/,
);
if (!regex.test(input as string)) {
const datatype = array ? "comma-separated list of IPv64s" : "IPv64";
throw new InvalidProperty(label, datatype);
}
return true;
}