-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.js
542 lines (515 loc) · 18.7 KB
/
validator.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import { translated } from './languageHelper'
import {
arrUnique,
EMAIL_REGEX,
hasValue,
isAddress,
isArr,
isBool,
isDefined,
isHash,
isHex,
isInteger,
isObj,
isStr,
isValidDate,
isValidNumber,
objHasKeys,
objWithoutKeys,
isValidURL,
isFn,
fallbackIfFails,
isMap,
} from './utils'
export const messages = {
accept: 'value not acceptable',
array: 'valid array required',
boolean: 'boolean value required',
date: 'valid date required',
dateMax: 'value must be smaller or equal to',
dateMin: 'value must be greater or equal to',
decimals: 'value exceeds maximum allowed decimals',
email: 'valid email address required',
function: 'valid function required',
hash: 'valid cryptographic hash string required',
hex: 'valid hexadecimal string required',
identity: 'valid identity required',
integer: 'valid integer required (no decimals)',
requiredKeys: 'missing one or more required fields',
lengthMax: 'maximum length exceeded',
lengthMin: 'minimum length required',
number: 'valid number required',
numberMax: 'value must be smaller or equal to',
numberMin: 'value must be greater or euqal to',
object: 'valid object required',
regex: 'invalid string pattern',
regexError: 'regex validation failed',
reject: 'value not acceptable',
required: 'required field',
string: 'valid string required',
type: 'invalid data type',
unique: 'array must not contain duplicate values',
url: 'invalid URL',
// non-TYPE specific
unexpectedError: 'unexpected validation error occured',
}
translated(messages, true)
export const messagesAlt = {
numberMax: 'max',
numberMin: 'min',
lengthMax: 'maxLength',
lengthMin: 'minLegnth',
}
// Accepted validation types.
// Any type not listed here will be ignored.
export const TYPES = Object.freeze({
array: 'array',
boolean: 'boolean',
date: 'date',
email: 'email',
function: 'function',
hash: 'hash',
hex: 'hex',
identity: 'identity',
integer: 'integer',
map: 'map',
number: 'number',
object: 'object',
string: 'string',
url: 'url',
})
const errorConcat = (message, ...suffix) => {
if (!isStr(message)) return message
return [message, ...suffix].join(' ')
}
/**
* @name setMessages
* @summary Overrides default error messages with custom/translated error messages
*
* @param {Object} messagesOverrides Object with custom/translated messages. Must contain the correct keys.
*/
export const setMessages = (messagesOverrides = {}) => {
isObj(messagesOverrides) && Object
.keys(messagesOverrides)
.forEach(key => messages[key] = messagesOverrides[key])
return messages
}
// if msg is falsy, returns true
const msgOrTrue = (msg, value) => !msg || msg === true
? true
: `${msg}${isDefined(value) ? ': ' + value : ''}`
/**
* @name validate
* @summary validate single value
*
* @param {*} value
* @param {Object} config
* @param {String|Object} customMessages if `string`, all errors will use a single message.
*
* @example
* <BR>
*
* ```javascript
* validate(123456, { max: 9999, min: 0, required: true, type: TYPES.number})
* ```
*
* @returns {String|Null} null if valid according to supplied config. Otherwise, validation error message.
*/
export const validate = (value, config, customMessages = {}) => {
const errorMsgs = {
...messages,
...customMessages,
...config.customMessages,
}
// If error message is falsy change it to `true`
Object
.keys(errorMsgs)
.forEach(key => errorMsgs[key] = errorMsgs[key] || true)
try {
let err
let {
accept,
decimals,
config: propertiesAlt, // to be deprecated
failFast,
includeLabel,
includeValue = true,
instanceOf,
label,
max,
maxLength,
min,
minLength,
name,
properties = propertiesAlt,
or, // alternative validation configaration if validation fails
regex,
reject,
required,
requiredKeys,
strict, // for url or object types
type,
unique = false,
} = config || {}
const _msgOrTrue = (msg, value) => msgOrTrue(
isStr(config.customMessages)
? config.customMessages
: msg,
includeValue
? value
: undefined
)
properties = !isArr(properties)
? properties
: properties // turn properties array into an object validation config
.filter(x => !!x.name) // must have a name of the property
.reduce((obj, item) => ({
...obj,
[item.name]: item,
}), { required, type: TYPES.object })
// default strict mode for the following types
strict ??= [
TYPES.email,
TYPES.hex,
].includes(type)
const gotValue = hasValue(value)
// const typeErrMsg = errorMsgs[type]
if (isObj(or) && !!TYPES[or.type]) {
const configWithoutOr = objWithoutKeys(config, ['or'])
err = validate(value, configWithoutOr, errorMsgs)
if (!err) return null
const errOr = validate(value, or, errorMsgs)
// alternative config validated successfully
if (!errOr) return null
return typeof value === or.type
? errOr
: err
// // primary validation
// if (!err && gotValue || err !== typeErrMsg) return err
// // secondary (or) validation
// return validate(value, or, errorMsgs)
}
// if doesn't have any value (undefined/null) and not `required`, assume valid
if (!gotValue) return required
? _msgOrTrue(errorMsgs.required)
: null
let valueIsArr, valueIsMap, valueIsObj
// validate value type
switch (type) {
case TYPES.array:
if (!isArr(value)) return _msgOrTrue(errorMsgs.array)
if (unique && arrUnique(value).length < value.length) return _msgOrTrue(errorMsgs.unique)
valueIsArr = true
break
case TYPES.boolean:
if (!isBool(value)) return _msgOrTrue(errorMsgs.boolean)
break
case TYPES.date:
// validates both string and Date object
const date = new Date(value)
// const isValidDate = isDate(date)
if (!isValidDate(value)) return _msgOrTrue(errorMsgs.date)
// makes sure auto correction didnt occur when using `new Date()`.
// Eg: 2020-02-30 is auto corrected to 2021-03-02)
const dateInvalid = isStr(value)
&& date.toISOString().split('T')[0] !== value.replace(' ', 'T').split('T')[0]
if (dateInvalid) return _msgOrTrue(errorMsgs.date)
if (max && new Date(max) < date) return _msgOrTrue(errorMsgs.dateMax, max)
if (min && new Date(min) > date) return _msgOrTrue(errorMsgs.dateMin, min)
break
case TYPES.email:
if (!isStr(value)) return _msgOrTrue(errorMsgs.email)
const x = value.split('@')[0]
const allowPlus = !strict && !x.startsWith('+')
&& !x.endsWith('+')
&& (x.match(/\+/g) || []).length === 1
if (allowPlus) value = value.replace('+', '')
if (!EMAIL_REGEX.test(value)) return _msgOrTrue(errorMsgs.email)
break
case TYPES.function:
if (!isFn(value)) return _msgOrTrue(errorMsgs.function)
break
case TYPES.hash:
if (!isHash(value)) return _msgOrTrue(errorMsgs.hash)
break
case TYPES.hex:
const prefix = !strict
&& isStr(value)
&& !value.startsWith('0x')
&& '0x'
|| ''
if (!isHex(prefix + value)) return _msgOrTrue(errorMsgs.hex)
break
case TYPES.identity:
const {
chainType,
chainId,
ignoreChecksum
} = config || {}
const isIdentityValid = isAddress(
value,
chainType,
chainId,
ignoreChecksum,
)
if (!isIdentityValid) return _msgOrTrue(errorMsgs.identity)
break
case TYPES.integer:
if (!isInteger(value)) return _msgOrTrue(errorMsgs.integer)
break
case TYPES.map:
if (!isMap(value)) return _msgOrTrue(errorMsgs.map)
valueIsMap = true
break
case TYPES.number:
if (!isValidNumber(value)) return _msgOrTrue(errorMsgs.number)
if (isValidNumber(min) && value < min) return _msgOrTrue(
errorMsgs.min ?? errorMsgs.numberMin,
min
)
if (isValidNumber(max) && value > max) return _msgOrTrue(
errorMsgs.max ?? errorMsgs.numberMax,
max
)
if (isValidNumber(decimals) && decimals >= 0) {
if (decimals === 0) {
if (!isInteger(value)) return _msgOrTrue(errorMsgs.integer)
break
}
const len = `${value || ''}`
.split('.')[1]
?.length || 0
if (len > decimals) return _msgOrTrue(errorMsgs.decimals, decimals)
}
break
case TYPES.object:
if (!isObj(value, strict)) return _msgOrTrue(errorMsgs.object)
if (
isArr(requiredKeys)
&& requiredKeys.length > 0
&& !objHasKeys(value, requiredKeys)
) return _msgOrTrue(errorMsgs.requiredKeys)
valueIsObj = true
// validate child properties of the `value` object
err = properties && validateObj(
value,
properties,
failFast,
includeLabel,
errorMsgs,
includeValue,
)
if (err) return err
break
case TYPES.string:
if (!isStr(value)) return _msgOrTrue(errorMsgs.string)
break
case TYPES.url:
try {
if (!isStr(value)) throw errorMsgs.url
const url = new URL(value)
// Hack to fix comparison failure due to a trailing slash automatically added by `new URL()`
if (url.href.endsWith('/') && !value.endsWith('/')) value += '/'
const urlInvalid = url.host.endsWith('.')
|| url.hostname.endsWith('.')
|| strict && (
// make sure domain extension is provided
// Or if domain starts with www both extension and name is provided
url.host.split('.').length <= (url.host.startsWith('www.') ? 2 : 1)
// catch any auto-correction by `new URL()`.
// Eg: spaces in domain name being replaced by `%20`
// or missing `//` in protocol being added.
// When `new URL('https:google.com').href` is turned inot 'https://google.com/'
|| url.href.toLowerCase() !== value.toLowerCase()
)
if (urlInvalid) throw errorMsgs.url
} catch (e) {
return _msgOrTrue(errorMsgs.url)
}
break
default:
// validation for unlisted types by checking if the value is an instance of `type`
// (eg: ApiPromise, BN)
try {
if (!(value instanceof instanceOf)) return _msgOrTrue(errorMsgs.instanceof || errorMsgs.type)
} catch (_) { }
// unsupported type
return _msgOrTrue(errorMsgs.type)
}
// valid only if value `accept` array includes `value` or items in `value` array
if (isArr(accept) && accept.length) {
// if `value` is array all items in it must be in the `accept` array
const valid = !valueIsArr
? accept.includes(value)
: !value.find(v => !accept.includes(v))
if (!valid) return _msgOrTrue(errorMsgs.accept)
}
// valid only if value `reject` array does not include the `value` or items in `value` array
if (isArr(reject) && reject.length) {
const valid = !valueIsArr
? !reject.includes(value)
: !value.find(v => reject.includes(v))
if (!valid) return _msgOrTrue(errorMsgs.reject)
}
// if regex is an Array, assume it as arguments to instantiate `RegExp` object
if (isArr(regex)) regex = new RegExp(...regex)
// validate regex expression
if (regex instanceof RegExp && !regex.test(value)) return _msgOrTrue(errorMsgs.regex)
// validate array/integer/number/string length
const len = (valueIsArr ? value : `${value}`).length
if (isValidNumber(maxLength) && len > maxLength) return _msgOrTrue(
errorMsgs.maxLength ?? errorMsgs.lengthMax,
maxLength
)
if (isValidNumber(minLength) && len < minLength) return _msgOrTrue(
errorMsgs.minLength ?? errorMsgs.lengthMin,
minLength
)
// WIP: validate children | test required
const validateChildren = [TYPES.array, TYPES.map].includes(type) && isObj(properties)
if (validateChildren) {
const items = valueIsArr
? value
: valueIsMap
? [...value].map(([_, item]) => item)
: valueIsObj
? [value]
: []
for (let i = 0;i < items.length;i++) {
err = validateObj(
items[i],
{
label: valueIsObj
? label
: `${label} [${i}]`,
name: valueIsObj
? name
: `${name}[${i}]`,
...properties,
},
failFast,
includeLabel,
errorMsgs,
includeValue,
)
if (err) return _msgOrTrue(err)
}
}
// valid according to the config
return null
} catch (err) {
return errorConcat(errorMsgs.unexpectedError, '\n', err)
}
}
/**
* @name validateObj
* @summary validate object property values
*
* @param {Object} obj object with values to validate
* @param {Object} config configuration to validate specific keys in the object
* @param {Boolean} failFast whether to return on first error
* @param {Boolean} includeLabel whether to include property name in the error
* @param {Boolean|String|Object} customMessages if `string`, all errors will use a single message.
* If `true`, will return true for all messages
* @param {Boolen} includeValue whether to include value in the error (where applicable)
*
* @example
* <BR>
*
* ```javascript
* const valueObj = { name: 'some name', age: 23 }
* const config = {
* name: {
* maxLength: 32,
* minLength: 3,
* required: true,
* type: TYPES.string,
* },
* age: {
* max: 50,
* maxLength: 2,
* min: 18,
* name: 'Minimum age', // alternate name to be used instead of the key with error
* required: true,
* type: TYPES.integer,
* }
* }
* validateObj(valueObj, { max: 9999, min: 0, required: true, type: TYPES.number})
* ```
*
* @returns {String|Object|Null} Null if no errors. If @failFast, String otherwise, Object with one or more errors.
*/
export const validateObj = (
obj = {},
config = {},
failFast = true,
includeLabel = true,
customMessages = {},
includeValue = true,
) => {
try {
const errorMsgs = {
...messages,
...customMessages,
...(isStr(customMessages) || isBool(customMessages))
&& Object
.keys(messages)
.reduce((obj, key) => ({
...obj,
[key]: customMessages,
}), {})
}
if (!isObj(obj, config.strict || false)) return msgOrTrue(errorMsgs.object)
const keys = Object.keys(config)
let errors = {}
for (let i = 0;i < keys.length;i++) {
const key = keys[i]
const value = obj[key]
const keyConf = config[key]
if (!isObj(keyConf, false)) continue
const {
// config: childConf,
customMessages: entryMsgs,
label,
name,
// type,
} = keyConf
let error = validate(
value,
{
failFast,
includeLabel,
includeValue,
...keyConf,
},
isStr(entryMsgs)
? entryMsgs
// combine error messages
: { ...errorMsgs, ...entryMsgs },
)
if (!error) continue
if (includeLabel) {
const errKey = isStr(label)
&& label // In case used with FormInput and label an element
|| name
|| key
error = `${errKey} => ${error}`
}
if (failFast) return error
// combine all errors into a single object
errors[key] = error
}
return Object.keys(errors).length
? errors
: null // all valid according to config
} catch (err) {
return err
}
}
export default {
messages,
setMessages,
TYPES,
validate,
validateObj,
}