validator.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. if(merchant.password.length < 10){
  15. return "Password must contain at least 10 characters";
  16. }
  17. if(merchant.password !== merchant.confirmPassword){
  18. return "Passwords do not match";
  19. }
  20. return true;
  21. },
  22. ingredient: function(ingredient){
  23. if(!this.isSanitary([ingredient.name, ingredient.category, ingredient.unit])){
  24. return false;
  25. }
  26. return true;
  27. },
  28. quantity: function(num){
  29. if(isNaN(num) || num === ""){
  30. return false;
  31. }
  32. if(num < 0){
  33. return false;
  34. }
  35. return true;
  36. },
  37. isSanitary: function(strings){
  38. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  39. for(let i = 0; i < strings.length; i++){
  40. for(let j = 0; j < disallowed.length; j++){
  41. if(strings[i].includes(disallowed[j])){
  42. return false;
  43. }
  44. }
  45. }
  46. return true;
  47. }
  48. }