From 70065774487096ed2edf35f618ba05db5845e497 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Mon, 3 Jul 2023 23:38:41 +0200
Subject: [PATCH] Validators => Rename array to json san. + Add enonce
 validator

---
 ExpressAPI/src/helpers/DojoValidators.ts | 46 +++++++++++++++++-------
 ExpressAPI/src/routes/EnonceRoutes.ts    |  2 +-
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/ExpressAPI/src/helpers/DojoValidators.ts b/ExpressAPI/src/helpers/DojoValidators.ts
index fb53873..09b7c82 100644
--- a/ExpressAPI/src/helpers/DojoValidators.ts
+++ b/ExpressAPI/src/helpers/DojoValidators.ts
@@ -49,19 +49,19 @@ class DojoValidators {
                                                                }
                                                            });
 
-    readonly arraySanitizer = this.toValidatorSchemaOptions({
-                                                                options: (value, {
-                                                                    req,
-                                                                    location,
-                                                                    path
-                                                                }) => {
-                                                                    try {
-                                                                        return JSON.parse(value);
-                                                                    } catch ( e ) {
-                                                                        return value;
-                                                                    }
-                                                                }
-                                                            });
+    readonly jsonSanitizer = this.toValidatorSchemaOptions({
+                                                               options: (value, {
+                                                                   req,
+                                                                   location,
+                                                                   path
+                                                               }) => {
+                                                                   try {
+                                                                       return JSON.parse(value);
+                                                                   } catch ( e ) {
+                                                                       return value;
+                                                                   }
+                                                               }
+                                                           });
 
     readonly templateUrlValidator = this.toValidatorSchemaOptions({
                                                                       bail        : true,
@@ -101,6 +101,26 @@ class DojoValidators {
                                                                           return value;
                                                                       }
                                                                   });
+
+    readonly enonceValidator = this.toValidatorSchemaOptions({
+                                                                 bail        : true,
+                                                                 errorMessage: 'Template doesn\'t exist or you don\'t have access to it',
+                                                                 options     : (value, {
+                                                                     req,
+                                                                     location,
+                                                                     path
+                                                                 }) => {
+                                                                     return new Promise((resolve, reject) => {
+                                                                         const template = this.getParamValue(req, path);
+                                                                         if ( template ) {
+                                                                             GitlabManager.checkTemplateAccess(template, req).then((templateAccess) => {
+                                                                                 templateAccess !== StatusCodes.OK ? reject() : resolve(true);
+                                                                             });
+                                                                         }
+                                                                         resolve(true);
+                                                                     });
+                                                                 }
+                                                             });
 }
 
 
diff --git a/ExpressAPI/src/routes/EnonceRoutes.ts b/ExpressAPI/src/routes/EnonceRoutes.ts
index 0bda88a..dd6320d 100644
--- a/ExpressAPI/src/routes/EnonceRoutes.ts
+++ b/ExpressAPI/src/routes/EnonceRoutes.ts
@@ -43,7 +43,7 @@ class EnonceRoutes implements RoutesManager {
         members : {
             trim           : true,
             notEmpty       : true,
-            customSanitizer: DojoValidators.arraySanitizer
+            customSanitizer: DojoValidators.jsonSanitizer
         },
         template: {
             trim           : true,
-- 
GitLab