Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

nest custom class validator

useContainer(app.select(AppModule), { fallbackOnErrors: true });

@ValidatorConstraint({ name: 'UserExists', async: true })
@Injectable()
export class UserExistsRule implements ValidatorConstraintInterface {
  constructor(private usersRepository: UsersRepository) {}

  async validate(value: number) {
    try {
      await this.usersRepository.getOneOrFail(value);
    } catch (e) {
      return false;
    }

    return true;
  }

  defaultMessage(args: ValidationArguments) {
    return `User doesn't exist`;
  }
}

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

Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript object of objects 
Typescript :: check if that inex exisits array c# 
Typescript :: react update state array of objects hooks 
Typescript :: install dependencies angular 
Typescript :: how to concate a string to all elements in a list in python 
Typescript :: token authentication requirements for git operations 
Typescript :: angular build Failed to load resource 
Typescript :: how to separate a string into 2 lists of numbers and letters python 
Typescript :: traits c++ 
Typescript :: instruments du marché monétaire 
Cpp :: dart and or 
Cpp :: ‘setprecision’ was not declared in this scope 
Cpp :: qt get hexa value from qstring 
Cpp :: cpp get data type 
Cpp :: c++ how to loop through a vector but not the last element 
Cpp :: c++ round number down 
Cpp :: set platformio to C++17 
Cpp :: split vector in half cpp 
Cpp :: c++ fill array with 0 
Cpp :: master header file c++ 
Cpp :: C++ Kelvin to Celsius 
Cpp :: make_move_iterator 
Cpp :: c++ save typeid 
Cpp :: c++ remove last element from vector 
Cpp :: regex for phone number c++ 
Cpp :: print queue c++ 
Cpp :: note++ 
Cpp :: winmain example 
Cpp :: convert string to stream c++ 
Cpp :: c++ main function 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =