@carecard/validate-ts 2.1.1 → 2.2.3

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.
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./validate"), exports);
18
- __exportStar(require("./validateProperties"), exports);
17
+ __exportStar(require("./validate.cjs"), exports);
18
+ __exportStar(require("./validateProperties.cjs"), exports);
@@ -3,7 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isValidDomainName = exports.isCountryCodeString = exports.isInStringArray = exports.isSafeString = exports.isPostalCodeString = exports.isBoolValue = exports.isProvinceString = exports.isString6To16CharacterLong = exports.isString6To24CharacterLong = exports.isUrlSafeString = exports.isUsernameString = exports.isSimplePasswordStringFailureMessage = exports.isPasswordStringFailureMessage = exports.isSimplePasswordString = exports.isPasswordString = exports.isJwtString = exports.isEmailString = exports.isSafeSearchString = exports.isNameString = exports.isCharactersString = exports.isValidUuidString = exports.isValidIntegerString = exports.isValidJsonString = exports.isInteger = exports.isImageUrl = void 0;
4
4
  exports.isPhoneNumber = isPhoneNumber;
5
5
  const isImageUrl = (imageUrl) => {
6
- if (imageUrl === undefined || typeof imageUrl !== 'string' || imageUrl.length === 0)
6
+ if (imageUrl === undefined ||
7
+ typeof imageUrl !== 'string' ||
8
+ imageUrl.length === 0 ||
9
+ imageUrl.length > 2048)
7
10
  return false;
8
11
  const imageUrlRegex = /^[a-zA-Z0-9-_./]+$/;
9
12
  return imageUrlRegex.test(imageUrl);
@@ -14,21 +17,21 @@ const isInteger = (number) => {
14
17
  };
15
18
  exports.isInteger = isInteger;
16
19
  const isValidJsonString = (str) => {
17
- if (str === undefined || typeof str !== 'string' || str.length === 0)
20
+ if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 10000)
18
21
  return false;
19
22
  try {
20
23
  const json = JSON.parse(str);
21
- return typeof json === 'object';
24
+ return typeof json === 'object' && json !== null;
22
25
  }
23
- catch (error) {
26
+ catch {
24
27
  return false;
25
28
  }
26
29
  };
27
30
  exports.isValidJsonString = isValidJsonString;
28
31
  const isValidIntegerString = (str) => {
29
- if (str === undefined || typeof str !== 'string' || str.length === 0)
32
+ if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 20)
30
33
  return false;
31
- return /^\d+$/g.test(str);
34
+ return /^\d+$/.test(str);
32
35
  };
33
36
  exports.isValidIntegerString = isValidIntegerString;
34
37
  const isValidUuidString = (str) => {
@@ -38,51 +41,49 @@ const isValidUuidString = (str) => {
38
41
  };
39
42
  exports.isValidUuidString = isValidUuidString;
40
43
  const isCharactersString = (str) => {
41
- if (str === undefined || typeof str !== 'string' || str.length === 0)
44
+ if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 1000)
42
45
  return false;
43
46
  return /^[\da-zA-Z _-]+$/.test(str);
44
47
  };
45
48
  exports.isCharactersString = isCharactersString;
