-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputValidation.ts
83 lines (71 loc) · 2.72 KB
/
inputValidation.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
/**
* The base class for Validations
*/
export default abstract class InputValidation {
/**
* Determines if an input string is masked or not
* @param input The input string to analyze
* @returns true or false depending if the input is masked.
*/
public abstract isMasked(input: string): boolean;
/**
* Removes the mask from an input. It will not do any validation,
* so for avoiding bugs, use cleanMaskAndValidate instead or only use
* this method if you are sure the input is valid
* @param input The input string to clean the mask
* @returns The cleaned input
*/
public abstract cleanMaskUnsafe(input: string): string;
/**
* Adds the mask to an input. It will not do any validation,
* so for avoiding bugs, use insertMaskAndValidate instead or only use
* this method if you are sure the input is valid
* @param input The input string to clean the mask
* @returns The cleaned input
*/
public abstract insertMaskUnsafe(input: string): string;
/**
* Removed the mask from an input. If the input isn't valid,
* an error will be thrown
* @param input The input string to clean
* @returns The cleaned input
*/
public cleanMaskAndValidate(input: string): string {
const clean = this.cleanMaskUnsafe(input);
if (!this.validateUnmasked(clean)) {
throw new Error(`Tried to remove mask from invalid input ("${input}").`);
}
return clean;
}
/**
* Adds the mask to an input string. If the input isn't valid,
* an error will be thrown
* @param input The input string to mask
* @returns The masked input
*/
public insertMaskAndValidate(input: string): string {
const masked = this.insertMaskUnsafe(input);
if (!this.validateMasked(masked)) {
throw new Error(`Tried mask an invalid input ("${input}").`);
}
return masked;
}
/**
* Validates an unmasked input. If it is valid but masked, false will be returned
* @param input The input string to validate
*/
public abstract validateUnmasked(input: string): boolean;
/**
* Validates an masked input. If it is valid but unmasked, false will be returned
* @param input The input string to validate
*/
public abstract validateMasked(input: string): boolean;
/**
* Validates the input string, masked or unmasked.
* @param input The input to validate
* @returns True or false if the input is valid or not. Being masked/unmasked makes no difference.
*/
public validate(input: string): boolean {
return this.isMasked(input) ? this.validateMasked(input) : this.validateUnmasked(input);
}
}