Skip to content
This repository was archived by the owner on Sep 20, 2025. It is now read-only.

Commit 6fd4d3d

Browse files
committed
Improve handling inputs globally
1 parent 4de1271 commit 6fd4d3d

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

src/models/v1/product.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ module.exports = (sequelize, DataTypes) => {
645645
return "The name field is required.";
646646
} else if ("string" !== typeof payload.name) {
647647
return "The name field must be a string.";
648-
} else if (50 < payload.name.length) {
648+
} else if (50 < payload.name.trim().length) {
649649
return "The name field must be less than 51 characters.";
650650
} else {
651651
const nameExists = await this.getProductBySlug(
@@ -666,18 +666,22 @@ module.exports = (sequelize, DataTypes) => {
666666
return "The weight field is required.";
667667
} else if (null === `${payload.weight}`.match(numberWithOptionalDecimalPartRegex)) {
668668
return "The weight field must be a number"
669+
} else if (500 < Number(payload.weight)) {
670+
return "The weight field number must not be greater than 500.";
669671
}
670672

671673
if (undefined === payload.price) {
672674
return "The price field is required.";
673675
} else if (null === `${payload.price}`.match(numberWithOptionalDecimalPartRegex)) {
674676
return "The price field must be a number"
677+
} else if (500 < Number(payload.price)) {
678+
return "The price field number must not be greater than 500.";
675679
}
676680

677681
if (payload.description) {
678682
if ("string" !== typeof payload.description) {
679683
return "The description field must be a string.";
680-
} else if (1000 < payload.description.length) {
684+
} else if (1000 < payload.description.trim().length) {
681685
return "The description field must be less than 1001 characters.";
682686
}
683687
}
@@ -728,19 +732,19 @@ module.exports = (sequelize, DataTypes) => {
728732
if (payload.stripeProductId) {
729733
if ("string" !== typeof payload.stripeProductId) {
730734
return "The stripe product id field must be of type string."
731-
} else if (15 > payload.stripeProductId.length) {
735+
} else if (15 > payload.stripeProductId.trim().length) {
732736
return "The stripe product id field length must be greater than 15 characters.";
733-
} else if (30 < payload.stripeProductId.length) {
737+
} else if (30 < payload.stripeProductId.trim().length) {
734738
return "The stripe product id field length must not exceed 30 characters.";
735739
}
736740
}
737741

738742
if (payload.stripePriceId) {
739743
if ("string" !== typeof payload.stripePriceId) {
740744
return "The stripe price id field must be of type string."
741-
} else if (15 > payload.stripePriceId.length) {
745+
} else if (15 > payload.stripePriceId.trim().length) {
742746
return "The stripe price id field length must be greater than 15 characters.";
743-
} else if (50 < payload.stripePriceId.length) {
747+
} else if (50 < payload.stripePriceId.trim().length) {
744748
return "The stripe price id field length must not exceed 50 characters.";
745749
}
746750
}
@@ -766,7 +770,7 @@ module.exports = (sequelize, DataTypes) => {
766770
return "The name field is required.";
767771
} else if ("string" !== typeof payload.name) {
768772
return "The name field must be a string.";
769-
} else if (50 < payload.name.length) {
773+
} else if (50 < payload.name.trim().length) {
770774
return "The name field must be less than 51 characters.";
771775
} else {
772776
const newSlug = slugify(payload.name);
@@ -790,18 +794,22 @@ module.exports = (sequelize, DataTypes) => {
790794
return "The weight field is required.";
791795
} else if (null === `${payload.weight}`.match(numberWithOptionalDecimalPartRegex)) {
792796
return "The weight field must be a number"
797+
} else if (500 < Number(payload.weight)) {
798+
return "The weight field number must not be greater than 500.";
793799
}
794800

795801
if (undefined === payload.price) {
796802
return "The price field is required.";
797803
} else if (null === `${payload.price}`.match(numberWithOptionalDecimalPartRegex)) {
798804
return "The price field must be a number"
805+
} else if (500 < Number(payload.price)) {
806+
return "The price field number must not be greater than 500.";
799807
}
800808

801809
if (payload.description) {
802810
if ("string" !== typeof payload.description) {
803811
return "The description field must be a string.";
804-
} else if (1000 < payload.description.length) {
812+
} else if (1000 < payload.description.trim().length) {
805813
return "The description field must be less than 1001 characters.";
806814
}
807815
}
@@ -883,14 +891,14 @@ module.exports = (sequelize, DataTypes) => {
883891

884892
static getNewProductData(payload) {
885893
const result = {
886-
name: payload.name,
894+
name: payload.name.trim(),
887895
units: payload.units,
888896
weight: payload.weight,
889897
price: payload.price,
890898
isLive: false,
891899
};
892900
if (payload.description) {
893-
result.description = payload.description;
901+
result.description = payload.description.trim();
894902
}
895903
if (payload.category) {
896904
result.category = payload.category;
@@ -904,24 +912,24 @@ module.exports = (sequelize, DataTypes) => {
904912
}
905913
}
906914
if (payload.stripeProductId) {
907-
result.stripeProductId = payload.stripeProductId;
915+
result.stripeProductId = payload.stripeProductId.trim();
908916
}
909917
if (payload.stripePriceId) {
910-
result.stripePriceId = payload.stripePriceId;
918+
result.stripePriceId = payload.stripePriceId.trim();
911919
}
912920
return result;
913921
}
914922

915923
static getEditProductData(payload) {
916924
const result = {
917-
name: payload.name,
925+
name: payload.name.trim(),
918926
units: payload.units,
919927
weight: payload.weight,
920928
price: payload.price,
921929
isLive: false,
922930
};
923931
if (payload.description) {
924-
result.description = payload.description;
932+
result.description = payload.description.trim();
925933
}
926934
if (payload.category) {
927935
result.category = payload.category;
@@ -935,10 +943,10 @@ module.exports = (sequelize, DataTypes) => {
935943
}
936944
}
937945
if (payload.stripeProductId) {
938-
result.stripeProductId = payload.stripeProductId;
946+
result.stripeProductId = payload.stripeProductId.trim();
939947
}
940948
if (payload.stripePriceId) {
941-
result.stripePriceId = payload.stripePriceId;
949+
result.stripePriceId = payload.stripePriceId.trim();
942950
}
943951
return result;
944952
}

src/models/v1/user.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ module.exports = (sequelize, DataTypes) => {
292292
return "The email field is missing.";
293293
} else if ("string" !== typeof bodyInput.email) {
294294
return "The email field must be of type string";
295-
} else if (30 < bodyInput.email.length) {
295+
} else if (30 < bodyInput.email.trim().length) {
296296
return "The email field length must not exceed 30 characters.";
297297
} else if (null === bodyInput.email.match(validEmailRegex)) {
298298
return "The email field must be a valid email address.";
@@ -508,7 +508,7 @@ module.exports = (sequelize, DataTypes) => {
508508
return "The email field is missing.";
509509
} else if ("string" !== typeof bodyInput.email) {
510510
return "The email field must be of type string";
511-
} else if (30 < bodyInput.email.length) {
511+
} else if (30 < bodyInput.email.trim().length) {
512512
return "The email field length must not exceed 30 characters.";
513513
} else if (null === bodyInput.email.match(validEmailRegex)) {
514514
return "The email field must be a valid email address.";
@@ -541,7 +541,7 @@ module.exports = (sequelize, DataTypes) => {
541541
return false;
542542
}
543543

544-
return { email, password, };
544+
return { email: email.trim(), password, };
545545
}
546546

547547
/**

src/models/v1/userAddress.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,36 +153,46 @@ module.exports = (sequelize, DataTypes) => {
153153
return "The address line 1 field is required.";
154154
} else if ("string" !== typeof payload.addressLine1) {
155155
return "The address line 1 field must be of type string.";
156+
} else if (50 < payload.addressLine1.trim().length) {
157+
return "The address line 1 field must not exceed 50 characters";
156158
}
157159

158160
if (payload.addressLine2) {
159161
if ("string" !== typeof payload.addressLine2) {
160162
return "The address line 2 field must be of type string.";
161163
}
164+
} else if (50 < payload.addressLine2.trim().length) {
165+
return "The address line 2 field must not exceed 50 characters";
162166
}
163167

164168
if (!payload.zipCode) {
165169
return "The zip code field is required.";
166170
} else if ("string" !== typeof payload.zipCode) {
167171
return "The zip code field must be of type string.";
172+
} else if (10 < payload.zipCode.trim().length) {
173+
return "The zip code field must not exceed 10 characters";
168174
}
169175

170176
if (!payload.city) {
171177
return "The city field is required.";
172178
} else if ("string" !== typeof payload.city) {
173179
return "The city field must be of type string.";
180+
} else if (20 < payload.city.trim().length) {
181+
return "The city field must not exceed 20 characters";
174182
}
175183

176184
if (!payload.state) {
177185
return "The state field is required.";
178186
} else if ("string" !== typeof payload.state) {
179187
return "The state field must be of type string.";
188+
} else if (20 < payload.state.trim().length) {
189+
return "The address line 1 field must not exceed 20 characters";
180190
}
181191

182192
if (payload.telephoneAreaCode) {
183193
if ("string" !== typeof payload.telephoneAreaCode) {
184194
return "The telephone area code field must be of type string.";
185-
} else if (6 < payload.telephoneAreaCode.length) {
195+
} else if (6 < payload.telephoneAreaCode.trim().length) {
186196
return "The telephone area code field must not be greater than 6 characters.";
187197
}
188198
}
@@ -191,7 +201,7 @@ module.exports = (sequelize, DataTypes) => {
191201
return "The telephone field is required.";
192202
} else if ("string" !== typeof payload.telephone) {
193203
return "The telephone field must be of type string.";
194-
} else if (30 < payload.telephone.length) {
204+
} else if (30 < payload.telephone.trim().length) {
195205
return "The telephone field must not be greater than 30 characters.";
196206
}
197207

@@ -216,31 +226,31 @@ module.exports = (sequelize, DataTypes) => {
216226
const result = {};
217227

218228
if (payload.addressLine1) {
219-
result.addressLine1 = payload.addressLine1;
229+
result.addressLine1 = payload.addressLine1.trim();
220230
}
221231

222232
if (payload.addressLine2) {
223-
result.addressLine2 = payload.addressLine2;
233+
result.addressLine2 = payload.addressLine2.trim();
224234
}
225235

226236
if (payload.zipCode) {
227-
result.zipCode = payload.zipCode;
237+
result.zipCode = payload.zipCode.trim();
228238
}
229239

230240
if (payload.city) {
231-
result.city = payload.city;
241+
result.city = payload.city.trim();
232242
}
233243

234244
if (payload.state) {
235-
result.state = payload.state;
245+
result.state = payload.state.trim();
236246
}
237247

238248
if (payload.telephoneAreaCode) {
239-
result.telephoneAreaCode = payload.telephoneAreaCode;
249+
result.telephoneAreaCode = payload.telephoneAreaCode.trim();
240250
}
241251

242252
if (payload.telephone) {
243-
result.telephone = payload.telephone;
253+
result.telephone = payload.telephone.trim();
244254
}
245255

246256
return result;

0 commit comments

Comments
 (0)