Repository navigation
Expand file tree
/
Copy pathsimple-validators.tsx
More file actions
72 lines (61 loc) · 1.63 KB
/
Copy pathsimple-validators.tsx
File metadata and controls
72 lines (61 loc) · 1.63 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import trim from 'lodash/trim'
function isBlank(s: string): boolean {
return trim(s).length === 0
}
function hasSpaces(s: string): boolean {
return s.includes(' ')
}
function hasPeriod(s: string): boolean {
return s.includes('.')
}
function hasAtSign(s: string): boolean {
return s.includes('@')
}
function isEmptyOrBlank(thing?: string): boolean {
if (!thing || isBlank(thing)) {
return true
}
return false
}
// Returns an error string if not valid
function isValidCommon(thing?: string): string {
if (isEmptyOrBlank(thing)) return 'Cannot be blank'
if (thing && hasSpaces(thing)) return 'No spaces allowed'
return ''
}
// Returns an error string if not valid
function isValidUsername(username?: string): string {
if (!username) {
return ''
}
const commonError = isValidCommon(username)
if (commonError) {
return commonError
}
if (hasPeriod(username)) {
return "Usernames can't contain periods."
}
if (username.startsWith('_')) {
return "Usernames can't start with an underscore."
}
if (username.includes('__')) {
return "Usernames can't contain double underscores to avoid confusion."
}
return ''
}
// Returns an error if not valid
function isValidEmail(email?: string): string {
const commonError = isValidCommon(email)
if (!commonError) {
if (email && hasAtSign(email)) {
return ''
}
}
return email ? 'Invalid email address.' : 'Empty email address.'
}
// Returns an error string if not valid
function isValidName(name?: string): string {
if (isEmptyOrBlank(name)) return 'Please provide your name.'
return ''
}
export {isValidUsername, isValidEmail, isValidName}