Forráskód Böngészése

Update validation for all order routes

Lee Morgan 6 éve
szülő
commit
3f58fd84d3
2 módosított fájl, 44 hozzáadás és 6 törlés
  1. 5 0
      controllers/orderData.js
  2. 39 6
      controllers/validator.js

+ 5 - 0
controllers/orderData.js

@@ -59,6 +59,11 @@ module.exports = {
             return res.redirect("/");
         }
 
+        let validation = Validator.order(req.body);
+        if(validation !== true){
+            return res.json(validation);
+        }
+
         let newOrder = new Order(req.body);
         newOrder.merchant = req.session.user;
         newOrder.save()

+ 39 - 6
controllers/validator.js

@@ -48,6 +48,18 @@ module.exports = {
         return true;
     },
 
+    price: function(price){
+        if(price < 0){
+            return "Price cannot be a negative number";
+        }
+
+        if(isNaN(price) || price === ""){
+            return "Price must be a number";
+        }
+
+        return true;
+    },
+
     ingredient: function(ingredient){
         if(!this.isSanitary([ingredient.name, ingredient.category, ingredient.unit])){
             return "Ingredient contains illegal characters";
@@ -61,12 +73,9 @@ module.exports = {
             return "Ingredient contains illegal characters";
         }
 
-        if(isNaN(recipe.price) || recipe.price === ""){
-            return "Price must be a number";
-        }
-
-        if(recipe.price < 0){
-            return "Price cannot be a negative number";
+        let priceCheck = this.price(recipe.price);
+        if(priceCheck !== true){
+            return priceCheck;
         }
 
         for(let i = 0; i < recipe.ingredients.length; i++){
@@ -79,6 +88,30 @@ module.exports = {
         return true;
     },
 
+    order: function(order){
+        if(!this.isSanitary([order.orderId])){
+            return "Order name contains illegal characters";
+        }
+
+        if(new Date(order.date) > new Date()){
+            return "Date cannot be in the future";
+        }
+
+        for(let i = 0; i < order.ingredients; i++){
+            let quantityCheck = this.quantity(order.ingredients[i].quantity);
+            if(quantityCheck !== true){
+                return quantityCheck;
+            }
+
+            let priceCheck = this.price(order.ingredients[i].price);
+            if(priceCheck !== true){
+                return priceCheck;
+            }
+        }
+
+        return true;
+    },
+
     isSanitary: function(strings){
         let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];