validator.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /*
  48. ingredient = {
  49. name: required,
  50. category: required,
  51. unit: required,
  52. quantity: required,
  53. specialUnit: optional,
  54. unitSize: optional
  55. }
  56. */
  57. ingredient: function(ingredient){
  58. if(!this.isSanitary([ingredient.name, ingredient.category])){
  59. return "INGREDIENT CONTAINS ILLEGAL CHARACTERS";
  60. }
  61. if(ingredient.specialUnit === "bottle"){
  62. let quantityCheck = this.quantity(ingredient.unitSize);
  63. if(quantityCheck !== true){
  64. return "BOTTLE SIZE MUST BE A NON-NEGATIVE NUMBER";
  65. }
  66. }
  67. return true;
  68. },
  69. recipe: function(recipe){
  70. if(!this.isSanitary([recipe.name])){
  71. return "INGREDIENT CONTAINS ILLEGAL CHARACTERS";
  72. }
  73. let priceCheck = this.price(recipe.price);
  74. if(priceCheck !== true){
  75. return priceCheck;
  76. }
  77. for(let i = 0; i < recipe.ingredients.length; i++){
  78. let checkQuantity = this.quantity(recipe.ingredients[i].quantity);
  79. if(checkQuantity !== true){
  80. return checkQuantity;
  81. }
  82. }
  83. for(let i = 0; i < recipe.ingredients.length; i++){
  84. for(let j = i + 1; j < recipe.ingredients.length; j++){
  85. if(recipe.ingredients[i].ingredient === recipe.ingredients[j].ingredient){
  86. return "RECIPE CANNOT CONTAIN DUPLICATE INGREDIENTS";
  87. }
  88. }
  89. }
  90. return true;
  91. },
  92. order: function(order){
  93. if(!this.isSanitary([order.name])){
  94. return "ORDER NAME CONTAINS ILLEGAL CHARACTERS";
  95. }
  96. if(new Date(order.date) > new Date()){
  97. return "DATE CANNOT BE IN THE FUTURE";
  98. }
  99. if(this.quantity(order.taxes) !== true){
  100. return "TAXES MUST BE A NON NEGATIVE NUMBER";
  101. }
  102. if(this.quantity(order.fees) !== true){
  103. return "FEES MUST BE A NON NEGATIVE NUMBER";
  104. }
  105. for(let i = 0; i < order.ingredients; i++){
  106. let quantityCheck = this.quantity(order.ingredients[i].quantity);
  107. if(quantityCheck !== true){
  108. return quantityCheck;
  109. }
  110. let priceCheck = this.price(order.ingredients[i].price);
  111. if(priceCheck !== true){
  112. return priceCheck;
  113. }
  114. }
  115. return true;
  116. },
  117. isSanitary: function(strings){
  118. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  119. for(let i = 0; i < strings.length; i++){
  120. for(let j = 0; j < disallowed.length; j++){
  121. if(strings[i].includes(disallowed[j])){
  122. return false;
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. }