Просмотр исходного кода

Replace all validation in merchantData.js file.

Lee Morgan 5 лет назад
Родитель
Сommit
9736fdf8ff
5 измененных файлов с 48 добавлено и 21 удалено
  1. 19 10
      controllers/merchantData.js
  2. 0 1
      controllers/transactionData.js
  3. 14 0
      controllers/validator.js
  4. 10 5
      models/ingredient.js
  5. 5 5
      models/merchant.js

+ 19 - 10
controllers/merchantData.js

@@ -57,8 +57,9 @@ module.exports = {
                 return res.redirect(`/verify/email/${merchant._id}`);
             })
             .catch((err)=>{
-                if(err.errors.name.properties.type === "user defined"){
-                    req.session.error = err.errors.name.properties.message;
+                const error = err.errors.name.properties;
+                if(error.type === "user defined"){
+                    req.session.error = error.message;
                 }else{
                     req.session.error = "ERROR: UNABLE TO CREATE ACCOUNT AT THIS TIME";
                 }
@@ -212,15 +213,8 @@ module.exports = {
             return res.redirect("/");
         }
 
-        for(let i = 0; i < req.body.length; i++){
-            let validation = validator.quantity(req.body[i].quantity);
-            if(validation !== true){
-                return res.json(validation);
-            }
-        }
-
         let adjustments = [];
-        let changedIngredients = []
+        let changedIngredients = [];
         Merchant.findOne({_id: req.session.user})
             .populate("inventory.ingredient")
             .then((merchant)=>{
@@ -253,6 +247,11 @@ module.exports = {
                 return;
             })
             .catch((err)=>{
+                const error = err.errors.name.properties;
+                if(error.type === "user defined" || error.type === "minlength"){
+                    return res.json(error.message);
+                }
+
                 return res.json("ERROR: UNABLE TO UPDATE DATA");
             });        
     },
@@ -271,6 +270,16 @@ module.exports = {
             return res.json(validation);
         }
 
+        if(req.body.pass.length < 10){
+            req.session.error = "PASSWORD MUST CONTAIN AT LEAST 10 CHARACTERS";
+            return res.redirect("/");
+        }
+
+        if(req.body.pass !== req.body.confirmPass){
+            req.session.error = "PASSWORDS DO NOT MATCH";
+            return res.redirect("/");
+        }
+
         Merchant.findOne({password: req.body.hash})
             .then((merchant)=>{
                 if(merchant){

+ 0 - 1
controllers/transactionData.js

@@ -111,7 +111,6 @@ module.exports = {
                 return res.json(transactions);
             })
             .catch((err)=>{
-                console.log(err);
                 return res.json("ERROR: UNABLE TO RETRIEVE DATA");
             });
     },

+ 14 - 0
controllers/validator.js

@@ -142,6 +142,20 @@ module.exports = {
             }
         }
 
+        return true;
+    },
+
+    isSanitary: function(strings){
+        let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
+
+        for(let i = 0; i < strings.length; i++){
+            for(let j = 0; j < disallowed.length; j++){
+                if(strings[i].includes(disallowed[j])){
+                    return false;
+                }
+            }
+        }
+
         return true;
     }
 }

+ 10 - 5
models/ingredient.js

@@ -9,16 +9,21 @@ let sanitary = (value)=>{
 const IngredientSchema = new mongoose.Schema({
     name: {
         type: String,
-        minlength: 2,
-        required: true,
+        minlength: [2, "INGREDIENT NAME MUST CONTAIN AT LEAST 2 CHARACTERS"],
+        required: [true, "INGREDIENT NAME IS REQUIRED"],
         validate: {
-
+            validator: sanitary,
+            message: "INGREDIENT NAME CONTAINS ILLEGAL CHARACTERS"
         }
     },
     category: {
         type: String,
-        minlength: 2,
-        required: true
+        minlength: [2, "INGREDIENT CATEGORY MUST CONTAIN AT LEAST 2 CHARACTERS"],
+        required: [true, "INGREDIENT CATEGORY IS REQUIRED"],
+        validate: {
+            validator: sanitary,
+            message: "INGREDIENT CATEGORY CONTAINS ILLEGAL CHARACTERS"
+        }
     },
     unitType: {
         type: String,

+ 5 - 5
models/merchant.js

@@ -13,7 +13,7 @@ let emailValid = (value)=>{
 const MerchantSchema = new mongoose.Schema({
     name: {
         type: String,
-        required: true,
+        required: [true, "MERCHANT NAME IS REQUIRED"],
         validate: {
             validator: nameSanitary,
             message: "NAME CONTAINS ILLEGAL CHARACTERS"
@@ -21,7 +21,7 @@ const MerchantSchema = new mongoose.Schema({
     },
     email: {
         type: String,
-        required: true,
+        required: [true, "EMAIL IS REQUIRED"],
         validate: {
             validator: emailValid,
             message: "INVALID EMAIL ADDRESS"
@@ -55,12 +55,12 @@ const MerchantSchema = new mongoose.Schema({
         },
         quantity: {
             type: Number,
-            required: true,
-            min: 0
+            required: [true, "INGREDIENT QUANTITY IS REQUIRED"],
+            min: [0, "QUANTITY CANNOT BE A NEGATIVE NUMBER"]
         },
         defaultUnit: {
             type: String,
-            required: true
+            required: [true, "INGREDIENT UNIT IS REQUIRED"]
         }
     }],
     recipes: [{