forked from iotaledger/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidationHelper.ts
More file actions
50 lines (47 loc) · 1.58 KB
/
Copy pathvalidationHelper.ts
File metadata and controls
50 lines (47 loc) · 1.58 KB
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
/**
* Helper functions for validating input.
*/
export class ValidationHelper {
/**
* Does the string have some content.
* @param str The string to validate.
* @param name The parameter name.
*/
public static string(str: string, name: string): void {
if (str === undefined || str === null || str.trim().length === 0) {
throw new Error(`The parameter '${name}' has an invalid value.`);
}
}
/**
* Does the number have a value.
* @param num The number to validate.
* @param name The parameter name.
*/
public static number(num: number, name: string): void {
if (num === undefined || num === null || typeof num !== "number") {
throw new Error(`The parameter '${name}' has an invalid value.`);
}
}
/**
* Is the value of one the specified items.
* @param val The value to validate.
* @param options The possible options.
* @param name The parameter name.
*/
public static oneOf(val: unknown, options: unknown[], name: string): void {
if (!options.includes(val)) {
throw new Error(`The parameter '${name}' has an invalid value.`);
}
}
/**
* Is the value trytes.
* @param str The string to validate.
* @param length The length to match.
* @param name The parameter name.
*/
public static trytes(str: string, length: number, name: string): void {
if (!new RegExp(`^[A-Z9]{${length}}$`).test(str)) {
throw new Error(`The parameter '${name}' has an invalid value.`);
}
}
}