1
1
const validator = require ( "validator" ) ;
2
2
3
+ /**
4
+ * @typedef {Object } TextInputProps
5
+ * @property {string } value - The input value
6
+ * @property {string } fieldName - The input label or labeled-by, used for validation message
7
+ * @property {string[] } errors - A collection of the validation errors
8
+ * @property {string } [required] - Indicates whether the input is required
9
+ * @property {string } [minLength] - Specifies the minimum character length of an input, if present
10
+ * @property {string } [maxLength] - Specifies the maximum character length of an input, if present
11
+ * @property {boolean } dirty - Indicates whether the input has been edited by the user
12
+ */
13
+
14
+ /**
15
+ * @typedef {Object } InputValidationResult
16
+ * @property {boolean } valid - Indicates whether the input meets the validation tests
17
+ * @property {string[] } errors - A collection of the validation errors
18
+ */
19
+
3
20
/**
4
21
* Validation library for handling checks for
5
22
* valid Email, Password, Phone, SSN, TIN, Date (MM/dd/yyyy) and Alphanumeric formats,
@@ -125,7 +142,7 @@ class Valido {
125
142
if ( typeof value !== "string" ) return false ;
126
143
127
144
const regex = new RegExp ( / ^ p o | ^ p o b o x | ^ p .o .| ^ p .o . b o x | ^ p o s t o f f i c e b o x | ^ p m b | ^ p r i v a t e m a i l b o x | ^ h c | ^ r r | h c [ 0 - 9 ] + b o x [ 0 - 9 ] + | p o b o x [ 0 - 9 ] + | r r [ 0 - 9 ] + b o x [ 0 - 9 ] + / gim) ;
128
- return regex . test ( value ) == false ;
145
+ return regex . test ( value ) === false ;
129
146
}
130
147
131
148
/**
@@ -152,6 +169,142 @@ class Valido {
152
169
return regex . test ( value ) ;
153
170
}
154
171
172
+
173
+ /**
174
+ * Validates the email address given meets the appropriate format
175
+ * @param {string } value
176
+ */
177
+ static validateEmail ( value ) {
178
+ let errors = [ ] ;
179
+
180
+ if ( value . length > 0 ) {
181
+ if ( ! Valido . IsEmail ( value ) ) {
182
+ errors . push ( "The value given is not a valid email address" ) ;
183
+ }
184
+ }
185
+
186
+ return { valid : errors . length === 0 , errors } ;
187
+ }
188
+
189
+ static validatePassword ( value , name , props ) {
190
+ let errors = [ ] ;
191
+
192
+ if ( value . length > 0 ) {
193
+ if (
194
+ ! Valido . IsPassword (
195
+ value ,
196
+ props [ name ] . alphanumeric ,
197
+ props [ name ] . containsUpperCase ,
198
+ props [ name ] . minLength ,
199
+ props [ name ] . maxLength
200
+ )
201
+ ) {
202
+ if ( 'containsUpperCase' in props [ name ] && Valido . containsUpperCase . test ( value ) === false ) {
203
+ errors . push ( `Password must contain at least one uppercase characters` ) ;
204
+ }
205
+
206
+ if ( 'alphanumeric' in props [ name ] && Valido . containsNumbers . test ( value ) === false ) {
207
+ errors . push ( `Password must contain at least one number` ) ;
208
+ }
209
+ }
210
+ if ( 'containsSpecialChar' in props [ name ] && Valido . containsSpecialChar . test ( value ) === false ) {
211
+ errors . push ( `Password must contain at least one special character` ) ;
212
+ }
213
+ }
214
+
215
+ return { valid : errors . length === 0 , errors } ;
216
+ }
217
+
218
+ /**
219
+ * Validates if the input meets the specified min length and returns a collection of the validation errors
220
+ * @param {TextInputProps } props
221
+ * @returns {InputValidationResult }
222
+ */
223
+ static validateMinLength ( props ) {
224
+ let errors = [ ] ;
225
+
226
+ if ( props . minLength && props . value . length < props . minLength && props . value . length > 0 ) {
227
+ errors . push ( `The field ${ props . fieldName } must be at least ${ props . minLength } characters long` ) ;
228
+ }
229
+
230
+ return { valid : errors . length === 0 , errors } ;
231
+ }
232
+
233
+ /**
234
+ * Validates if the input meets the specified max length and returns a collection of the validation errors
235
+ * @param {TextInputProps } props
236
+ * @returns {InputValidationResult }
237
+ */
238
+ static validateMaxLength ( props ) {
239
+ let errors = [ ] ;
240
+
241
+ if ( props . maxLength && props . value . length > props . maxLength ) {
242
+ errors . push ( `The field ${ props . fieldName } does not meet the maximum length` ) ;
243
+ }
244
+
245
+ return { valid : errors . length === 0 , errors } ;
246
+ }
247
+
248
+ /**
249
+ * Validates if the input meets the specified min length and returns a collection of the validation errors
250
+ * @param {TextInputProps } props
251
+ * @returns {InputValidationResult }
252
+ */
253
+ static validateProperLength ( props ) {
254
+ let errors = [ ] ;
255
+
256
+ if ( props . minLength && props . value . length < props . minLength && props . value . length > 0 ) {
257
+ errors . push ( `The field ${ props . fieldName } must be at least ${ props . minLength } characters long` ) ;
258
+ }
259
+
260
+ if ( props . maxLength && props . value . length > props . maxLength ) {
261
+ errors . push ( `The field ${ props . fieldName } does not meet the maximum length` ) ;
262
+ }
263
+
264
+ return { valid : errors . length === 0 , errors } ;
265
+ }
266
+
267
+ /**
268
+ * Validates if the input is required and has a value, and returns a valid flag along with a collection of the validation errors
269
+ * @param {TextInputProps } props
270
+ * @returns {InputValidationResult }
271
+ */
272
+ static validateRequired ( props ) {
273
+ let errors = [ ] ;
274
+
275
+ if ( props . required && props . value . length === 0 && ( props . dirty ) ) {
276
+ errors . push ( `The field ${ props . fieldName } is required` ) ;
277
+ }
278
+
279
+ return { valid : errors . length === 0 , errors } ;
280
+ }
281
+
282
+ /**
283
+ *
284
+ * @param {{[x: string]: TextInputProps } } values
285
+ * @returns {Promise<boolean> } result
286
+ */
287
+ static isFormValid ( values ) {
288
+ const inputKeys = Object . keys ( values ) ;
289
+
290
+ const result = inputKeys . every ( function ( propertyName ) {
291
+
292
+ /**
293
+ * @type {boolean }
294
+ */
295
+ const propertyIsRequired = typeof values [ propertyName ] === "object" && values [ propertyName ] . required ;
296
+
297
+ // if not required then it evaluates to true, otherwise it checks that there are no errors and the input is not empty
298
+ const isValid =
299
+ ! propertyIsRequired ||
300
+ ( values [ propertyName ] . errors . length === 0 && values [ propertyName ] . value . length > 0 ) ;
301
+
302
+ return isValid ;
303
+ } ) ;
304
+
305
+ return Promise . resolve ( result ) ;
306
+ }
307
+
155
308
}
156
309
157
310
module . exports = Valido ;
0 commit comments