ソースを参照

Update validation for all recipe routes

Lee Morgan 6 年 前
コミット
cd2641386c
2 ファイル変更39 行追加6 行削除
  1. 10 0
      controllers/recipeData.js
  2. 29 6
      controllers/validator.js

+ 10 - 0
controllers/recipeData.js

@@ -20,6 +20,11 @@ module.exports = {
             return res.redirect("/");
         }
 
+        let validation = Validator(req.body);
+        if(validation !== true){
+            return res.json(validation);
+        }
+
         let recipe = new Recipe({
             merchant: req.session.user,
             name: req.body.name,
@@ -67,6 +72,11 @@ module.exports = {
             return res.redirect("/");
         }
 
+        let validation = Validator(req.body);
+        if(validation !== true){
+            return res.json(validation);
+        }
+
         Recipe.findOne({_id: req.body.id})
             .then((recipe)=>{
                 recipe.name = req.body.name;

+ 29 - 6
controllers/validator.js

@@ -36,21 +36,44 @@ module.exports = {
         return true;
     },
 
+    quantity: function(num){
+        if(isNaN(num) || num === ""){
+            return "Quantity must be a number";
+        }
+
+        if(num < 0){
+            return "Quantity cannot be a negative number";
+        }
+
+        return true;
+    },
+
     ingredient: function(ingredient){
         if(!this.isSanitary([ingredient.name, ingredient.category, ingredient.unit])){
-            return false;
+            return "Ingredient contains illegal characters";
         }
 
         return true;
     },
 
-    quantity: function(num){
-        if(isNaN(num) || num === ""){
-            return false;
+    recipe: function(recipe){
+        if(!this.isSanitary([recipe.name])){
+            return "Ingredient contains illegal characters";
         }
 
-        if(num < 0){
-            return false;
+        if(isNaN(recipe.price) || recipe.price === ""){
+            return "Price must be a number";
+        }
+
+        if(recipe.price < 0){
+            return "Price cannot be a negative number";
+        }
+
+        for(let i = 0; i < recipe.ingredients.length; i++){
+            let checkQuantity = this.quantity(recipe.ingredients[i].quantity);
+            if(checkQuantity !== true){
+                return checkQuantity;
+            }
         }
 
         return true;