validation.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. let validator = {
  2. /*
  3. ingredient = {
  4. ingredient: {
  5. name: name of ingredient,
  6. category: category of ingredient,
  7. unit: unit measure for ingredient
  8. },
  9. quantity: quantity of ingredient for current merchant
  10. }
  11. */
  12. ingredient: function(ingredient, createBanner = true){
  13. let errors = [];
  14. if(!this.isSanitary(ingredient.ingredient.name) ||
  15. !this.isSanitary(ingredient.ingredient.category) ||
  16. !this.isSanitary(ingredient.ingredient.unit)){
  17. errors.push("Contains illegal characters");
  18. }
  19. if(isNaN(ingredient.quantity) || ingredient.quantity === ""){
  20. errors.push("Must enter a valid number");
  21. }
  22. if(ingredient.quantity < 0){
  23. banner.createError("Quantity cannot be a negative number");
  24. }
  25. if(errors.length > 0){
  26. if(createBanner){
  27. for(let i = 0; i < errors.length; i++){
  28. banner.createError(errors[i]);
  29. }
  30. }
  31. return false;
  32. }
  33. return true;
  34. },
  35. ingredientQuantity: function(quantity, createBanner = true){
  36. let errors = [];
  37. if(isNaN(quantity) || quantity === ""){
  38. errors.push("Must enter a valid number");
  39. }
  40. if(quantity < 0){
  41. banner.createError("Quantity cannot be a negative number");
  42. }
  43. if(errors.length > 0){
  44. if(createBanner){
  45. for(let i = 0; i < errors.length; i++){
  46. banner.createError(errors[i]);
  47. }
  48. }
  49. return false;
  50. }
  51. return true;
  52. },
  53. merchant: {
  54. password: function(pass, confirmPass, createBanner = true){
  55. if(pass !== confirmPass){
  56. if(createBanner){
  57. banner.createError("Your passwords do not match");
  58. }
  59. return false;
  60. }
  61. if(pass.length < 15){
  62. if(createBanner){
  63. banner.createError("Your password must contain at least 15 characters");
  64. }
  65. return false;
  66. }
  67. return true;
  68. }
  69. },
  70. transaction: {
  71. date: function(from, to = new Date(), createBanner = true){
  72. let errors = [];
  73. let today = new Date();
  74. if(from > to){
  75. errors.push("Starting date must be before ending date");
  76. }
  77. if(from > today || to > today.setDate(today.getDate() + 1)){
  78. errors.push("Cannot choose a date in the future");
  79. }
  80. if(errors.length > 0){
  81. if(createBanner){
  82. for(let i = 0; i < errors.length; i++){
  83. banner.createError(errors[i]);
  84. }
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. },
  91. recipe: function(newRecipe, createBanner = true){
  92. let errors = [];
  93. if(!validator.isSanitary(newRecipe.name)){
  94. errors.push("Name contains invalid characters");
  95. }
  96. if(newRecipe.price < 0){
  97. errors.push("Price must contain a non-negative number");
  98. }
  99. if(newRecipe.ingredients.length === 0){
  100. errors.push("Must include at least one ingredient");
  101. }
  102. let checkSet = new Set();
  103. for(let i = 0; i < newRecipe.ingredients.length; i++){
  104. if(newRecipe.ingredients[i].quantity < 0){
  105. errors.push("Quantity must contain a non-negative number");
  106. break;
  107. }
  108. checkSet.add(newRecipe.ingredients[i].ingredient);
  109. }
  110. if(checkSet.size !== newRecipe.ingredients.length){
  111. errors.push("Recipe contains duplicate ingredients");
  112. }
  113. if(isNaN(newRecipe.price) || newRecipe.price === "" || newRecipe.price< 0){
  114. errors.push("Must enter a valid price");
  115. }
  116. if(errors.length > 0){
  117. if(createBanner){
  118. for(let i = 0; i < errors.length; i++){
  119. banner.createError(errors[i]);
  120. }
  121. return false;
  122. }
  123. }
  124. return true;
  125. },
  126. order: function(order, createBanner = true){
  127. let errors = [];
  128. if(!validator.isSanitary(order.name, false)){
  129. errors.push("Your string contains illegal characters");
  130. }
  131. let now = new Date()
  132. if(order.date > now){
  133. errors.push("Cannot have a date/time in the future");
  134. }
  135. for(let i = 0; i < order.ingredients.length; i++){
  136. if(order.ingredients[i].quantity < 0){
  137. errors.push("Quantity cannot be negative");
  138. break;
  139. }
  140. if(order.ingredients[i].price < 0){
  141. errors.push("Price cannot be negative");
  142. break;
  143. }
  144. if(order.ingredients[i].price === "" || order.ingredients[i].quantity === ""){
  145. errors.push("Incomplete information");
  146. }
  147. }
  148. if(errors.length > 0){
  149. if(createBanner){
  150. for(let i = 0; i < errors.length; i++){
  151. banner.createError(errors[i]);
  152. }
  153. }
  154. return false;
  155. }
  156. return true;
  157. },
  158. isSanitary: function(str, createBanner = true){
  159. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  160. for(let i = 0; i < disallowed.length; i++){
  161. if(str.includes(disallowed[i])){
  162. if(createBanner){
  163. banner.createError("Your string contains illegal characters");
  164. }
  165. return false;
  166. }
  167. }
  168. return true;
  169. }
  170. }