validation.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. // ingredient: {
  36. // name: function(ingName, createBanner = true){
  37. // //Check for special chars
  38. // if(!validator.isSanitary(ingName)){
  39. // if(createBanner){
  40. // banner.createError("Your inputs contain illegal characters");
  41. // }
  42. // return false;
  43. // }
  44. // //Check for length
  45. // if(ingName.length < 2){
  46. // if(createBanner){
  47. // banner.createError("Ingredient name must contain at least 2 characters");
  48. // }
  49. // return false;
  50. // }
  51. // return true;
  52. // },
  53. // category: function(ingCategory, createBanner = true){
  54. // //Check for special chars
  55. // if(!validator.isSanitary(ingCategory)){
  56. // if(createBanner){
  57. // banner.createError("Your inputs contain illegal characters");
  58. // }
  59. // return false;
  60. // }
  61. // //Check for length
  62. // if(ingCategory.length < 3){
  63. // if(createBanner){
  64. // banner.createError("Category name must contain at least 3 characters");
  65. // }
  66. // return false;
  67. // }
  68. // return true;
  69. // },
  70. // quantity: function(num, createBanner = true){
  71. // if(isNaN(num) || num === ""){
  72. // if(createBanner){
  73. // banner.createError("Must enter a valid number");
  74. // }
  75. // return false;
  76. // }
  77. // if(num < 0){
  78. // if(createBanner){
  79. // banner.createError("Quantity cannot be a negative number");
  80. // }
  81. // return false;
  82. // }
  83. // return true;
  84. // },
  85. // unit: function(ingUnit, createBanner = true){
  86. // //Check for special chars
  87. // if(!validator.isSanitary(ingUnit)){
  88. // if(createBanner){
  89. // banner.createError("Your inputs contain illegal characters");
  90. // }
  91. // return false;
  92. // }
  93. // return true;
  94. // },
  95. // //Check all parts of ingredient, return true if all pass
  96. // //Quantity passed seperately and optional
  97. // all: function(ingObject, quantity = 0, createBanner = true){
  98. // let nameCheck = this.name(ingObject.name, createBanner);
  99. // let categoryCheck = this.category(ingObject.category, createBanner);
  100. // let unitCheck = this.unit(ingObject.unit, createBanner);
  101. // let quantityCheck = this.quantity(quantity, createBanner);
  102. // if(!nameCheck || !categoryCheck || !quantityCheck || !unitCheck){
  103. // return false;
  104. // }
  105. // return true;
  106. // }
  107. // },
  108. merchant: {
  109. password: function(pass, confirmPass, createBanner = true){
  110. if(pass !== confirmPass){
  111. if(createBanner){
  112. banner.createError("Your passwords do not match");
  113. }
  114. return false;
  115. }
  116. if(pass.length < 15){
  117. if(createBanner){
  118. banner.createError("Your password must contain at least 15 characters");
  119. }
  120. return false;
  121. }
  122. return true;
  123. }
  124. },
  125. transaction: {
  126. date: function(from, to = new Date(), createBanner = true){
  127. let errors = [];
  128. let today = new Date();
  129. if(from > to){
  130. errors.push("Starting date must be before ending date");
  131. }
  132. if(from > today || to > today.setDate(today.getDate() + 1)){
  133. errors.push("Cannot choose a date in the future");
  134. }
  135. if(errors.length > 0){
  136. if(createBanner){
  137. for(let error of errors){
  138. banner.createError(error);
  139. }
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. },
  146. recipe: function(newRecipe, createBanner = true){
  147. let errors = [];
  148. if(!validator.isSanitary(newRecipe.name)){
  149. errors.push("Name contains invalid characters");
  150. }
  151. if(newRecipe.price < 0){
  152. errors.push("Price must contain a non-negative number");
  153. }
  154. if(newRecipe.ingredients.length === 0){
  155. errors.push("Must include at least one ingredient");
  156. }
  157. let checkSet = new Set();
  158. for(let ingredient of newRecipe.ingredients){
  159. if(ingredient.quantity < 0){
  160. errors.push("Quantity must contain a non-negative number");
  161. break;
  162. }
  163. checkSet.add(ingredient.ingredient);
  164. }
  165. if(checkSet.size !== newRecipe.ingredients.length){
  166. errors.push("Recipe contains duplicate ingredients");
  167. }
  168. if(isNaN(newRecipe.price) || newRecipe.price === "" || newRecipe.price< 0){
  169. errors.push("Must enter a valid price");
  170. }
  171. if(errors.length > 0){
  172. if(createBanner){
  173. for(let error of errors){
  174. banner.createError(error);
  175. }
  176. return false;
  177. }
  178. }
  179. return true;
  180. },
  181. order: function(order, createBanner = true){
  182. let errors = [];
  183. if(!validator.isSanitary(order.orderId, false)){
  184. errors.push("Your string contains illegal characters");
  185. }
  186. let now = new Date()
  187. if(order.date > now){
  188. errors.push("Cannot have a date/time in the future");
  189. }
  190. for(let i = 0; i < order.ingredients.length; i++){
  191. if(order.ingredients[i].quantity < 0){
  192. errors.push("Quantity cannot be negative");
  193. break;
  194. }
  195. if(order.ingredients[i].price < 0){
  196. errors.push("Price cannot be negative");
  197. break;
  198. }
  199. if(order.ingredients[i].price === "" || order.ingredients[i].quantity === ""){
  200. errors.push("Incomplete information");
  201. }
  202. }
  203. if(errors.length > 0){
  204. if(createBanner){
  205. for(let error of errors){
  206. banner.createError(error);
  207. }
  208. }
  209. return false;
  210. }
  211. return true;
  212. },
  213. isSanitary: function(str, createBanner = true){
  214. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  215. for(let char of disallowed){
  216. if(str.includes(char)){
  217. if(createBanner){
  218. banner.createError("Your string contains illegal characters");
  219. }
  220. return false;
  221. }
  222. }
  223. return true;
  224. }
  225. }