| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- let validator = {
- ingredient: {
- name: function(ingName, createBanner = true){
- //Check for special chars
- if(!validator.isSanitary(ingName)){
- if(createBanner){
- banner.createError("Your inputs contain illegal characters");
- }
- return false;
- }
- //Check for length
- if(ingName.length < 2){
- if(createBanner){
- banner.createError("Ingredient name must contain at least 2 characters");
- }
- return false;
- }
- return true;
- },
- category: function(ingCategory, createBanner = true){
- //Check for special chars
- if(!validator.isSanitary(ingCategory)){
- if(createBanner){
- banner.createError("Your inputs contain illegal characters");
- }
- return false;
- }
- //Check for length
- if(ingCategory.length < 3){
- if(createBanner){
- banner.createError("Category name must contain at least 3 characters");
- }
- return false;
- }
- return true;
- },
- quantity: function(num, createBanner = true){
- if(isNaN(num) || num === ""){
- if(createBanner){
- banner.createError("Must enter a valid number");
- }
- return false;
- }
- if(num < 0){
- if(createBanner){
- banner.createError("Quantity cannot be a negative number");
- }
- return false;
- }
- return true;
- },
- unit: function(ingUnit, createBanner = true){
- //Check for special chars
- if(!validator.isSanitary(ingUnit)){
- if(createBanner){
- banner.createError("Your inputs contain illegal characters");
- }
- return false;
- }
- return true;
- },
- //Check all parts of ingredient, return true if all pass
- //Quantity passed seperately and optional
- all: function(ingObject, quantity = 0, createBanner = true){
- let nameCheck = this.name(ingObject.name, createBanner);
- let categoryCheck = this.category(ingObject.category, createBanner);
- let unitCheck = this.unit(ingObject.unit, createBanner);
- let quantityCheck = this.quantity(quantity, createBanner);
- if(!nameCheck || !categoryCheck || !quantityCheck || !unitCheck){
- return false;
- }
- return true;
- }
- },
- merchant: {
- password: function(pass, confirmPass, createBanner = true){
- if(pass !== confirmPass){
- if(createBanner){
- banner.createError("Your passwords do not match");
- }
- return false;
- }
- if(pass.length < 15){
- if(createBanner){
- banner.createError("Your password must contain at least 15 characters");
- }
- return false;
- }
- return true;
- }
- },
- transaction: {
- date: function(from, to = new Date(), createBanner = true){
- let errors = [];
- let today = new Date();
- if(from > to){
- errors.push("Starting date must be before ending date");
- }
- if(from > today || to > today.setDate(today.getDate() + 1)){
- errors.push("Cannot choose a date in the future");
- }
- if(errors.length > 0){
- if(createBanner){
- for(let error of errors){
- banner.createError(error);
- }
- return false;
- }
- }
- return true;
- }
- },
- recipe: function(newRecipe, createBanner = true){
- let errors = [];
- if(!validator.isSanitary(newRecipe.name)){
- errors.push("Name contains invalid characters");
- }
- if(newRecipe.price < 0){
- errors.push("Price must contain a non-negative number");
- }
- if(newRecipe.ingredients.length === 0){
- errors.push("Must include at least one ingredient");
- }
- let checkSet = new Set();
- for(let ingredient of newRecipe.ingredients){
- if(ingredient.quantity < 0){
- errors.push("Quantity must contain a non-negative number");
- break;
- }
- checkSet.add(ingredient.ingredient);
- }
- if(checkSet.size !== newRecipe.ingredients.length){
- errors.push("Recipe contains duplicate ingredients");
- }
- if(isNaN(newRecipe.price) || newRecipe.price === "" || newRecipe.price< 0){
- errors.push("Must enter a valid price");
- }
- if(errors.length > 0){
- if(createBanner){
- for(let error of errors){
- banner.createError(error);
- }
- return false;
- }
- }
- return true;
- },
- order: function(order, createBanner = true){
- let errors = [];
- if(!validator.isSanitary(order.orderId, false)){
- errors.push("Your string contains illegal characters");
- }
- let now = new Date()
- if(order.date > now){
- errors.push("Cannot have a date/time in the future");
- }
- for(let i = 0; i < order.ingredients.length; i++){
- if(order.ingredients[i].quantity < 0){
- errors.push("Quantity cannot be negative");
- break;
- }
- if(order.ingredients[i].price < 0){
- errors.push("Price cannot be negative");
- break;
- }
- if(order.ingredients[i].price === "" || order.ingredients[i].quantity === ""){
- errors.push("Incomplete information");
- }
- }
- if(errors.length > 0){
- if(createBanner){
- for(let error of errors){
- banner.createError(error);
- }
- }
- return false;
- }
- return true;
- },
- isSanitary: function(str, createBanner = true){
- let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
- for(let char of disallowed){
- if(str.includes(char)){
- if(createBanner){
- banner.createError("Your string contains illegal characters");
- }
- return false;
- }
- }
- return true;
- }
- }
|