This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathiodine.js
519 lines (456 loc) · 13 KB
/
iodine.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
/*
|--------------------------------------------------------------------------
| Iodine - JavaScript Library
|--------------------------------------------------------------------------
|
| This library contains a collection of useful validation rules that can
| be used to quickly verify whether items meet certain conditions.
|
*/
export default class Iodine
{
/**
* Constructor.
*
*/
constructor()
{
this.locale = undefined;
this.messages = {
after : "The date must be after: '[PARAM]'",
afterOrEqual : "The date must be after or equal to: '[PARAM]'",
array : "[FIELD] must be an array",
before : "The date must be before: '[PARAM]'",
beforeOrEqual : "The date must be before or equal to: '[PARAM]'",
boolean : "[FIELD] must be true or false",
date : "[FIELD] must be a date",
different : "[FIELD] must be different to '[PARAM]'",
endsWith : "[FIELD] must end with '[PARAM]'",
email : "[FIELD] must be a valid email address",
falsy : "[FIELD] must be a falsy value (false, 'false', 0 or '0')",
in : "[FIELD] must be one of the following options: [PARAM]",
integer : "[FIELD] must be an integer",
json : "[FIELD] must be a parsable JSON object string",
max : "[FIELD] must be less than or equal to [PARAM]",
min : "[FIELD] must be greater than or equal to [PARAM]",
maxLength : "[FIELD] must not be greater than '[PARAM]' in character length",
minLength : "[FIELD] must not be less than '[PARAM]' character length",
notIn : "[FIELD] must not be one of the following options: [PARAM]",
numeric : "[FIELD] must be numeric",
optional : "[FIELD] is optional",
regexMatch : "[FIELD] must satisify the regular expression: [PARAM]",
required : "[FIELD] must be present",
same : "[FIELD] must be '[PARAM]'",
startsWith : "[FIELD] must start with '[PARAM]'",
string : "[FIELD] must be a string",
truthy : "[FIELD] must be a truthy value (true, 'true', 1 or '1')",
url : "[FIELD] must be a valid url",
uuid : "[FIELD] must be a valid UUID",
};
}
/**
* @internal.
*
*/
_compare(first, second, type, equals = false)
{
if (! this.assertDate(first)) return false;
if (! this.assertDate(second) && ! this.assertInteger(second)) return false;
second = typeof second === 'number' ? second : second.getTime();
if (type === 'less' && equals) return first.getTime() <= second;
if (type === 'less' && ! equals) return first.getTime() < second;
if (type === 'more' && equals) return first.getTime() >= second;
if (type === 'more' && ! equals) return first.getTime() > second;
}
/**
* @internal.
*
*/
_error(rule, args = undefined)
{
let { param, field } = typeof args === 'object' ? args : { param : args, field : undefined };
const chunks = rule.split(':');
let key = chunks.shift();
param = param || chunks.join(':');
if (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) {
param = new Date(parseInt(param)).toLocaleTimeString(this.locale, {
year : 'numeric',
month : 'short',
day : 'numeric',
hour : '2-digit',
minute : 'numeric',
hour12 : false,
});
}
let message = [null, undefined, ''].includes(param)
? this.messages[key]
: this.messages[key].replace('[PARAM]', param);
return [null, undefined, ''].includes(field)
? message.replace('[FIELD]', this.default_field_name || 'Value')
: message.replace('[FIELD]', field);
}
/**
* @internal.
*
*/
_missing()
{
return {
valid : false,
rule : 'None',
error : 'Rules exist, but no value was provided to check',
};
}
/**
* @internal.
*
*/
_prepare(value, rules = [])
{
if (! rules.length) return [];
if (rules[0] === 'optional' && this.assertOptional(value)) return [];
return rules.filter(rule => rule !== 'optional').map(rule =>
typeof(rule) === 'string'
? [rule, this._title(rule.split(':').shift()), rule.split(':').slice(1).join(':')]
: [`${ rule.rule }:${ rule.param }`, this._title(rule.rule), rule.param]
);
}
/**
* @internal.
*
*/
_title(value)
{
return `${value[0].toUpperCase()}${value.slice(1)}`;
}
/**
* @internal.
*
*/
_validate(value, rules, errors = null)
{
for (let index in rules = this._prepare(value, rules)) {
if (! this[`assert${rules[index][1]}`].apply(this, [value, rules[index][2]])) {
return {
valid : false,
rule : rules[index][0],
error : errors
? errors[rules[index][0]]
: this._error(rules[index][0]),
};
}
}
return {
valid : true,
rule : '',
error : '',
};
}
/**
* Determine if the given content matches the given schema.
*
*/
assert(values, schema, errors = null)
{
if (Array.isArray(schema)) {
return this._validate(values, schema, errors);
}
let keys = Object.keys(schema);
let result = { valid : true, fields : { } };
for (let i = 0; i < keys.length; i++) {
result.fields[keys[i]] = values.hasOwnProperty(keys[i])
? this._validate(values[keys[i]], schema[keys[i]], errors != null ? errors[keys[i]] : null)
: this._missing();
if (! result.fields[keys[i]].valid) {
result.valid = false;
}
}
return result;
}
/**
* Determine if the given date is after another given date.
*
*/
assertAfter(value, after)
{
return this._compare(value, after, 'more', false);
}
/**
* Determine if the given date is after or equal to another given date.
*
*/
assertAfterOrEqual(value, after)
{
return this._compare(value, after, 'more', true);
}
/**
* Determine if the given value is an array.
*
*/
assertArray(value)
{
return Array.isArray(value);
}
/**
* Determine if the given date is before another given date.
*
*/
assertBefore(value, before)
{
return this._compare(value, before, 'less', false);
}
/**
* Determine if the given date is before or equal to another given date.
*
*/
assertBeforeOrEqual(value, before)
{
return this._compare(value, before, 'less', true);
}
/**
* Determine if the given value is a boolean.
*
*/
assertBoolean(value)
{
return [true, false].includes(value);
}
/**
* Determine if the given value is a date object.
*
*/
assertDate(value)
{
return (value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value));
}
/**
* Determine if the given value is different to another given value.
*
*/
assertDifferent(value, different)
{
return value != different;
}
/**
* Determine if the given value ends with another given value.
*
*/
assertEndsWith(value, sub)
{
return this.assertString(value) && value.endsWith(sub);
}
/**
* Determine if the given value is a valid email address.
*
*/
assertEmail(value)
{
let regex = "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$";
return new RegExp(regex).test(String(value).toLowerCase());
}
/**
* Determine if the given value is falsy.
*
*/
assertFalsy(value)
{
return [0, '0', false, 'false'].includes(value);
}
/**
* Determine if the given value is within the given array of options.
*
*/
assertIn(value, options)
{
return (typeof options === 'string' ? options.split(',') : options).includes(value);
}
/**
* Determine if the given value is an integer.
*
*/
assertInteger(value)
{
return Number.isInteger(value) && parseInt(value).toString() === value.toString();
}
/**
* Determine if the given value is a JSON string.
*
*/
assertJson(value)
{
try {
return typeof JSON.parse(value) === 'object';
} catch (e) {
return false;
}
}
/**
* Determine if the given number is less than or equal to the maximum limit.
*
*/
assertMax(value, limit)
{
return parseFloat(value) <= limit;
}
/**
* Determine if the given number is greater than or equal to the minimum limit.
*
*/
assertMin(value, limit)
{
return parseFloat(value) >= limit;
}
/**
* Determine if the given value string length is less than or equal to the maximum limit.
*
*/
assertMaxLength(value, limit)
{
return typeof value === 'string' ? value.length <= limit : false;
}
/**
* Determine if the given value string length is greater than or equal to the minimum limit.
*
*/
assertMinLength(value, limit)
{
return typeof value === 'string' ? value.length >= limit : false;
}
/**
* Determine if the given value is not within the given array of options.
*
*/
assertNotIn(value, options)
{
return ! this.assertIn(value, options);
}
/**
* Determine if the given value is numeric (an integer or a float).
*
*/
assertNumeric(value)
{
return ! isNaN(parseFloat(value)) && isFinite(value);
}
/**
* Determine if the given value is optional.
*
*/
assertOptional(value)
{
return [null, undefined, ''].includes(value);
}
/**
* Determine if the given value satisifies the given regular expression.
*
*/
assertRegexMatch(value, expression)
{
return new RegExp(expression).test(String(value));
}
/**
* Determine if the given value is present.
*
*/
assertRequired(value)
{
return ! this.assertOptional(value);
}
/**
* Determine if the given value is the same as another given value.
*
*/
assertSame(value, same)
{
return value == same;
}
/**
* Determine if the given value starts with another given value.
*
*/
assertStartsWith(value, sub)
{
return this.assertString(value) && value.startsWith(sub);
}
/**
* Determine if the given value is a string.
*
*/
assertString(value)
{
return typeof value === 'string';
}
/**
* Determine if the given value is truthy.
*
*/
assertTruthy(value)
{
return [1, '1', true, 'true'].includes(value);
}
/**
* Determine if the given value is a valid URL.
*
*/
assertUrl(value)
{
let regex = "^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$";
return new RegExp(regex).test(String(value).toLowerCase());
}
/**
* Determine if the given value is a valid UUID.
*
*/
assertUuid(value)
{
let regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$";
return new RegExp(regex).test(String(value).toLowerCase());
}
/**
* Attach a custom validation rule to the library.
*
*/
rule(name, closure)
{
Iodine.prototype[`assert${this._title(name)}`] = closure;
}
/**
* Replace the default error messages with a new set.
*
*/
setErrorMessages(messages)
{
this.messages = messages;
}
/**
* Add or replace an error message.
*
*/
setErrorMessage(key, message)
{
this.messages[key] = message;
}
/**
* Replace the default locale with a new value.
*
*/
setLocale(locale)
{
this.locale = locale;
}
/**
* Replace the default field name with a new value.
*
*/
setDefaultFieldName(fieldName)
{
this.default_field_name = fieldName;
}
}
/**
* Create an instance of the library.
*
*/
if (typeof window !== 'undefined') {
window.Iodine = new Iodine();
}