Преглед изворни кода

Add validation to the creation of new recipes

Lee Morgan пре 6 година
родитељ
комит
c468a10331
3 измењених фајлова са 48 додато и 12 уклоњено
  1. 1 0
      views/dashboardPage/dashboard.ejs
  2. 5 12
      views/dashboardPage/recipeBook.js
  3. 42 0
      views/shared/validation.js

+ 1 - 0
views/dashboardPage/dashboard.ejs

@@ -101,5 +101,6 @@
         <script src="/dashboardPage/ingredients.js"></script>
         <script src="/dashboardPage/recipeBook.js"></script>
         <script src="/dashboardPage/controller.js"></script>
+        <script src="../shared/validation.js"></script>
     </body>
 </html>

+ 5 - 12
views/dashboardPage/recipeBook.js

@@ -35,8 +35,6 @@ window.recipeBookStrandObj = {
                 }
             }
 
-            console.log("ranned");
-
             this.isPopulated = true;
         }
     },
@@ -111,20 +109,15 @@ window.recipeBookStrandObj = {
         }
 
         let inputs = document.querySelectorAll("#recipeInputIngredients > div");
-        let duplicateCheck = new Set();
         for(let input of inputs){
-            let id = input.children[1].children[0].value;
-
             newRecipe.ingredients.push({
-                ingredient: id,
+                ingredient: input.children[1].children[0].value,
                 quantity: input.children[2].children[0].value
             });
+        }
 
-            duplicateCheck.add(id);
-            if(duplicateCheck.size !== newRecipe.ingredients.length){
-                banner.createError("You have duplicate ingredients in your recipe");
-                return;
-            }
+        if(!validator.recipe(newRecipe)){
+            return;
         }
 
         fetch("/recipe/create", {
@@ -139,7 +132,7 @@ window.recipeBookStrandObj = {
                 if(typeof(response.data) === "string"){
                     banner.createError(response.data);
                 }else{
-                    banner.createNotification("New recipe successfully penised")
+                    banner.createNotification("New recipe successfully created")
                 }
             })
             .catch((err)=>{

+ 42 - 0
views/shared/validation.js

@@ -137,6 +137,48 @@ let validator = {
         }
     },
 
+    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(errors.length > 0){
+            if(createBanner){
+                for(let error of errors){
+                    banner.createError(error);
+                }
+
+                return false;
+            }
+        }
+
+        return true;
+    },
+
     isSanitary: function(str){
         let disallowed = ["\\", "<", ">", "$"];