-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validators.ts
165 lines (132 loc) · 4.63 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
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
import { Validator } from './Validator'
const MAX_SAFE_INT32 = 2 ** 31 - 1
const MIN_SAFE_INT32 = -MAX_SAFE_INT32
function required(): Validator<string> {
return (value) => {
if (!value.trim()) return 'This field is required.'
return undefined
}
}
function minLength(minLength: number): Validator<string> {
return (value) => {
// trim because backend may trim value before checking length
if (value && value.trim().length < minLength)
return `The value must be at least ${minLength} characters.`
return undefined
}
}
function maxLength(maxLength: number): Validator<string> {
return (value) => {
if (value && value.length > maxLength)
return `The value cannot be longer than ${maxLength} characters.`
return undefined
}
}
// Don't do: !isNaN(parseFloat(value)) since then isNumber('12b') === true
function isNumber(value: string): boolean {
// 1st case: has digits to right of decimal (may have digits to the left)
// 2nd case: has digits to the left of decimal only
if (/^-?\d*\.\d+$/.test(value) || /^-?\d+\.?$/.test(value)) {
const n = parseFloat(value)
return Math.abs(n) <= Number.MAX_VALUE
}
return false
}
/** For a required numeric/integer input, you must also pass the required() validator */
function number(): Validator<string> {
return (value) => {
if (value && !isNumber(value)) return 'You must enter a number.'
return undefined
}
}
function isInteger(value: string): boolean {
if (/^-?\d+$/.test(value)) {
const n = parseInt(value)
return n <= MAX_SAFE_INT32 && n >= MIN_SAFE_INT32
}
return false
}
function integer(): Validator<string> {
return (value) => {
if (value && !isInteger(value)) return 'You must enter a whole number.'
return undefined
}
}
function greaterThan(x: number): Validator<string> {
return (value) => {
if (value && !isNumber(value)) return 'You must enter a number.'
if (value && parseFloat(value) <= x) return `The value must be greater than ${x}.`
return undefined
}
}
function greaterThanOrEqual(x: number): Validator<string> {
return (value) => {
if (value && !isNumber(value)) return 'You must enter a number.'
if (value && parseFloat(value) < x)
return `The value must be greater than or equal to ${x}.`
return undefined
}
}
function lessThan(x: number): Validator<string> {
return (value) => {
if (value && !isNumber(value)) return 'You must enter a number.'
if (value && parseFloat(value) >= x) return `The value must be less than ${x}.`
return undefined
}
}
function lessThanOrEqual(x: number): Validator<string> {
return (value) => {
if (value && !isNumber(value)) return 'You must enter a number.'
if (value && parseFloat(value) > x)
return `The value must be less than or equal to ${x}.`
return undefined
}
}
// From HTML5 spec - https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
const emailRegex =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
/** Regex-based email validator that uses the regex from the HTML5 spec. */
function email(): Validator<string> {
return (value) => {
if (value && !emailRegex.test(value))
return 'You must enter a valid email address.'
return undefined
}
}
function money(options?: { allowNegative?: boolean }): Validator<string> {
const allowNegative = options?.allowNegative ?? false
const defaultFeedback =
'You must enter a valid dollar amount. Do not type the $ sign.'
return (value) => {
value = value.trim()
if (!value) return undefined
const _isNumber = isNumber(value)
const hasAtMost2DecimalPlaces = /^-?\d*\.?\d{0,2}$/.test(value)
const signIsAllowed = allowNegative || parseFloat(value) >= 0
if (!_isNumber) return defaultFeedback
if (!signIsAllowed) return 'Negative amounts are not allowed.'
if (!hasAtMost2DecimalPlaces) return defaultFeedback
return undefined
}
}
/**
* Validators for `string` values. Each property on this object is a function that returns
* a `Validator<string>`.
*
* ```
* const validators = [Validators.required(), Validators.integer(), Validators.greaterThan(0)]
* ```
*/
export const Validators = {
required,
minLength,
maxLength,
number,
integer,
greaterThan,
greaterThanOrEqual,
lessThan,
lessThanOrEqual,
email,
money,
}