File

src/common/decorators/validdomain.decorator.ts

Index

Methods

Constructor

constructor(configService: ConfigService)
Parameters :
Name Type Optional
configService ConfigService No

Methods

defaultMessage
defaultMessage()
Returns : any
validate
validate(value: any)
Parameters :
Name Type Optional
value any No
Returns : any
import {
  ValidatorConstraintInterface,
  ValidatorConstraint,
  ValidationOptions,
  registerDecorator,
} from 'class-validator';

import { ConfigService } from '@nestjs/config';
import { Injectable } from '@nestjs/common';
import { GenericRespEnum } from '../../utils/enums/responses.enum';

@ValidatorConstraint({ name: 'ValidDomain' })
@Injectable()
export class ValidDomainRule implements ValidatorConstraintInterface {
  constructor(private readonly configService: ConfigService) {}

  validate(value: any) {
    return this.configService.get('domains').includes(value);
  }
  defaultMessage() {
    return GenericRespEnum.ERR_INVALID_DOMAIN;
  }
}

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

results matching ""

    No results matching ""