forked from ivanoff/2valid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.js
More file actions
91 lines (76 loc) · 3 KB
/
types.js
File metadata and controls
91 lines (76 loc) · 3 KB
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
/**** List of types to validate ****/
module.exports = {
string : { // string properties and methods
min : 0, // string.min Minimum length of the string
max : Infinity, // string.max Maximum length of the string
check : function( string ){ // string.check check sting type and size
return (( typeof string === 'string' || string instanceof String )
&& string.length >= this.min
&& string.length <= this.max
&& ( !this.match || string.match( this.match ) )
);
},
},
integer : { // number properties and methods
min : -Infinity, // number.min Minimum number value
max : Infinity, // number.max Maximum number value
check : function( number ){ // number.check check number type and size
return typeof number === 'number'
&& number >= this.min
&& number <= this.max
&& !`${number}`.match(/\./);
},
},
float : { // number properties and methods
min : -Infinity, // number.min Minimum number value
max : Infinity, // number.max Maximum number value
check : function( number ){ // number.check check number type and size
return typeof number === 'number'
&& number >= this.min
&& number <= this.max;
},
},
boolean : {
check : function( bool ){
return typeof bool === 'boolean';
},
},
date : { // date methods
check : function( date ){ // date.check Maximum length of the string
return date instanceof Date && typeof date.getMonth === 'function';
},
},
email : { // date methods
match : /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]+/i,
check : function( email ){ // date.check Maximum length of the string
return `${email}`.match( this.match );
},
},
password : {
min : 4, // minimum length of the password
max : Infinity,
// at least one caps and one small letter, digit and special
match : /^.*(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).*$/,
check : function( password ) { // check password type and size
return typeof password === 'string'
&& password.length >= this.min
&& password.length <= this.max
&& password.match( this.match )
},
},
md5 : {
match : /^[\da-f]{32}$/,
check : function( md5 ){
return md5 && `${md5}`.match( this.match );
},
},
uuid : { // uuid methods. uuid.check returns true if parameter looks like UUID, false otherwise
match : /^[\da-z]{8}-[\da-z]{4}-4[\da-z]{3}-[\da-z]{4}-[\da-z]{12}$/,
check : function( uuid ){
return uuid && `${uuid}`.match( this.match );
},
},
object : {
check : function(){ return 1 }
},
}