validator.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. quantity: function(num){
  30. console.log(num);
  31. if(isNaN(num) || num === ""){
  32. return "Quantity must be a number";
  33. }
  34. if(num < 0){
  35. return "Quantity cannot be a negative number";
  36. }
  37. return true;
  38. },
  39. price: function(price){
  40. if(price < 0){
  41. return "Price cannot be a negative number";
  42. }
  43. if(isNaN(price) || price === ""){
  44. return "Price must be a number";
  45. }
  46. return true;
  47. },
  48. ingredient: function(ingredient){
  49. if(!this.isSanitary([ingredient.name, ingredient.category, ingredient.unit])){
  50. return "Ingredient contains illegal characters";
  51. }
  52. return true;
  53. },
  54. recipe: function(recipe){
  55. if(!this.isSanitary([recipe.name])){
  56. return "Ingredient contains illegal characters";
  57. }
  58. let priceCheck = this.price(recipe.price);
  59. if(priceCheck !== true){
  60. return priceCheck;
  61. }
  62. for(let i = 0; i < recipe.ingredients.length; i++){
  63. let checkQuantity = this.quantity(recipe.ingredients[i].quantity);
  64. if(checkQuantity !== true){
  65. return checkQuantity;
  66. }
  67. }
  68. return true;
  69. },
  70. order: function(order){
  71. if(!this.isSanitary([order.orderId])){
  72. return "Order name contains illegal characters";
  73. }
  74. if(new Date(order.date) > new Date()){
  75. return "Date cannot be in the future";
  76. }
  77. for(let i = 0; i < order.ingredients; i++){
  78. let quantityCheck = this.quantity(order.ingredients[i].quantity);
  79. if(quantityCheck !== true){
  80. return quantityCheck;
  81. }
  82. let priceCheck = this.price(order.ingredients[i].price);
  83. if(priceCheck !== true){
  84. return priceCheck;
  85. }
  86. }
  87. return true;
  88. },
  89. isSanitary: function(strings){
  90. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  91. for(let i = 0; i < strings.length; i++){
  92. for(let j = 0; j < disallowed.length; j++){
  93. if(strings[i].includes(disallowed[j])){
  94. return false;
  95. }
  96. }
  97. }
  98. return true;
  99. }
  100. }