-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
674 lines (614 loc) · 16.1 KB
/
sound.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// --------------------------------------------------------------------------------------------------------------------
//
// sound - Make sure your data is sound! Schema generator, converter and validation library for Node.js.
//
// Copyright (c) 2013 Andrew Chilton
// - andychilton@gmail.com
// - https://chilts.org/
//
// License: http://chilts.mit-license.org/2013/
//
// --------------------------------------------------------------------------------------------------------------------
// npm
import _ from 'lodash'
import isemail from 'isemail'
const valid = {
boolean : {
'true' : true,
'false' : false,
't' : true,
'f' : false,
'yes' : true,
'no' : false,
'on' : true,
'off' : false,
'1' : true,
'0' : false,
},
}
const T_IS_STRING = 'isString'
const T_IS_INTEGER = 'isInteger'
const T_IS_FLOAT = 'isFloat'
const T_IS_BOOLEAN = 'isBoolean'
const T_IS_DATE = 'isDate'
const T_IS_OBJECT = 'isObject'
const T_IS_ARRAY = 'isArray'
const T_IS_URL = 'isUrl'
const T_IS_DOMAIN = 'isDomain'
const T_IS_EMAIL_ADDRESS = 'isEmailAddress'
const T_IS_TOKEN = 'isToken'
const T_IS_MATCH = 'isMatch'
const T_IS_ENUM = 'isEnum'
const T_TO_TRIM = 'toTrim'
const T_TO_LOWER_CASE = 'toLowerCase'
const T_TO_UPPER_CASE = 'toUpperCase'
const T_TO_REPLACE = 'toReplace'
const T_IS_EQUAL = 'isEqual'
const T_IS_NOT_EMPTY = 'isNotEmpty'
const T_IS_MIN_LEN = 'isMinLen'
const T_IS_MAX_LEN = 'isMaxLen'
const T_IS_MIN_VAL = 'isMinVal'
const T_IS_MAX_VAL = 'isMaxVal'
const T_IS_GREATER_THAN = 'isGreaterThan'
const T_IS_LESS_THAN = 'isLessThan'
const T_TO_STRING = 'toString'
const T_TO_INTEGER = 'toInteger'
const T_TO_FLOAT = 'toFloat'
const T_TO_BOOLEAN = 'toBoolean'
// --------------------------------------------------------------------------------------------------------------------
const Constraint = function(name) {
this._name = name
this._required = false
this._requiredMsg = undefined
this._default = undefined
this.rules = []
return this
}
Constraint.prototype.setName = function(_name) {
this._name = _name
return this
}
Constraint.prototype.isRequired = function(msg) {
this._required = true
this._requiredMsg = msg
return this
}
Constraint.prototype.hasDefault = function(val) {
this._default = val
return this
}
// --------------------------------------------------------------------------------------------------------------------
// validations
Constraint.prototype.isString = function(msg) {
this.rules.push({
type : T_IS_STRING,
msg : msg,
})
return this
}
Constraint.prototype.isInteger = function(msg) {
this.rules.push({
type : T_IS_INTEGER,
msg : msg,
})
return this
}
Constraint.prototype.isFloat = function(msg) {
this.rules.push({
type : T_IS_FLOAT,
msg : msg,
})
return this
}
Constraint.prototype.isBoolean = function(msg) {
this.rules.push({
type : T_IS_BOOLEAN,
msg : msg,
})
return this
}
Constraint.prototype.isDate = function(msg) {
this.rules.push({
type : T_IS_DATE,
msg : msg,
})
return this
}
Constraint.prototype.isObject = function(msg) {
this.rules.push({
type : T_IS_OBJECT,
msg : msg,
})
return this
}
Constraint.prototype.isArray = function(msg) {
this.rules.push({
type : T_IS_ARRAY,
msg : msg,
})
return this
}
Constraint.prototype.isEqual = function(value, msg) {
this.rules.push({
type : T_IS_EQUAL,
value : value,
msg : msg,
})
return this
}
Constraint.prototype.isLessThan = function(value, msg) {
this.rules.push({
type : T_IS_LESS_THAN,
value : value,
msg : msg,
})
return this
}
Constraint.prototype.isGreaterThan = function(value, msg) {
this.rules.push({
type : T_IS_GREATER_THAN,
value : value,
msg : msg,
})
return this
}
Constraint.prototype.isMinVal = function(value, msg) {
this.rules.push({
type : T_IS_MIN_VAL,
value : value,
msg : msg,
})
return this
}
Constraint.prototype.isMaxVal = function(value, msg) {
this.rules.push({
type : T_IS_MAX_VAL,
value : value,
msg : msg,
})
return this
}
Constraint.prototype.isNotEmpty = function(msg) {
this.rules.push({
type : T_IS_NOT_EMPTY,
msg : msg,
})
return this
}
Constraint.prototype.isMinLen = function(len, msg) {
this.rules.push({
type : T_IS_MIN_LEN,
len : len,
msg : msg,
})
return this
}
Constraint.prototype.isMaxLen = function(len, msg) {
this.rules.push({
type : T_IS_MAX_LEN,
len : len,
msg : msg,
})
return this
}
Constraint.prototype.isMatch = function(regex, msg) {
this.rules.push({
type : T_IS_MATCH,
regex : regex,
msg : msg,
})
return this
}
Constraint.prototype.isEnum = function(values, msg) {
const value = {}
values.forEach(v => value[v] = true)
this.rules.push({
type : T_IS_ENUM,
values : values,
value : value,
msg : msg,
})
return this
}
Constraint.prototype.isUrl = function(msg) {
this.rules.push({
type : T_IS_URL,
msg : msg,
})
return this
}
Constraint.prototype.isDomain = function(msg) {
this.rules.push({
type : T_IS_DOMAIN,
msg : msg,
})
return this
}
Constraint.prototype.isEmailAddress = function(msg) {
this.rules.push({
type : T_IS_EMAIL_ADDRESS,
msg : msg,
})
return this
}
Constraint.prototype.isToken = function(msg) {
this.rules.push({
type : T_IS_TOKEN,
msg : msg,
})
return this
}
Constraint.prototype._inject = function(item) {
this.rules.push(item)
return this
}
// --------------------------------------------------------------------------------------------------------------------
// conversions
Constraint.prototype.toTrim = function() {
this.rules.push({
type : T_TO_TRIM,
})
return this
}
Constraint.prototype.toLowerCase = function() {
this.rules.push({
type : T_TO_LOWER_CASE,
})
return this
}
Constraint.prototype.toUpperCase = function() {
this.rules.push({
type : T_TO_UPPER_CASE,
})
return this
}
Constraint.prototype.toReplace = function(a, b) {
this.rules.push({
type : T_TO_REPLACE,
a : a,
b : b,
})
return this
}
// --------------------------------------------------------------------------------------------------------------------
// coercions
Constraint.prototype.toString = function(msg) {
this.rules.push({
type : T_TO_STRING,
msg : msg,
})
return this
}
Constraint.prototype.toInteger = function(msg) {
this.rules.push({
type : T_TO_INTEGER,
msg : msg,
})
return this
}
Constraint.prototype.toFloat = function(msg) {
this.rules.push({
type : T_TO_FLOAT,
msg : msg,
})
return this
}
Constraint.prototype.toBoolean = function(msg) {
this.rules.push({
type : T_TO_BOOLEAN,
msg : msg,
})
return this
}
// --------------------------------------------------------------------------------------------------------------------
// sound itself (for the constraints) and it's only function for validation
const sound = function(name) {
return new Constraint(name)
}
sound.validate = function(arg, schema) {
const val = {}
const err = {}
let ok = true
// go through each property of the schema (we ignore all others)
const keys = Object.keys(schema)
keys.forEach(function(key) {
// validate the arg against it's own constraints in the schema
// Note: sound.validateParam(name, value, schema, fn)
const validation = validateParam(schema[key]._name || key, arg[key], schema[key])
if ( validation.ok ) {
// don't copy over nulls which appear if the arg doesn't have it
if (typeof validation.val !== 'undefined' ) {
val[key] = validation.val
}
}
else {
ok = false
err[key] = validation.err
}
})
return {
ok : ok,
arg : arg,
val : val,
err : err,
}
}
const validateParam = function(name, value, constraint) {
// first thing to do is see if this param is empty, has a default, and is required
if ( _.isUndefined(value) || _.isNull(value) || value === '' ) {
// check to see if a default was provided and set it
if ( _.isUndefined(constraint._default) ) {
// no default provided, now check if it is required
if ( constraint._required ) {
// yes, required, so this fails
return {
ok : false,
err : constraint._requiredMsg || name + ' is required',
}
}
else {
// not required, so this is okay
return {
ok : true,
}
}
}
else {
// we have a default, so set the value for later checking
value = constraint._default
}
}
// else, we have a value, so keep checking things
// now, loop through all the constraints
for ( let i = 0; i < constraint.rules.length; i++ ) {
const r = constraint.rules[i]
// check all of the different constraints
if ( r.type === T_IS_STRING ) {
if ( !_.isString(value) ) {
return {
ok : false,
err : r.msg || name + ' should be a string',
}
}
}
else if ( r.type === T_IS_INTEGER ) {
if ( parseInt(value, 10) !== value ) {
return {
ok : false,
err : r.msg || name + ' should be an integer',
}
}
}
else if ( r.type === T_IS_FLOAT ) {
if ( typeof value !== 'number' ) {
return {
ok : false,
err : r.msg || name + ' should be a float',
}
}
}
else if ( r.type === T_IS_BOOLEAN ) {
if ( !_.isBoolean(value) ) {
return {
ok : false,
err : r.msg || name + ' should be a boolean',
}
}
}
else if ( r.type === T_IS_DATE ) {
if ( !_.isDate(value) ) {
return {
ok : false,
err : r.msg || name + ' should be a date',
}
}
}
else if ( r.type === T_IS_OBJECT ) {
if ( !_.isPlainObject(value) ) {
return {
ok : false,
err : r.msg || name + ' should be an object',
}
}
}
else if ( r.type === T_IS_ARRAY ) {
if ( !_.isArray(value) ) {
return {
ok : false,
err : r.msg || name + ' should be an array',
}
}
}
else if ( r.type === T_IS_EQUAL ) {
if ( value !== r.value ) {
return {
ok : false,
err : r.msg || name + ' should be ' + r.value,
}
}
}
else if ( r.type === T_IS_GREATER_THAN ) {
if ( value <= r.value ) {
return {
ok : false,
err : r.msg || name + ' should be greater than ' + r.value,
}
}
}
else if ( r.type === T_IS_LESS_THAN ) {
if ( value >= r.value ) {
return {
ok : false,
err : r.msg || name + ' should be less than ' + r.value,
}
}
}
else if ( r.type === T_IS_MIN_VAL ) {
if ( value < r.value ) {
return {
ok : false,
err : r.msg || name + ' should be at least ' + r.value,
}
}
}
else if ( r.type === T_IS_MAX_VAL ) {
if ( value > r.value ) {
return {
ok : false,
err : r.msg || name + ' should be at most ' + r.value,
}
}
}
else if ( r.type === T_IS_NOT_EMPTY ) {
if ( value.trim() === '' ) {
return {
ok : false,
err : r.msg || name + ' should be provided',
}
}
}
else if ( r.type === T_IS_MIN_LEN ) {
if ( value.length < r.len ) {
return {
ok : false,
err : r.msg || name + ' should be at least ' + r.len + ' characters',
}
}
}
else if ( r.type === T_IS_MAX_LEN ) {
if ( value.length > r.len ) {
return {
ok : false,
err : r.msg || name + ' should be at most ' + r.len + ' characters',
}
}
}
else if ( r.type === T_IS_MATCH ) {
if ( !value.match(r.regex) ) {
return {
ok : false,
err : r.msg || name + ' is not valid',
}
}
}
else if ( r.type === T_IS_ENUM ) {
if ( !(value in r.value) ) {
return {
ok : false,
err : r.msg || name + ' is not a valid value',
}
}
}
else if ( r.type === T_IS_URL ) {
// ToDo: http://someweblog.com/url-regular-expression-javascript-link-shortener/
// From: http://stackoverflow.com/questions/8188645/javascript-regex-to-match-a-url-in-a-field-of-text
// * (http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?
if ( !value.match(/^https?:\/\/[A-Za-z0-9][A-Za-z0-9-]*(\.[A-Za-z]+)+(:\d+)?(\/\S*)?$/) ) {
return {
ok : false,
err : r.msg || name + ' should be a URL and start with http:// or https://',
}
}
}
else if ( r.type === T_IS_DOMAIN ) {
// Copy part of the above T_IS_URL
if ( !value.match(/^([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}$/) ) {
return {
ok : false,
err : r.msg || name + ' should be a FQDN such as example.com or my.example.org',
}
}
}
else if ( r.type === T_IS_EMAIL_ADDRESS ) {
// From npm.im/isemail, we're going to include TLD weirdnesses, but not quoted usernames or address literals.
//
// rfc5321TLD: 9,
// rfc5321TLDNumeric: 10,
// rfc5321QuotedString: 11,
// rfc5321AddressLiteral: 12,
if ( isemail.validate(value, { errorLevel: 10 }) !== 0 ) {
return {
ok : false,
err : r.msg || name + ' should be an Email Address',
}
}
}
else if ( r.type === T_IS_TOKEN ) {
// Tokens are like URI segments. Lowercase letters, numbers and dashes.
if ( !value.match(/^[a-z0-9][a-z0-9-]*[a-z0-9]$/) ) {
return {
ok : false,
err : r.msg || name + ' should start and end with letters/numbers and contain only lowercase letters, numbers and dashes',
}
}
}
else if ( r.type === T_TO_TRIM ) {
value = value.replace(/^\s+|\s+$/g, '')
}
else if ( r.type === T_TO_LOWER_CASE ) {
value = value.toLowerCase()
}
else if ( r.type === T_TO_UPPER_CASE ) {
value = value.toUpperCase()
}
else if ( r.type === T_TO_REPLACE ) {
value = value.replace(r.a, r.b)
}
else if ( r.type === T_TO_STRING ) {
value = String(value)
}
else if ( r.type === T_TO_INTEGER ) {
value = parseInt(value, 10)
if ( Number.isNaN(value) ) {
return {
ok : false,
err : r.msg || name + ' could not be converted to an integer',
}
}
}
else if ( r.type === T_TO_FLOAT ) {
value = parseFloat(value)
if ( Number.isNaN(value) ) {
return {
ok : false,
err : r.msg || name + ' could not be converted to a float',
}
}
}
else if ( r.type === T_TO_BOOLEAN ) {
if ( _.isString(value) ) {
if ( value.toLowerCase() in valid.boolean ) {
value = valid.boolean[value.toLowerCase()]
}
else {
// do nothing, the user needs to validate their input prior to this conversion
return {
ok : false,
err : r.msg || name + ' is not a recognised value when converting to boolean',
}
}
}
else if ( _.isNumber(value) ) {
value = !(value == 0)
}
else {
// do nothing, the user needs to validate their input prior to this conversion
return {
ok : false,
err : r.msg || name + ' value should be a string or integer when converting to boolean',
}
}
}
else {
throw new Error('Program Error: unknown type ' + r.type)
}
}
// no error, so just return nothing
return {
ok : true,
val : value,
}
}
// --------------------------------------------------------------------------------------------------------------------
export default sound
// --------------------------------------------------------------------------------------------------------------------