Răsfoiți Sursa

Add or update all validation on all of the necessary models.
Only added validation for those that may be seen on the frontend.

Lee Morgan 5 ani în urmă
părinte
comite
0e59996d53

+ 5 - 10
controllers/ingredientData.js

@@ -116,17 +116,12 @@ module.exports = {
             req.session.error = "MUST BE LOGGED IN TO DO THAT";
             return res.redirect("/");
         }
-
-        const ingredientCheck = validator.ingredient(req.body);
-        if(ingredientCheck !== true){
-            return res.json(ingredientCheck);
-        }
-
         let updatedIngredient = {};
         Ingredient.findOne({_id: req.body.id})
             .then((ingredient)=>{
                 ingredient.name = req.body.name,
                 ingredient.category = req.body.category
+                //TODO: Handle this in the class
                 if(ingredient.specialUnit === "bottle"){
                     ingredient.unitSize = req.body.unitSize;
                 }
@@ -166,7 +161,10 @@ module.exports = {
                 return res.json(updatedIngredient);
             })
             .catch((err)=>{
-                return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                return res.json(err.errors.name.properties.message);
             });
     },
 
@@ -285,11 +283,8 @@ module.exports = {
         }
 
         //Update the database
-        let createdIngredients = [];
         Ingredient.create(ingredients)
             .then((ingredients)=>{
-                createdIngredients = ingredients;
-
                 return Merchant.findOne({_id: req.session.user});
             })
             .then((merchant)=>{

+ 0 - 1
controllers/merchantData.js

@@ -251,7 +251,6 @@ module.exports = {
                 if(error.type === "user defined" || error.type === "minlength"){
                     return res.json(error.message);
                 }
-
                 return res.json("ERROR: UNABLE TO UPDATE DATA");
             });        
     },

+ 4 - 8
models/ingredient.js

@@ -1,18 +1,14 @@
-const helper = require("../controllers/helper.js");
+const isSanitary = require("../controllers/helper.js").isSanitary;
 
 const mongoose = require("mongoose");
 
