validator.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const Merchant = require("../models/merchant.js");
  2. module.exports = {
  3. merchant: async function(merchant){
  4. if(!this.isSanitary([merchant.name])){
  5. return "Name contains illegal characters";
  6. }
  7. if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(merchant.email)){
  8. return "Invalid email address";
  9. }
  10. let checkMerchant = await Merchant.findOne({email: merchant.email});
  11. if(checkMerchant){
  12. return "An account with that email address already exists";
  13. }
  14. let checkPassword = this.password(merchant.password, merchant.confirmPassword);
  15. if(this.password(checkPassword !== true)){
  16. return checkPassword;
  17. }
  18. return true;
  19. },
  20. password: function(password, confirmPassword){
  21. if(password.length < 10){
  22. return "Password must contain at least 10 characters";
  23. }
  24. if(password !== confirmPassword){
  25. return "Passwords do not match";
  26. }
  27. return true;
  28. },
  29. ingredient: function(ingredient){
  30. if(!this.isSanitary([ingredient.name, ingredient.category, ingredient.unit])){
  31. return false;
  32. }
  33. return true;
  34. },
  35. quantity: function(num){
  36. if(isNaN(num) || num === ""){
  37. return false;
  38. }
  39. if(num < 0){
  40. return false;
  41. }
  42. return true;
  43. },
  44. isSanitary: function(strings){
  45. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  46. for(let i = 0; i < strings.length; i++){
  47. for(let j = 0; j < disallowed.length; j++){
  48. if(strings[i].includes(disallowed[j])){
  49. return false;
  50. }
  51. }
  52. }
  53. return true;
  54. }
  55. }