-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
validators.ts
133 lines (119 loc) · 3.36 KB
/
validators.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
/**
* required
* @param {string} value The string value that should be validated
* @returns {string | null}
* @description Method that validates if a value is empty or not
*/
export const required = (value: string | boolean): string | null => {
if (
value === undefined ||
value === null ||
(typeof value === "string" && value.length === 0)
) {
return "This field is required!";
}
return null;
};
/**
* min
* @param {string} value The value that should be validated
* @param {number} length The minimal length that the value has to be
* @param {string} fieldName The field name is needed, so that the validator return a more sensible message
* @returns {string | null}
* @description Method that validates if a value is shorter than a minimal length
*/
export const min = (
value: string,
length: number,
fieldName?: string
): string | null => {
if (value.length <= length) {
return `${
fieldName ? fieldName : "This field"
} has to be longer than ${length} characters!`;
}
return null;
};
/**
* max
* @param {string} value The value that should be validated
* @param {number} length The maximum length that the value has to be
* @param {string} fieldName The field name is needed, so that the validator return a more sensible message
* @returns {string | null}
* @description Method that validates if a value is longer than a minimal length
*/
export const max = (
value: string,
length: number,
fieldName?: string
): string | null => {
if (value.length > length) {
return `${
fieldName ? fieldName : "This field"
} has to be shorter than ${length} characters!`;
}
return null;
};
/**
* match
* @param {string | number} value The value that should be validated
* @param {string | number} revalue The other value that should be matched to the first value
* @param {string} fieldName The field name is needed, so that the validator return a more sensible message
* @returns {string | null}
* @description Method that checks if two values are the same
*/
export const match = (
value: string | number | boolean | number,
revalue: string | number,
fieldName?: string
): string | null => {
if (value !== revalue) {
return `${fieldName ? fieldName : "Fields"} must match!`;
}
return null;
};
/**
* url
* @param {string} value The value that should be validated
* @returns {string | null}
* @description Method that check if a value is a valid url
*/
export const url = (value: string): string | null => {
if (!/^(ftp|http|https):\/\/[^ "]+$/.test(value)) {
return "This is not a valid URL!";
}
return null;
};
/**
* email
* @param {string} value The value that should be validated
* @returns {string | null}
* @description Method that checks if a value is a valid email
*/
export const email = (value: string): string | null => {
if (!/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/.test(value)) {
return "This is not a valid email!";
}
return null;
};
/**
* username
* @param {string} value The value that should be validated
* @returns {string | null}
* @description Method that checks if a value is a valid username
*/
export const username = (value: string): string | null => {
if (!/^[a-z0-9_-]{4,255}$/.test(value)) {
return "This is not a valid username!";
}
return null;
};
export default {
min,
max,
required,
match,
email,
username,
url,
};