File

src/common/decorators/codevaild.decorator.ts

Index

Methods

Constructor

constructor(codesService: CodesService)
Parameters :
Name Type Optional
codesService CodesService No

Methods

defaultMessage
defaultMessage(args: ValidationArguments)
Parameters :
Name Type Optional
args ValidationArguments No
Returns : any
Async validate
validate(value: any, args: ValidationArguments)
Parameters :
Name Type Optional
value any No
args ValidationArguments No
Returns : unknown
import {
  ValidationArguments,
  ValidatorConstraintInterface,
  ValidatorConstraint,
  ValidationOptions,
  registerDecorator,
} from 'class-validator';
import { CodesService } from '../../codes/codes.service';
import { Injectable } from '@nestjs/common';
import { GenericRespEnum } from '../../utils/enums/responses.enum';

@ValidatorConstraint({ name: 'IsCodeValid', async: true })
@Injectable()
export class IsCodeValidRule implements ValidatorConstraintInterface {
  constructor(private readonly codesService: CodesService) {}

  async validate(value: any, args: ValidationArguments) {
    return await this.codesService.isCodeValid(value);
  }
  defaultMessage(args: ValidationArguments) {
    return GenericRespEnum.ERR_INVALID_CODE;
  }
}

export function IsCodeValid(validationOptions?: ValidationOptions) {
  return function (object: any, propertyName: string) {
    registerDecorator({
      name: 'IsCodeValid',
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      validator: IsCodeValidRule,
    });
  };
}

results matching ""

    No results matching ""