@carecard/validate 2.2.8 → 2.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/validate.js +17 -1
- package/lib/validateProperties.js +154 -139
- package/package.json +2 -2
package/lib/validate.js
CHANGED
|
@@ -185,6 +185,20 @@ const isValidDomainName = (domain) => {
|
|
|
185
185
|
return domainRegex.test(domain);
|
|
186
186
|
};
|
|
187
187
|
|
|
188
|
+
const isValidTimestampzString = (str) => {
|
|
189
|
+
if (typeof str !== 'string' || str.length === 0 || str.length > 64) return false;
|
|
190
|
+
// ISO 8601 for UTC or with Offset: 2023-10-27T10:00:00Z or 2023-10-27T10:00:00+02:00
|
|
191
|
+
const timestampzRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;
|
|
192
|
+
return timestampzRegex.test(str) && !isNaN(Date.parse(str));
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const isValidTimestampString = (str) => {
|
|
196
|
+
if (typeof str !== 'string' || str.length === 0 || str.length > 64) return false;
|
|
197
|
+
// ISO 8601 without time zone: 2023-10-27T10:00:00 or 2023-10-27T10:00:00.123
|
|
198
|
+
const timestampRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?$/;
|
|
199
|
+
return timestampRegex.test(str) && !isNaN(Date.parse(str));
|
|
200
|
+
};
|
|
201
|
+
|
|
188
202
|
module.exports = {
|
|
189
203
|
isImageUrl,
|
|
190
204
|
isInteger,
|
|
@@ -211,5 +225,7 @@ module.exports = {
|
|
|
211
225
|
isSafeString,
|
|
212
226
|
isInStringArray,
|
|
213
227
|
isCountryCodeString,
|
|
214
|
-
isValidDomainName
|
|
228
|
+
isValidDomainName,
|
|
229
|
+
isValidTimestampzString,
|
|
230
|
+
isValidTimestampString
|
|
215
231
|
};
|
|
@@ -1,152 +1,167 @@
|
|
|
1
1
|
// Validate the property values format.
|
|
2
2
|
const {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
isEmailString,
|
|
4
|
+
isPhoneNumber,
|
|
5
|
+
isUrlSafeString,
|
|
6
|
+
isValidUuidString,
|
|
7
|
+
isSimplePasswordString,
|
|
8
|
+
isPasswordString,
|
|
9
|
+
isString6To16CharacterLong,
|
|
10
|
+
isNameString,
|
|
11
|
+
isSafeSearchString,
|
|
12
|
+
isCharactersString,
|
|
13
|
+
isValidIntegerString,
|
|
14
|
+
isValidJsonString,
|
|
15
|
+
isImageUrl,
|
|
16
|
+
isValidDomainName,
|
|
17
|
+
isValidTimestampzString,
|
|
18
|
+
isValidTimestampString,
|
|
17
19
|
} = require('./validate');
|
|
18
20
|
|
|
19
21
|
function validateProperties(obj = {}) {
|
|
20
|
-
|
|
22
|
+
const returnObj = {};
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
24
|
+
for (const [key, value] of Object.entries(obj || {})) {
|
|
25
|
+
switch (key) {
|
|
26
|
+
case 'first_name':
|
|
27
|
+
case 'firstName':
|
|
28
|
+
case 'username':
|
|
29
|
+
case 'new_status':
|
|
30
|
+
case 'newStatus':
|
|
31
|
+
case 'description':
|
|
32
|
+
case 'comment':
|
|
33
|
+
case 'status':
|
|
34
|
+
case 'name':
|
|
35
|
+
case 'title':
|
|
36
|
+
case 'brand':
|
|
37
|
+
case 'short_description':
|
|
38
|
+
case 'shortDescription':
|
|
39
|
+
case 'college_name':
|
|
40
|
+
case 'collegeName':
|
|
41
|
+
case 'campus_name':
|
|
42
|
+
case 'campusName':
|
|
43
|
+
case 'role':
|
|
44
|
+
if (isNameString(value)) {
|
|
45
|
+
returnObj[key] = value;
|
|
46
|
+
}
|
|
47
|
+
break;
|
|
48
|
+
case 'search_string':
|
|
49
|
+
case 'searchString':
|
|
50
|
+
if (isSafeSearchString(value)) {
|
|
51
|
+
returnObj[key] = value;
|
|
52
|
+
}
|
|
53
|
+
break;
|
|
54
|
+
case 'password':
|
|
55
|
+
case 'new_password':
|
|
56
|
+
case 'newPassword':
|
|
57
|
+
if (isString6To16CharacterLong(value) && isSimplePasswordString(value)) {
|
|
58
|
+
returnObj[key] = value;
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
case 'strong_password':
|
|
62
|
+
case 'strongPassword':
|
|
63
|
+
if (isString6To16CharacterLong(value) && isPasswordString(value)) {
|
|
64
|
+
returnObj[key] = value;
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case 'email':
|
|
68
|
+
if (isEmailString(value)) {
|
|
69
|
+
returnObj[key] = value;
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
case 'phone_number':
|
|
73
|
+
case 'phoneNumber':
|
|
74
|
+
if (isPhoneNumber(value)) {
|
|
75
|
+
returnObj[key] = value;
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
case 'token':
|
|
79
|
+
case 'email_confirm_token':
|
|
80
|
+
case 'emailConfirmToken':
|
|
81
|
+
case 'verification_token':
|
|
82
|
+
case 'verificationToken':
|
|
83
|
+
if (isUrlSafeString(value)) {
|
|
84
|
+
returnObj[key] = value;
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
case 'uuid':
|
|
88
|
+
case 'item_id':
|
|
89
|
+
case 'user_id':
|
|
90
|
+
case 'image_id':
|
|
91
|
+
case 'itemId':
|
|
92
|
+
case 'userId':
|
|
93
|
+
case 'imageId':
|
|
94
|
+
case 'order_id':
|
|
95
|
+
case 'orderId':
|
|
96
|
+
case 'category_id':
|
|
97
|
+
case 'categoryId':
|
|
98
|
+
case 'parent_id':
|
|
99
|
+
case 'parentId':
|
|
100
|
+
case 'college_id':
|
|
101
|
+
case 'collegeId':
|
|
102
|
+
case 'campus_id':
|
|
103
|
+
case 'campusId':
|
|
104
|
+
case 'program_id':
|
|
105
|
+
case 'programId':
|
|
106
|
+
if (isValidUuidString(value)) {
|
|
107
|
+
returnObj[key] = value;
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
case 'period':
|
|
111
|
+
if (isCharactersString(value)) {
|
|
112
|
+
returnObj[key] = value;
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
case 'offset_number':
|
|
116
|
+
case 'offsetNumber':
|
|
117
|
+
case 'number_of_orders':
|
|
118
|
+
case 'numberOfOrders':
|
|
119
|
+
case 'price':
|
|
120
|
+
if (isValidIntegerString(value)) {
|
|
121
|
+
returnObj[key] = value;
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
case 'about':
|
|
125
|
+
if (isValidJsonString(value)) {
|
|
126
|
+
returnObj[key] = value.trim();
|
|
127
|
+
}
|
|
128
|
+
break;
|
|
129
|
+
case 'weight':
|
|
130
|
+
case 'dimensions':
|
|
131
|
+
case 'permission':
|
|
132
|
+
if (isValidJsonString(JSON.stringify(value))) {
|
|
133
|
+
returnObj[key] = value;
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
case 'image_url':
|
|
137
|
+
case 'imageUrl':
|
|
138
|
+
if (isImageUrl(value)) {
|
|
139
|
+
returnObj[key] = value;
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
case 'domain_name':
|
|
143
|
+
case 'domainName':
|
|
144
|
+
case 'domain':
|
|
145
|
+
case 'email_domain':
|
|
146
|
+
case 'emailDomain':
|
|
147
|
+
case 'email_domain_name':
|
|
148
|
+
case 'emailDomainName':
|
|
149
|
+
if (isValidDomainName(value)) {
|
|
150
|
+
returnObj[key] = value;
|
|
151
|
+
}
|
|
152
|
+
break;
|
|
153
|
+
case 'expires_at':
|
|
154
|
+
case 'expiresAt':
|
|
155
|
+
if (isValidTimestampzString(value) || isValidTimestampString(value)) {
|
|
156
|
+
returnObj[key] = value;
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
43
159
|
}
|
|
44
|
-
break;
|
|
45
|
-
case 'search_string':
|
|
46
|
-
case 'searchString':
|
|
47
|
-
if (isSafeSearchString(value)) {
|
|
48
|
-
returnObj[key] = value;
|
|
49
|
-
}
|
|
50
|
-
break;
|
|
51
|
-
case 'password':
|
|
52
|
-
case 'new_password':
|
|
53
|
-
case 'newPassword':
|
|
54
|
-
if (isString6To16CharacterLong(value) && isSimplePasswordString(value)) {
|
|
55
|
-
returnObj[key] = value;
|
|
56
|
-
}
|
|
57
|
-
break;
|
|
58
|
-
case 'strong_password':
|
|
59
|
-
case 'strongPassword':
|
|
60
|
-
if (isString6To16CharacterLong(value) && isPasswordString(value)) {
|
|
61
|
-
returnObj[key] = value;
|
|
62
|
-
}
|
|
63
|
-
break;
|
|
64
|
-
case 'email':
|
|
65
|
-
if (isEmailString(value)) {
|
|
66
|
-
returnObj[key] = value;
|
|
67
|
-
}
|
|
68
|
-
break;
|
|
69
|
-
case 'phone_number':
|
|
70
|
-
case 'phoneNumber':
|
|
71
|
-
if (isPhoneNumber(value)) {
|
|
72
|
-
returnObj[key] = value;
|
|
73
|
-
}
|
|
74
|
-
break;
|
|
75
|
-
case 'token':
|
|
76
|
-
case 'email_confirm_token':
|
|
77
|
-
case 'emailConfirmToken':
|
|
78
|
-
case 'verification_token':
|
|
79
|
-
case 'verificationToken':
|
|
80
|
-
if (isUrlSafeString(value)) {
|
|
81
|
-
returnObj[key] = value;
|
|
82
|
-
}
|
|
83
|
-
break;
|
|
84
|
-
case 'uuid':
|
|
85
|
-
case 'item_id':
|
|
86
|
-
case 'user_id':
|
|
87
|
-
case 'image_id':
|
|
88
|
-
case 'itemId':
|
|
89
|
-
case 'userId':
|
|
90
|
-
case 'imageId':
|
|
91
|
-
case 'order_id':
|
|
92
|
-
case 'orderId':
|
|
93
|
-
case 'category_id':
|
|
94
|
-
case 'categoryId':
|
|
95
|
-
case 'parent_id':
|
|
96
|
-
case 'parentId':
|
|
97
|
-
if (isValidUuidString(value)) {
|
|
98
|
-
returnObj[key] = value;
|
|
99
|
-
}
|
|
100
|
-
break;
|
|
101
|
-
case 'period':
|
|
102
|
-
if (isCharactersString(value)) {
|
|
103
|
-
returnObj[key] = value;
|
|
104
|
-
}
|
|
105
|
-
break;
|
|
106
|
-
case 'offset_number':
|
|
107
|
-
case 'offsetNumber':
|
|
108
|
-
case 'number_of_orders':
|
|
109
|
-
case 'numberOfOrders':
|
|
110
|
-
case 'price':
|
|
111
|
-
if (isValidIntegerString(value)) {
|
|
112
|
-
returnObj[key] = value;
|
|
113
|
-
}
|
|
114
|
-
break;
|
|
115
|
-
case 'about':
|
|
116
|
-
if (isValidJsonString(value)) {
|
|
117
|
-
returnObj[key] = value.trim();
|
|
118
|
-
}
|
|
119
|
-
break;
|
|
120
|
-
case 'weight':
|
|
121
|
-
case 'dimensions':
|
|
122
|
-
case 'permission':
|
|
123
|
-
if (isValidJsonString(JSON.stringify(value))) {
|
|
124
|
-
returnObj[key] = value;
|
|
125
|
-
}
|
|
126
|
-
break;
|
|
127
|
-
case 'image_url':
|
|
128
|
-
case 'imageUrl':
|
|
129
|
-
if (isImageUrl(value)) {
|
|
130
|
-
returnObj[key] = value;
|
|
131
|
-
}
|
|
132
|
-
break;
|
|
133
|
-
case 'domain_name':
|
|
134
|
-
case 'domainName':
|
|
135
|
-
case 'domain':
|
|
136
|
-
case 'email_domain':
|
|
137
|
-
case 'emailDomain':
|
|
138
|
-
case 'email_domain_name':
|
|
139
|
-
case 'emailDomainName':
|
|
140
|
-
if (isValidDomainName(value)) {
|
|
141
|
-
returnObj[key] = value;
|
|
142
|
-
}
|
|
143
|
-
break;
|
|
144
160
|
}
|
|
145
|
-
}
|
|
146
161
|
|
|
147
|
-
|
|
162
|
+
return returnObj;
|
|
148
163
|
}
|
|
149
164
|
|
|
150
165
|
module.exports = {
|
|
151
|
-
|
|
166
|
+
validateProperties,
|
|
152
167
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carecard/validate",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.11",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/CareCard-ca/pkg-validate.git"
|
|
@@ -17,6 +17,6 @@
|
|
|
17
17
|
"author": "CareCard team",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"mocha": "11.
|
|
20
|
+
"mocha": "11.7.5"
|
|
21
21
|
}
|
|
22
22
|
}
|