-let sanitary = (value)=>{
-    return helper.isSanitary(value);
-}
-
 const IngredientSchema = new mongoose.Schema({
     name: {
         type: String,
         minlength: [2, "INGREDIENT NAME MUST CONTAIN AT LEAST 2 CHARACTERS"],
         required: [true, "INGREDIENT NAME IS REQUIRED"],
         validate: {
-            validator: sanitary,
+            validator: isSanitary,
             message: "INGREDIENT NAME CONTAINS ILLEGAL CHARACTERS"
         }
     },
@@ -21,13 +17,13 @@ const IngredientSchema = new mongoose.Schema({
         minlength: [2, "INGREDIENT CATEGORY MUST CONTAIN AT LEAST 2 CHARACTERS"],
         required: [true, "INGREDIENT CATEGORY IS REQUIRED"],
         validate: {
-            validator: sanitary,
+            validator: isSanitary,
             message: "INGREDIENT CATEGORY CONTAINS ILLEGAL CHARACTERS"
         }
     },
     unitType: {
         type: String,
-        required: true
+        required: [true, "UNIT TYPE IS REQUIRED"]
     },
     specialUnit: String,
     unitSize: String

+ 4 - 8
models/merchant.js

@@ -1,11 +1,7 @@
-const helper = require("../controllers/helper.js");
+const isSanitary = require("../controllers/helper.js").isSanitary;
 
 const mongoose = require("mongoose");
 
-let nameSanitary = (value)=>{
-    return helper.isSanitary(value);
-}
-
 let emailValid = (value)=>{
     /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value);
 }
@@ -15,7 +11,7 @@ const MerchantSchema = new mongoose.Schema({
         type: String,
         required: [true, "MERCHANT NAME IS REQUIRED"],
         validate: {
-            validator: nameSanitary,
+            validator: isSanitary,
             message: "NAME CONTAINS ILLEGAL CHARACTERS"
         }
     },
@@ -29,7 +25,7 @@ const MerchantSchema = new mongoose.Schema({
     },
     password: {
         type: String,
-        required: true,
+        required: [true, "MUST PROVIDE A PASSWORD"],
     },
     pos: {
         type: String,
@@ -51,7 +47,7 @@ const MerchantSchema = new mongoose.Schema({
         ingredient: {
             type: mongoose.Schema.Types.ObjectId,
             ref: "Ingredient",
-            required: true
+            required: [true, "MUST PROVIDE THE INGREDIENT"]
         },
         quantity: {
             type: Number,

+ 18 - 15
models/order.js

@@ -1,39 +1,42 @@
+const isSanitary = require("../controllers/helper.js").isSanitary;
+
 const mongoose = require("mongoose");
 
 const OrderSchema = new mongoose.Schema({
     merchant: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Merchant",
-        required: true
+        required: [true, "MUST PROVIDE THE MERCHANT"]
+    },
+    name: {
+        type: String,
+        minlength: [2, "ORDER NAME MUST CONTAIN AT LEAST 2 CHARACTERS"],
+        validate: {
+            validator: isSanitary,
+            message: "ORDER NAME CONTAINS ILLEGAL CHARACTERS"
+        }
     },
-    name: String,
     date: {
         type: Date,
         default: Date.now,
-        required: true
-    },
-    taxes: {
-        type: Number,
-        required: true
-    },
-    fees: {
-        type: Number,
-        required: true
+        required: [true, "MUST PROVIDE A DATE FOR THE ORDER"]
     },
+    taxes: Number,
+    fees: Number,
     ingredients: [{
         ingredient: {
             type: mongoose.Schema.Types.ObjectId,
             ref: "Ingredient",
-            required: true
+            required: [true, "MUST PROVIDE THE INGREDIENT"]
         },
         quantity: {
             type: Number,
-            required: true,
-            min: 0
+            required: [true, "MUST PROVIDE THE QUANTITY FOR EVERY INGREDIENT IN THE ORDER"],
+            min: [0, "INGREDIENT QUANTITY IN AN ORDER CANNOT BE LESS THAN 0"]
         },
         pricePerUnit: {
             type: Number,
-            min: 0
+            min: [0, "PRICE PER UNIT CANNOT BE A NEGATIVE NUMBER"]
         }
     }]
 });

+ 14 - 6
models/recipe.js

@@ -1,3 +1,5 @@
+const isSanitary = require("../controllers/helper.js").isSanitary;
+
 const mongoose = require("mongoose");
 
 const RecipeSchema = new mongoose.Schema({
@@ -5,26 +7,32 @@ const RecipeSchema = new mongoose.Schema({
     merchant: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Merchant",
-        required: true
+        required: [true, "MERCHANT IS REQUIRED"]
     },
     name: {
         type: String,
-        required: true,
+        minlength: [2, "RECIPE NAME MUST CONTAIN AT LEAST 2 CHARACTERS"],
+        validate: {
+            validator: isSanitary,
+            message: "RECIPE NAME CONTAINS ILLEGAL CHARACTERS"
+        },
+        required: true
     },
     price: {
         type: Number,
-        min: 0
+        min: [0, "PRICE OF RECIPE CANNOT BE A NEGATIVE NUMBER"],
+        required: [true, "RECIPE PRICE IS REQUIRED"]
     },
     ingredients: [{
         ingredient: {
             type: mongoose.Schema.Types.ObjectId,
             ref: "Ingredient",
-            required: [true, "Must provide ingredient"]
+            required: [true, "INGREDIENT IS REQUIRED"]
         },
         quantity: {
             type: Number,
-            min: [0, "Cannot have a negative quantity"],
-            required: [true, "Must provide a quantity"]
+            min: [0, "QUANTITY OF INGREDIENTS CANNOT BE A NEGATIVE NUMBER"],
+            required: [true, "MUST PROVED A QUANTITY FOR ALL INGREDIENTS"]
         }
     }]
 });

+ 0 - 5
views/dashboardPage/bundle.js

@@ -127,11 +127,6 @@ module.exports = Ingredient;
 },{}],2:[function(require,module,exports){
 class MerchantIngredient{
     constructor(ingredient, quantity){
-        if(quantity < 0){
-            banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
-            return false;
-        }
-        
         this._quantity = quantity;
         this._ingredient = ingredient;
     }

+ 0 - 5
views/dashboardPage/js/classes/Merchant.js

@@ -1,10 +1,5 @@
 class MerchantIngredient{
     constructor(ingredient, quantity){
-        if(quantity < 0){
-            banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
-            return false;
-        }
-        
         this._quantity = quantity;
         this._ingredient = ingredient;
     }