validator.js 3.6 KB

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