46
49
  const isNameString = (str) => {
47
- if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 1000) {
50
+ if (typeof str !== 'string' || str.length === 0 || str.length > 1000) {
48
51
  return false;
49
52
  }
50
53
  return /^[A-Za-z][0-9a-zA-Z-_.,'() ]+$/.test(str.trim());
51
54
  };
52
55
  exports.isNameString = isNameString;
53
56
  const isSafeSearchString = (str) => {
54
- if (str === undefined || typeof str !== 'string' || str.length === 0)
57
+ if (typeof str !== 'string' || str.length === 0)
55
58
  return false;
56
59
  return /^[A-Za-z][0-9a-zA-Z\-_.,'()@ ]{1,100}$/.test(str.trim());
57
60
  };
58
61
  exports.isSafeSearchString = isSafeSearchString;
59
62
  const isEmailString = (email) => {
60
- if (email === undefined ||
61
- typeof email !== 'string' ||
62
- email.length === 0 ||
63
- email.length > 1000) {
63
+ if (typeof email !== 'string' || email.length === 0 || email.length > 320) {
64
64
  return false;
65
65
  }
66
- const regExpEmail = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
67
- return regExpEmail.test(String(email).toLowerCase());
66
+ // Simplified safe email regex
67
+ const regExpEmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-zA-Z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/;
68
+ return regExpEmail.test(email);
68
69
  };
69
70
  exports.isEmailString = isEmailString;
70
71
  const isJwtString = (jwt) => {
71
- if (jwt === '' || jwt.trim() === '')
72
+ if (typeof jwt !== 'string' || jwt.length === 0 || jwt.length > 8192 || jwt.trim() === '')
72
73
  return false;
73
74
  const jwtRegex = /^eyJ[a-zA-Z0-9-_.]+$/;
74
75
  return jwtRegex.test(jwt);
75
76
  };
76
77
  exports.isJwtString = isJwtString;
77
78
  const isPasswordString = (password) => {
78
- if (password === undefined || typeof password !== 'string' || password.length === 0)
79
+ if (typeof password !== 'string' || password.length === 0 || password.length > 128)
79
80
  return false;
80
81
  const regExPassword = /^(?=.*[a-zA-Z0-9])(?=.*[!@#$%^&*_-])[a-zA-Z0-9!@#$%^&*_-]{6,32}$/;
81
82
  return regExPassword.test(String(password));
82
83
  };
83
84
  exports.isPasswordString = isPasswordString;
84
85
  const isSimplePasswordString = (password) => {
85
- if (password === undefined || typeof password !== 'string' || password.length === 0)
86
+ if (typeof password !== 'string' || password.length === 0 || password.length > 128)
86
87
  return false;
87
88
  const regExPassword = /^[a-zA-Z0-9!@#$%^&*_-]{6,32}$/;
88
89
  return regExPassword.test(String(password));
@@ -98,7 +99,7 @@ const isPasswordStringFailureMessage = (password) => {
98
99
  };
99
100
  exports.isPasswordStringFailureMessage = isPasswordStringFailureMessage;
100
101
  const isSimplePasswordStringFailureMessage = (password) => {
101
- if (!(0, exports.isPasswordString)(password)) {
102
+ if (!(0, exports.isSimplePasswordString)(password)) {
102
103
  return 'Total 6 to 32 characters, numbers or !@#$%^&*_-';
103
104
  }
104
105
  else {
@@ -107,31 +108,34 @@ const isSimplePasswordStringFailureMessage = (password) => {
107
108
  };
108
109
  exports.isSimplePasswordStringFailureMessage = isSimplePasswordStringFailureMessage;
109
110
  const isUsernameString = (str) => {
110
- if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 200)
111
+ if (typeof str !== 'string' || str.length === 0 || str.length > 200)
111
112
  return false;
112
113
  return /^[0-9a-zA-Z]+$/.test(str);
113
114
  };
114
115
  exports.isUsernameString = isUsernameString;
115
116
  function isPhoneNumber(str) {
116
- if (str === undefined || typeof str !== 'string' || str.length === 0)
117
+ if (typeof str !== 'string' || str.length === 0 || str.length > 20)
117
118
  return false;
118
119
  return /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/.test(str);
119
120
  }
120
121
  const isUrlSafeString = (inputString) => {
121
- if (inputString === '' || inputString.trim() === '')
122
+ if (typeof inputString !== 'string' ||
123
+ inputString.length === 0 ||
124
+ inputString.length > 2048 ||
125
+ inputString.trim() === '')
122
126
  return false;
123
- const jwtRegex = /^[a-zA-Z0-9-_.]+$/;
124
- return jwtRegex.test(inputString);
127
+ const urlSafeRegex = /^[a-zA-Z0-9-_.]+$/;
128
+ return urlSafeRegex.test(inputString);
125
129
  };
126
130
  exports.isUrlSafeString = isUrlSafeString;
127
131
  const isString6To24CharacterLong = (password) => {
128
- if (password === undefined || typeof password !== 'string' || password.length === 0)
132
+ if (typeof password !== 'string' || password.length === 0)
129
133
  return false;
130
134
  return 6 <= password.length && password.length <= 24;
131
135
  };
132
136
  exports.isString6To24CharacterLong = isString6To24CharacterLong;
133
137
  const isString6To16CharacterLong = (password) => {
134
- if (password === undefined || typeof password !== 'string' || password.length === 0)
138
+ if (typeof password !== 'string' || password.length === 0)
135
139
  return false;
136
140
  return 6 <= password.length && password.length <= 16;
137
141
  };
@@ -139,48 +143,45 @@ exports.isString6To16CharacterLong = isString6To16CharacterLong;
139
143
  const isProvinceString = (inputString) => {
140
144
  const provinces = ['on', 'qc'];
141
145
  if ((0, exports.isNameString)(inputString)) {
142
- return provinces.includes(inputString.toLowerCase());
146
+ return provinces.includes(inputString.toLowerCase().trim());
143
147
  }
144
148
  return false;
145
149
  };
146
150
  exports.isProvinceString = isProvinceString;
147
151
  const isBoolValue = (inputValue) => {
148
- return !(inputValue === undefined || typeof inputValue !== 'boolean');
152
+ return typeof inputValue === 'boolean';
149
153
  };
150
154
  exports.isBoolValue = isBoolValue;
151
155
  const isPostalCodeString = (inputString) => {
152
156
  const postalCodeRegex = /^(?!.*[DFIOQU])[A-VXY][0-9][A-Z] ?[0-9][A-Z][0-9]$/i;
153
157
  if ((0, exports.isNameString)(inputString)) {
154
- return postalCodeRegex.test(inputString);
158
+ return postalCodeRegex.test(inputString.trim());
155
159
  }
156
160
  return false;
157
161
  };
158
162
  exports.isPostalCodeString = isPostalCodeString;
159
163
  const isSafeString = (str) => {
160
- if (str === undefined || typeof str !== 'string' || str.length === 0)
164
+ if (typeof str !== 'string' || str.length === 0 || str.length > 10000)
161
165
  return false;
162
- return /^[\da-zA-Z-_.,#*'()\[\]: ]+$/.test(str);
166
+ return /^[\da-zA-Z-_.,#*'()[\]: ]+$/.test(str);
163
167
  };
164
168
  exports.isSafeString = isSafeString;
165
169
  const isInStringArray = (StringArray, inputString) => {
166
170
  if ((0, exports.isNameString)(inputString)) {
167
- return StringArray.includes(inputString.toLowerCase());
171
+ return StringArray.includes(inputString.toLowerCase().trim());
168
172
  }
169
173
  return false;
170
174
  };
171
175
  exports.isInStringArray = isInStringArray;
172
176
  const isCountryCodeString = (str) => {
173
- if (str === undefined || typeof str !== 'string' || str.length === 0)
177
+ if (typeof str !== 'string' || str.length === 0 || str.length > 4)
174
178
  return false;
175
- const postalCodeRegex = /^\+[1-9][0-9]{0,2}$/i;
176
- return postalCodeRegex.test(str);
179
+ const countryCodeRegex = /^\+[1-9][0-9]{0,2}$/;
180
+ return countryCodeRegex.test(str);
177
181
  };
178
182
  exports.isCountryCodeString = isCountryCodeString;
179
183
  const isValidDomainName = (domain) => {
180
- if (domain === undefined ||
181
- typeof domain !== 'string' ||
182
- domain.length === 0 ||
183
- domain.length > 253)
184
+ if (typeof domain !== 'string' || domain.length === 0 || domain.length > 253)
184
185
  return false;
185
186
  const domainRegex = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i;
186
187
  return domainRegex.test(domain);
@@ -1,26 +1,26 @@
1
- export declare const isImageUrl: (imageUrl: string | any[]) => boolean;
2
- export declare const isInteger: (number: number | string) => boolean;
3
- export declare const isValidJsonString: (str: string | number | any[]) => boolean;
4
- export declare const isValidIntegerString: (str: string | number | any[]) => boolean;
5
- export declare const isValidUuidString: (str: string | number | any[]) => boolean;
6
- export declare const isCharactersString: (str: string | number | any[]) => boolean;
7
- export declare const isNameString: (str: string) => boolean;
8
- export declare const isSafeSearchString: (str: string) => boolean;
9
- export declare const isEmailString: (email: string | any[]) => boolean;
10
- export declare const isJwtString: (jwt: string) => boolean;
11
- export declare const isPasswordString: (password: string | any[]) => boolean;
12
- export declare const isSimplePasswordString: (password: string | any[]) => boolean;
13
- export declare const isPasswordStringFailureMessage: (password: string | any[]) => "Total 6 to 32 characters, numbers and one of !@#$%^&*_-" | null;
14
- export declare const isSimplePasswordStringFailureMessage: (password: string | any[]) => "Total 6 to 32 characters, numbers or !@#$%^&*_-" | null;
15
- export declare const isUsernameString: (str: string | any[]) => boolean;
16
- export declare function isPhoneNumber(str: string | any[]): boolean;
17
- export declare const isUrlSafeString: (inputString: string) => boolean;
18
- export declare const isString6To24CharacterLong: (password: string | any[]) => boolean;
19
- export declare const isString6To16CharacterLong: (password: string | any[]) => boolean;
20
- export declare const isProvinceString: (inputString: string) => boolean;
21
- export declare const isBoolValue: (inputValue: any) => inputValue is boolean;
22
- export declare const isPostalCodeString: (inputString: string) => boolean;
23
- export declare const isSafeString: (str: string | any[]) => boolean;
24
- export declare const isInStringArray: (StringArray: string | any[], inputString: string) => boolean;
25
- export declare const isCountryCodeString: (str: string | any[]) => boolean;
26
- export declare const isValidDomainName: (domain: string) => boolean;
1
+ export declare const isImageUrl: (imageUrl: unknown) => boolean;
2
+ export declare const isInteger: (number: unknown) => boolean;
3
+ export declare const isValidJsonString: (str: unknown) => boolean;
4
+ export declare const isValidIntegerString: (str: unknown) => boolean;
5
+ export declare const isValidUuidString: (str: unknown) => boolean;
6
+ export declare const isCharactersString: (str: unknown) => boolean;
7
+ export declare const isNameString: (str: unknown) => boolean;
8
+ export declare const isSafeSearchString: (str: unknown) => boolean;
9
+ export declare const isEmailString: (email: unknown) => boolean;
10
+ export declare const isJwtString: (jwt: unknown) => boolean;
11
+ export declare const isPasswordString: (password: unknown) => boolean;
12
+ export declare const isSimplePasswordString: (password: unknown) => boolean;
13
+ export declare const isPasswordStringFailureMessage: (password: unknown) => "Total 6 to 32 characters, numbers and one of !@#$%^&*_-" | null;
14
+ export declare const isSimplePasswordStringFailureMessage: (password: unknown) => "Total 6 to 32 characters, numbers or !@#$%^&*_-" | null;
15
+ export declare const isUsernameString: (str: unknown) => boolean;
16
+ export declare function isPhoneNumber(str: unknown): boolean;
17
+ export declare const isUrlSafeString: (inputString: unknown) => boolean;
18
+ export declare const isString6To24CharacterLong: (password: unknown) => boolean;
19
+ export declare const isString6To16CharacterLong: (password: unknown) => boolean;
20
+ export declare const isProvinceString: (inputString: unknown) => boolean;
21
+ export declare const isBoolValue: (inputValue: unknown) => inputValue is boolean;
22
+ export declare const isPostalCodeString: (inputString: unknown) => boolean;
23
+ export declare const isSafeString: (str: unknown) => boolean;
24
+ export declare const isInStringArray: (StringArray: string[], inputString: unknown) => boolean;
25
+ export declare const isCountryCodeString: (str: unknown) => boolean;
26
+ export declare const isValidDomainName: (domain: unknown) => boolean;
@@ -2,13 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateProperties = validateProperties;
4
4
  // Validate the property values format.
5
- const validate_1 = require("./validate");
5
+ const validate_1 = require("./validate.cjs");
6
6
  function validateProperties(obj = {}) {
7
- let returnObj = {};
8
- for (let [key, value] of Object.entries(obj || {})) {
7
+ const returnObj = {};
8
+ for (const [key, value] of Object.entries(obj || {})) {
9
9
  switch (key) {
10
10
  case 'first_name':
11
+ case 'firstName':
11
12
  case 'username':
13
+ case 'new_status':
12
14
  case 'newStatus':
13
15
  case 'description':
14
16
  case 'comment':
@@ -17,33 +19,49 @@ function validateProperties(obj = {}) {
17
19
  case 'title':
18
20
  case 'brand':
19
21
  case 'short_description':
20
- (0, validate_1.isNameString)(value) ? (returnObj[key] = value) : null;
22
+ case 'shortDescription':
23
+ if ((0, validate_1.isNameString)(value)) {
24
+ returnObj[key] = value;
25
+ }
21
26
  break;
22
27
  case 'search_string':
23
28
  case 'searchString':
24
- (0, validate_1.isSafeSearchString)(value) ? (returnObj[key] = value) : null;
29
+ if ((0, validate_1.isSafeSearchString)(value)) {
30
+ returnObj[key] = value;
31
+ }
25
32
  break;
26
33
  case 'password':
27
34
  case 'new_password':
28
- (0, validate_1.isString6To16CharacterLong)(value) && (0, validate_1.isSimplePasswordString)(value)
29
- ? (returnObj[key] = value)
30
- : null;
35
+ case 'newPassword':
36
+ if ((0, validate_1.isString6To16CharacterLong)(value) && (0, validate_1.isSimplePasswordString)(value)) {
37
+ returnObj[key] = value;
38
+ }
31
39
  break;
32
40
  case 'strong_password':
33
- (0, validate_1.isString6To16CharacterLong)(value) && (0, validate_1.isPasswordString)(value)
34
- ? (returnObj[key] = value)
35
- : null;
41
+ case 'strongPassword':
42
+ if ((0, validate_1.isString6To16CharacterLong)(value) && (0, validate_1.isPasswordString)(value)) {
43
+ returnObj[key] = value;
44
+ }
36
45
  break;
37
46
  case 'email':
38
- (0, validate_1.isEmailString)(value) ? (returnObj[key] = value) : null;
47
+ if ((0, validate_1.isEmailString)(value)) {
48
+ returnObj[key] = value;
49
+ }
39
50
  break;
40
51
  case 'phone_number':
41
- (0, validate_1.isPhoneNumber)(value) ? (returnObj[key] = value) : null;
52
+ case 'phoneNumber':
53
+ if ((0, validate_1.isPhoneNumber)(value)) {
54
+ returnObj[key] = value;
55
+ }
42
56
  break;
43
57
  case 'token':
44
58
  case 'email_confirm_token':
59
+ case 'emailConfirmToken':
45
60
  case 'verification_token':
46
- (0, validate_1.isUrlSafeString)(value) ? (returnObj[key] = value) : null;
61
+ case 'verificationToken':
62
+ if ((0, validate_1.isUrlSafeString)(value)) {
63
+ returnObj[key] = value;
64
+ }
47
65
  break;
48
66
  case 'uuid':
49
67
  case 'item_id':
@@ -51,35 +69,59 @@ function validateProperties(obj = {}) {
51
69
  case 'image_id':
52
70
  case 'itemId':
53
71
  case 'userId':
72
+ case 'imageId':
73
+ case 'order_id':
54
74
  case 'orderId':
55
75
  case 'category_id':
76
+ case 'categoryId':
56
77
  case 'parent_id':
57
- (0, validate_1.isValidUuidString)(value) ? (returnObj[key] = value) : null;
78
+ case 'parentId':
79
+ if ((0, validate_1.isValidUuidString)(value)) {
80
+ returnObj[key] = value;
81
+ }
58
82
  break;
59
83
  case 'period':
60
- (0, validate_1.isCharactersString)(value) ? (returnObj[key] = value) : null;
84
+ if ((0, validate_1.isCharactersString)(value)) {
85
+ returnObj[key] = value;
86
+ }
61
87
  break;
62
88
  case 'offset_number':
89
+ case 'offsetNumber':
63
90
  case 'number_of_orders':
91
+ case 'numberOfOrders':
64
92
  case 'price':
65
- (0, validate_1.isValidIntegerString)(value) ? (returnObj[key] = value) : null;
93
+ if ((0, validate_1.isValidIntegerString)(value)) {
94
+ returnObj[key] = value;
95
+ }
66
96
  break;
67
97
  case 'about':
68
- (0, validate_1.isValidJsonString)(value) ? (returnObj[key] = value.trim()) : null;
98
+ if ((0, validate_1.isValidJsonString)(value)) {
99
+ returnObj[key] = value.trim();
100
+ }
69
101
  break;
70
102
  case 'weight':
71
103
  case 'dimensions':
72
104
  case 'permission':
73
- (0, validate_1.isValidJsonString)(JSON.stringify(value)) ? (returnObj[key] = value) : null;
105
+ if ((0, validate_1.isValidJsonString)(JSON.stringify(value))) {
106
+ returnObj[key] = value;
107
+ }
74
108
  break;
75
109
  case 'image_url':
76
- (0, validate_1.isImageUrl)(value) ? (returnObj[key] = value) : null;
110
+ case 'imageUrl':
111
+ if ((0, validate_1.isImageUrl)(value)) {
112
+ returnObj[key] = value;
113
+ }
77
114
  break;
78
115
  case 'domain_name':
116
+ case 'domainName':
79
117
  case 'domain':
80
118
  case 'email_domain':
119
+ case 'emailDomain':
81
120
  case 'email_domain_name':
82
- (0, validate_1.isValidDomainName)(value) ? (returnObj[key] = value) : null;
121
+ case 'emailDomainName':
122
+ if ((0, validate_1.isValidDomainName)(value)) {
123
+ returnObj[key] = value;
124
+ }
83
125
  break;
84
126
  }
85
127
  }
@@ -1,5 +1,5 @@
1
1
  export declare function validateProperties(obj?: {
2
- [key: string]: string | any;
2
+ [key: string]: unknown;
3
3
  }): {
4
- [key: string]: any;
4
+ [key: string]: unknown;
5
5
  };
@@ -1,26 +1,26 @@
1
- export declare const isImageUrl: (imageUrl: string | any[]) => boolean;
2
- export declare const isInteger: (number: number | string) => boolean;
3
- export declare const isValidJsonString: (str: string | number | any[]) => boolean;
4
- export declare const isValidIntegerString: (str: string | number | any[]) => boolean;
5
- export declare const isValidUuidString: (str: string | number | any[]) => boolean;
6
- export declare const isCharactersString: (str: string | number | any[]) => boolean;
7
- export declare const isNameString: (str: string) => boolean;
8
- export declare const isSafeSearchString: (str: string) => boolean;
9
- export declare const isEmailString: (email: string | any[]) => boolean;
10
- export declare const isJwtString: (jwt: string) => boolean;
11
- export declare const isPasswordString: (password: string | any[]) => boolean;
12
- export declare const isSimplePasswordString: (password: string | any[]) => boolean;
13
- export declare const isPasswordStringFailureMessage: (password: string | any[]) => "Total 6 to 32 characters, numbers and one of !@#$%^&*_-" | null;
14
- export declare const isSimplePasswordStringFailureMessage: (password: string | any[]) => "Total 6 to 32 characters, numbers or !@#$%^&*_-" | null;
15
- export declare const isUsernameString: (str: string | any[]) => boolean;
16
- export declare function isPhoneNumber(str: string | any[]): boolean;
17
- export declare const isUrlSafeString: (inputString: string) => boolean;
18
- export declare const isString6To24CharacterLong: (password: string | any[]) => boolean;
19
- export declare const isString6To16CharacterLong: (password: string | any[]) => boolean;
20
- export declare const isProvinceString: (inputString: string) => boolean;
21
- export declare const isBoolValue: (inputValue: any) => inputValue is boolean;
22
- export declare const isPostalCodeString: (inputString: string) => boolean;
23
- export declare const isSafeString: (str: string | any[]) => boolean;
24
- export declare const isInStringArray: (StringArray: string | any[], inputString: string) => boolean;
25
- export declare const isCountryCodeString: (str: string | any[]) => boolean;
26
- export declare const isValidDomainName: (domain: string) => boolean;
1
+ export declare const isImageUrl: (imageUrl: unknown) => boolean;
2
+ export declare const isInteger: (number: unknown) => boolean;
3
+ export declare const isValidJsonString: (str: unknown) => boolean;
4
+ export declare const isValidIntegerString: (str: unknown) => boolean;
5
+ export declare const isValidUuidString: (str: unknown) => boolean;
6
+ export declare const isCharactersString: (str: unknown) => boolean;
7
+ export declare const isNameString: (str: unknown) => boolean;
8
+ export declare const isSafeSearchString: (str: unknown) => boolean;
9
+ export declare const isEmailString: (email: unknown) => boolean;
10
+ export declare const isJwtString: (jwt: unknown) => boolean;
11
+ export declare const isPasswordString: (password: unknown) => boolean;
12
+ export declare const isSimplePasswordString: (password: unknown) => boolean;
13
+ export declare const isPasswordStringFailureMessage: (password: unknown) => "Total 6 to 32 characters, numbers and one of !@#$%^&*_-" | null;
14
+ export declare const isSimplePasswordStringFailureMessage: (password: unknown) => "Total 6 to 32 characters, numbers or !@#$%^&*_-" | null;
15
+ export declare const isUsernameString: (str: unknown) => boolean;
16
+ export declare function isPhoneNumber(str: unknown): boolean;
17
+ export declare const isUrlSafeString: (inputString: unknown) => boolean;
18
+ export declare const isString6To24CharacterLong: (password: unknown) => boolean;
19
+ export declare const isString6To16CharacterLong: (password: unknown) => boolean;
20
+ export declare const isProvinceString: (inputString: unknown) => boolean;
21
+ export declare const isBoolValue: (inputValue: unknown) => inputValue is boolean;
22
+ export declare const isPostalCodeString: (inputString: unknown) => boolean;
23
+ export declare const isSafeString: (str: unknown) => boolean;
24
+ export declare const isInStringArray: (StringArray: string[], inputString: unknown) => boolean;
25
+ export declare const isCountryCodeString: (str: unknown) => boolean;
26
+ export declare const isValidDomainName: (domain: unknown) => boolean;
@@ -1,5 +1,8 @@
1
1
  export const isImageUrl = (imageUrl) => {
2
- if (imageUrl === undefined || typeof imageUrl !== 'string' || imageUrl.length === 0)
2
+ if (imageUrl === undefined ||
3
+ typeof imageUrl !== 'string' ||
4
+ imageUrl.length === 0 ||
5
+ imageUrl.length > 2048)
3
6
  return false;
4
7
  const imageUrlRegex = /^[a-zA-Z0-9-_./]+$/;
5
8
  return imageUrlRegex.test(imageUrl);
@@ -8,20 +11,20 @@ export const isInteger = (number) => {
8
11
  return Number.isInteger(number);
9
12
  };
10
13
  export const isValidJsonString = (str) => {
11
- if (str === undefined || typeof str !== 'string' || str.length === 0)
14
+ if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 10000)
12
15
  return false;
13
16
  try {
14
17
  const json = JSON.parse(str);
15
- return typeof json === 'object';
18
+ return typeof json === 'object' && json !== null;
16
19
  }
17
- catch (error) {
20
+ catch {
18
21
  return false;
19
22
  }
20
23
  };
21
24
  export const isValidIntegerString = (str) => {
22
- if (str === undefined || typeof str !== 'string' || str.length === 0)
25
+ if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 20)
23
26
  return false;
24
- return /^\d+$/g.test(str);
27
+ return /^\d+$/.test(str);
25
28
  };
26
29
  export const isValidUuidString = (str) => {
27
30
  if (str === undefined || typeof str !== 'string' || str.length === 0)
@@ -29,45 +32,43 @@ export const isValidUuidString = (str) => {
29
32
  return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(str);
30
33
  };
31
34
  export const isCharactersString = (str) => {
32
- if (str === undefined || typeof str !== 'string' || str.length === 0)
35
+ if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 1000)
33
36
  return false;
34
37
  return /^[\da-zA-Z _-]+$/.test(str);
35
38
  };
36
39
  export const isNameString = (str) => {
37
- if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 1000) {
40
+ if (typeof str !== 'string' || str.length === 0 || str.length > 1000) {
38
41
  return false;
39
42
  }
40
43
  return /^[A-Za-z][0-9a-zA-Z-_.,'() ]+$/.test(str.trim());
41
44
  };
42
45
  export const isSafeSearchString = (str) => {
43
- if (str === undefined || typeof str !== 'string' || str.length === 0)
46
+ if (typeof str !== 'string' || str.length === 0)
44
47
  return false;
45
48
  return /^[A-Za-z][0-9a-zA-Z\-_.,'()@ ]{1,100}$/.test(str.trim());
46
49
  };
47
50
  export const isEmailString = (email) => {
48
- if (email === undefined ||
49
- typeof email !== 'string' ||
50
- email.length === 0 ||
51
- email.length > 1000) {
51
+ if (typeof email !== 'string' || email.length === 0 || email.length > 320) {
52
52
  return false;
53
53
  }
54
- const regExpEmail = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
55
- return regExpEmail.test(String(email).toLowerCase());
54
+ // Simplified safe email regex
55
+ const regExpEmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-zA-Z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/;
56
+ return regExpEmail.test(email);
56
57
  };
57
58
  export const isJwtString = (jwt) => {
58
- if (jwt === '' || jwt.trim() === '')
59
+ if (typeof jwt !== 'string' || jwt.length === 0 || jwt.length > 8192 || jwt.trim() === '')
59
60
  return false;
60
61
  const jwtRegex = /^eyJ[a-zA-Z0-9-_.]+$/;
61
62
  return jwtRegex.test(jwt);
62
63
  };
63
64
  export const isPasswordString = (password) => {
64
- if (password === undefined || typeof password !== 'string' || password.length === 0)
65
+ if (typeof password !== 'string' || password.length === 0 || password.length > 128)
65
66
  return false;
66
67
  const regExPassword = /^(?=.*[a-zA-Z0-9])(?=.*[!@#$%^&*_-])[a-zA-Z0-9!@#$%^&*_-]{6,32}$/;
67
68
  return regExPassword.test(String(password));
68
69
  };
69
70
  export const isSimplePasswordString = (password) => {
70
- if (password === undefined || typeof password !== 'string' || password.length === 0)
71
+ if (typeof password !== 'string' || password.length === 0 || password.length > 128)
71
72
  return false;
72
73
  const regExPassword = /^[a-zA-Z0-9!@#$%^&*_-]{6,32}$/;
73
74
  return regExPassword.test(String(password));
@@ -81,7 +82,7 @@ export const isPasswordStringFailureMessage = (password) => {
81
82
  }
82
83
  };
83
84
  export const isSimplePasswordStringFailureMessage = (password) => {
84
- if (!isPasswordString(password)) {
85
+ if (!isSimplePasswordString(password)) {
85
86
  return 'Total 6 to 32 characters, numbers or !@#$%^&*_-';
86
87
  }
87
88
  else {
@@ -89,70 +90,70 @@ export const isSimplePasswordStringFailureMessage = (password) => {
89
90
  }
90
91
  };
91
92
  export const isUsernameString = (str) => {
92
- if (str === undefined || typeof str !== 'string' || str.length === 0 || str.length > 200)
93
+ if (typeof str !== 'string' || str.length === 0 || str.length > 200)
93
94
  return false;
94
95
  return /^[0-9a-zA-Z]+$/.test(str);
95
96
  };
96
97
  export function isPhoneNumber(str) {
97
- if (str === undefined || typeof str !== 'string' || str.length === 0)
98
+ if (typeof str !== 'string' || str.length === 0 || str.length > 20)
98
99
  return false;
99
100
  return /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/.test(str);
100
101
  }
101
102
  export const isUrlSafeString = (inputString) => {
102
- if (inputString === '' || inputString.trim() === '')
103
+ if (typeof inputString !== 'string' ||
104
+ inputString.length === 0 ||
105
+ inputString.length > 2048 ||
106
+ inputString.trim() === '')
103
107
  return false;
104
- const jwtRegex = /^[a-zA-Z0-9-_.]+$/;
105
- return jwtRegex.test(inputString);
108
+ const urlSafeRegex = /^[a-zA-Z0-9-_.]+$/;
109
+ return urlSafeRegex.test(inputString);
106
110
  };
107
111
  export const isString6To24CharacterLong = (password) => {
108
- if (password === undefined || typeof password !== 'string' || password.length === 0)
112
+ if (typeof password !== 'string' || password.length === 0)
109
113
  return false;
110
114
  return 6 <= password.length && password.length <= 24;
111
115
  };
112
116
  export const isString6To16CharacterLong = (password) => {
113
- if (password === undefined || typeof password !== 'string' || password.length === 0)
117
+ if (typeof password !== 'string' || password.length === 0)
114
118
  return false;
115
119
  return 6 <= password.length && password.length <= 16;
116
120
  };
117
121
  export const isProvinceString = (inputString) => {
118
122
  const provinces = ['on', 'qc'];
119
123
  if (isNameString(inputString)) {
120
- return provinces.includes(inputString.toLowerCase());
124
+ return provinces.includes(inputString.toLowerCase().trim());
121
125
  }
122
126
  return false;
123
127
  };
124
128
  export const isBoolValue = (inputValue) => {
125
- return !(inputValue === undefined || typeof inputValue !== 'boolean');
129
+ return typeof inputValue === 'boolean';
126
130
  };
127
131
  export const isPostalCodeString = (inputString) => {
128
132
  const postalCodeRegex = /^(?!.*[DFIOQU])[A-VXY][0-9][A-Z] ?[0-9][A-Z][0-9]$/i;
129
133
  if (isNameString(inputString)) {
130
- return postalCodeRegex.test(inputString);
134
+ return postalCodeRegex.test(inputString.trim());
131
135
  }
132
136
  return false;
133
137
  };
134
138
  export const isSafeString = (str) => {
135
- if (str === undefined || typeof str !== 'string' || str.length === 0)
139
+ if (typeof str !== 'string' || str.length === 0 || str.length > 10000)
136
140
  return false;
137
- return /^[\da-zA-Z-_.,#*'()\[\]: ]+$/.test(str);
141
+ return /^[\da-zA-Z-_.,#*'()[\]: ]+$/.test(str);
138
142
  };
139
143
  export const isInStringArray = (StringArray, inputString) => {
140
144
  if (isNameString(inputString)) {
141
- return StringArray.includes(inputString.toLowerCase());
145
+ return StringArray.includes(inputString.toLowerCase().trim());
142
146
  }
143
147
  return false;
144
148
  };
145
149
  export const isCountryCodeString = (str) => {
146
- if (str === undefined || typeof str !== 'string' || str.length === 0)
150
+ if (typeof str !== 'string' || str.length === 0 || str.length > 4)
147
151
  return false;
148
- const postalCodeRegex = /^\+[1-9][0-9]{0,2}$/i;
149
- return postalCodeRegex.test(str);
152
+ const countryCodeRegex = /^\+[1-9][0-9]{0,2}$/;
153
+ return countryCodeRegex.test(str);
150
154
  };
151
155
  export const isValidDomainName = (domain) => {
152
- if (domain === undefined ||
153
- typeof domain !== 'string' ||
154
- domain.length === 0 ||
155
- domain.length > 253)
156
+ if (typeof domain !== 'string' || domain.length === 0 || domain.length > 253)
156
157
  return false;
157
158
  const domainRegex = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i;
158
159
  return domainRegex.test(domain);
@@ -1,5 +1,5 @@
1
1
  export declare function validateProperties(obj?: {
2
- [key: string]: string | any;
2
+ [key: string]: unknown;
3
3
  }): {
4
- [key: string]: any;
4
+ [key: string]: unknown;
5
5
  };
@@ -1,11 +1,13 @@
1
1
  // Validate the property values format.
2
2
  import { isEmailString, isPhoneNumber, isUrlSafeString, isValidUuidString, isSimplePasswordString, isPasswordString, isString6To16CharacterLong, isNameString, isSafeSearchString, isCharactersString, isValidIntegerString, isValidJsonString, isImageUrl, isValidDomainName, } from './validate';
3
3
  export function validateProperties(obj = {}) {
4
- let returnObj = {};
5
- for (let [key, value] of Object.entries(obj || {})) {
4
+ const returnObj = {};
5
+ for (const [key, value] of Object.entries(obj || {})) {
6
6
  switch (key) {
7
7
  case 'first_name':
8
+ case 'firstName':
8
9
  case 'username':
10
+ case 'new_status':
9
11
  case 'newStatus':
10
12
  case 'description':
11
13
  case 'comment':
@@ -14,33 +16,49 @@ export function validateProperties(obj = {}) {
14
16
  case 'title':
15
17
  case 'brand':
16
18
  case 'short_description':
17
- isNameString(value) ? (returnObj[key] = value) : null;
19
+ case 'shortDescription':
20
+ if (isNameString(value)) {
21
+ returnObj[key] = value;
22
+ }
18
23
  break;
19
24
  case 'search_string':
20
25
  case 'searchString':
21
- isSafeSearchString(value) ? (returnObj[key] = value) : null;
26
+ if (isSafeSearchString(value)) {
27
+ returnObj[key] = value;
28
+ }
22
29
  break;
23
30
  case 'password':
24
31
  case 'new_password':
25
- isString6To16CharacterLong(value) && isSimplePasswordString(value)
26
- ? (returnObj[key] = value)
27
- : null;
32
+ case 'newPassword':
33
+ if (isString6To16CharacterLong(value) && isSimplePasswordString(value)) {
34
+ returnObj[key] = value;
35
+ }
28
36
  break;
29
37
  case 'strong_password':
30
- isString6To16CharacterLong(value) && isPasswordString(value)
31
- ? (returnObj[key] = value)
32
- : null;
38
+ case 'strongPassword':
39
+ if (isString6To16CharacterLong(value) && isPasswordString(value)) {
40
+ returnObj[key] = value;
41
+ }
33
42
  break;
34
43
  case 'email':
35
- isEmailString(value) ? (returnObj[key] = value) : null;
44
+ if (isEmailString(value)) {
45
+ returnObj[key] = value;
46
+ }
36
47
  break;
37
48
  case 'phone_number':
38
- isPhoneNumber(value) ? (returnObj[key] = value) : null;
49
+ case 'phoneNumber':
50
+ if (isPhoneNumber(value)) {
51
+ returnObj[key] = value;
52
+ }
39
53
  break;
40
54
  case 'token':
41
55
  case 'email_confirm_token':
56
+ case 'emailConfirmToken':
42
57
  case 'verification_token':
43
- isUrlSafeString(value) ? (returnObj[key] = value) : null;
58
+ case 'verificationToken':
59
+ if (isUrlSafeString(value)) {
60
+ returnObj[key] = value;
61
+ }
44
62
  break;
45
63
  case 'uuid':
46
64
  case 'item_id':
@@ -48,35 +66,59 @@ export function validateProperties(obj = {}) {
48
66
  case 'image_id':
49
67
  case 'itemId':
50
68
  case 'userId':
69
+ case 'imageId':
70
+ case 'order_id':
51
71
  case 'orderId':
52
72
  case 'category_id':
73
+ case 'categoryId':
53
74
  case 'parent_id':
54
- isValidUuidString(value) ? (returnObj[key] = value) : null;
75
+ case 'parentId':
76
+ if (isValidUuidString(value)) {
77
+ returnObj[key] = value;
78
+ }
55
79
  break;
56
80
  case 'period':
57
- isCharactersString(value) ? (returnObj[key] = value) : null;
81
+ if (isCharactersString(value)) {
82
+ returnObj[key] = value;
83
+ }
58
84
  break;
59
85
  case 'offset_number':
86
+ case 'offsetNumber':
60
87
  case 'number_of_orders':
88
+ case 'numberOfOrders':
61
89
  case 'price':
62
- isValidIntegerString(value) ? (returnObj[key] = value) : null;
90
+ if (isValidIntegerString(value)) {
91
+ returnObj[key] = value;
92
+ }
63
93
  break;
64
94
  case 'about':
65
- isValidJsonString(value) ? (returnObj[key] = value.trim()) : null;
95
+ if (isValidJsonString(value)) {
96
+ returnObj[key] = value.trim();
97
+ }
66
98
  break;
67
99
  case 'weight':
68
100
  case 'dimensions':
69
101
  case 'permission':
70
- isValidJsonString(JSON.stringify(value)) ? (returnObj[key] = value) : null;
102
+ if (isValidJsonString(JSON.stringify(value))) {
103
+ returnObj[key] = value;
104
+ }
71
105
  break;
72
106
  case 'image_url':
73
- isImageUrl(value) ? (returnObj[key] = value) : null;
107
+ case 'imageUrl':
108
+ if (isImageUrl(value)) {
109
+ returnObj[key] = value;
110
+ }
74
111
  break;
75
112
  case 'domain_name':
113
+ case 'domainName':
76
114
  case 'domain':
77
115
  case 'email_domain':
116
+ case 'emailDomain':
78
117
  case 'email_domain_name':
79
- isValidDomainName(value) ? (returnObj[key] = value) : null;
118
+ case 'emailDomainName':
119
+ if (isValidDomainName(value)) {
120
+ returnObj[key] = value;
121
+ }
80
122
  break;
81
123
  }
82
124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carecard/validate-ts",
3
- "version": "2.1.1",
3
+ "version": "2.2.3",
4
4
  "private": false,
5
5
  "description": "Validate functions",
6
6
  "license": "ISC",
@@ -18,9 +18,9 @@
18
18
  "types": "./dist/esm/index.d.ts",
19
19
  "exports": {
20
20
  ".": {
21
+ "types": "./dist/esm/index.d.ts",
21
22
  "import": "./dist/esm/index.js",
22
- "require": "./dist/cjs/index.cjs",
23
- "types": "./dist/esm/index.d.ts"
23
+ "require": "./dist/cjs/index.cjs"
24
24
  }
25
25
  },
26
26
  "files": [
@@ -34,14 +34,27 @@
34
34
  "format": "prettier --write .",
35
35
  "format:check": "prettier --check .",
36
36
  "lint": "eslint",
37
+ "lint:fix": "eslint --fix",
37
38
  "prepare": "husky"
38
39
  },
40
+ "lint-staged": {
41
+ "*.{js,ts,cjs}": [
42
+ "eslint --fix",
43
+ "prettier --write"
44
+ ],
45
+ "*.{json,md,yml}": [
46
+ "prettier --write"
47
+ ]
48
+ },
39
49
  "devDependencies": {
50
+ "@eslint/js": "^9.39.2",
40
51
  "@types/jest": "30.0.0",
41
52
  "@types/node": "25.0.3",
42
53
  "eslint": "9.39.2",
54
+ "globals": "^17.1.0",
43
55
  "husky": "9.1.7",
44
56
  "jest": "30.2.0",
57
+ "lint-staged": "^16.2.7",
45
58
  "prettier": "3.7.4",
46
59
  "ts-jest": "29.4.6",
47
60
  "typescript": "5.9.3",