Skip to content
Snippets Groups Projects
Select Git revision
  • 70065774487096ed2edf35f618ba05db5845e497
  • main default protected
  • add_export_route
  • add_route_assignments
  • 4.1.0-dev
  • 4.0.0
  • 3.5.3
  • 3.5.3-dev
  • 3.5.2
  • 3.5.2-dev
  • 3.5.1
  • 3.5.1-dev
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.4.0
  • 3.3.0
  • 3.2.0
  • 3.1.3
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
  • 3.0.0
24 results

DojoValidators.ts

Blame
  • Forked from Dojo Project (HES-SO) / Projects / Backend / DojoBackendAPI
    Source project has a limited visibility.
    DojoValidators.ts 8.77 KiB
    import ApiRequest                                                   from '../models/ApiRequest';
    import Config                                                       from '../config/Config';
    import { StatusCodes }                                              from 'http-status-codes';
    import { CustomValidator, ErrorMessage, FieldMessageFactory, Meta } from 'express-validator/src/base';
    import { BailOptions, ValidationChain }                             from 'express-validator/src/chain';
    import GitlabManager                                                from '../managers/GitlabManager';
    
    
    declare type DojoMeta = Meta & {
        req: ApiRequest
    };
    
    declare type DojoCustomValidator = (input: any, meta: DojoMeta) => any;
    
    
    class DojoValidators {
        private static _instance: DojoValidators;
    
        private constructor() { }
    
        public static get instance(): DojoValidators {
            if ( !DojoValidators._instance ) {
                DojoValidators._instance = new DojoValidators();
            }
    
            return DojoValidators._instance;
        }
    
        private toValidatorSchemaOptions(arg: { errorMessage?: FieldMessageFactory | ErrorMessage, negated?: boolean, bail?: boolean | BailOptions, if?: DojoCustomValidator | ValidationChain, options?: DojoCustomValidator }) {
            // This is a hack to make the types work with req arg as ApiRequest instead of Request
            return arg as unknown as { errorMessage?: FieldMessageFactory | ErrorMessage, negated?: boolean, bail?: boolean | BailOptions, if?: CustomValidator | ValidationChain, options?: CustomValidator };
        }
    
        private getParamValue(req: ApiRequest, path: string): any {
            return 'body' in req && path in req.body ? req.body[path] : req.query[path];
        }
    
        readonly nullSanitizer = this.toValidatorSchemaOptions({
                                                                   options: (value, {
                                                                       req,
                                                                       location,
                                                                       path
                                                                   }) => {
                                                                       try {
                                                                           return value == 'null' || value == 'undefined' || value == '' ? null : 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,
                                                                          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);
                                                                              });
                                                                          }
                                                                      });
    
        readonly templateUrlSanitizer = this.toValidatorSchemaOptions({
                                                                          options: (value, {
                                                                              req,
                                                                              location,
                                                                              path
                                                                          }) => {
                                                                              try {
                                                                                  const template = this.getParamValue(req, path);
                                                                                  if ( template ) {
                                                                                      return `${ Config.gitlab.urls[0].replace(/^([a-z]{3,5}:\/{2})?(.*)/, `$1${ Config.gitlab.account.username }:${ Config.gitlab.account.token }@$2`) }${ template }.git`;
                                                                                  } else {
                                                                                      return Config.enonce.default.template;
                                                                                  }
                                                                              } catch ( e ) { }
    
                                                                              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);
                                                                         });
                                                                     }
                                                                 });
    }
    
    
    export default DojoValidators.instance;