All files / app/validators inject-custom-checks.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 471x       1x 1x       1x         1x                                     1x 2x 6x 22x 6x   16x              
import {
  GroupValidators,
  GroupValidatorsImpl,
} from '@app/validators/group-validators';
import {ValidatorsImpl} from 'express-validator/src/chain';
import {
  UserValidators,
  UserValidatorsImpl,
} from '@app/validators/user-validators';
import {CarValidators, CarValidatorsImpl} from '@app/validators/car-validators';
 
export type ExtendedValidationChain = GroupValidators &
  UserValidators &
  CarValidators;
const customValidators = [
  GroupValidatorsImpl,
  UserValidatorsImpl,
  CarValidatorsImpl,
];
 
/**
 * Injects custom validation checks into the validation chain
 * of **express-validator**.
 *
 * This function also requires
 * a modification of the `ValidationChain` type in
 * `typings/index.d.ts` for typescript to properly
 * detect the added custom check methods.
 *
 * This function should be called somewhere at
 * the start of the app so that custom validators
 * are always added.
 */
export const injectCustomChecks = (): void => {
  for (const validators of customValidators) {
    const validatorsNames = Object.getOwnPropertyNames(validators.prototype)
        .filter((name) => name !== 'constructor');
    for (const name of validatorsNames) {
      /* eslint-disable @typescript-eslint/no-explicit-any */
      (ValidatorsImpl.prototype as any)[name] =
        (validators.prototype as any)[name];
      /* eslint-enable @typescript-eslint/no-explicit-any */
    }
  }
};