How to get request in class-validator constraint in nestjs
Asked Answered
A

1

10

If I want to validate the role which user post to api to create if is unique of relation enterprise

@Injectable({ scope: Scope.REQUEST })
@ValidatorConstraint({ name: 'Ddd', async: true })
export class IsUnqiueForEnterpriseConstraint implements ValidatorConstraintInterface {
    constructor(@Inject(REQUEST) private request: Request) {}

    async validate(value: any, args: ValidationArguments) {
        const { enterprise } = this.request.user as any
        const { model } = args.constraints[0] as IParams;
        if(!enterprise) return false;
        if (!model) return false;
        const repo = getManager().getRepository(model as ObjectType<any>);
        const item = await repo.findOne({
            [args.property]: value,
            enterprise: { id: enterprise.id },
        });
        return !item;
    }

    defaultMessage(args: ValidationArguments) {
        const { model } = args.constraints[0];
        if (!model) {
            return 'Model not been specified!';
        }
        return `${args.property} of ${model.name} must been unique!`;
    }
}
export function IsUnqiueForEnterprise(params: IParams, validationOptions?: ValidationOptions) {
    return (object: Record<string, any>, propertyName: string) => {
        registerDecorator({
            target: object.constructor,
            propertyName,
            options: validationOptions,
            constraints: [params],
            validator: IsUnqiueForEnterpriseConstraint,
        });
    };
}

And in main.ts I will container class-validator like follow

useContainer(app, { fallbackOnErrors: true });

and dto

@IsUnqiueForEnterprise({model: Role})
label!: string;

But in constraint request is undefined,how can I get it?

Assyria answered 13/10, 2020 at 10:44 Comment(0)
L
2

You should perform multiple steps:

1- Create a ContextInterceptor that injects user info into request when it has body (if you have post API then your request has body).

import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class ContextInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const request = context.switchToHttp().getRequest();
    if (request.body) {
      request.body.context = {
        user: request.user,
      };
    }

    return next.handle();
  }
}

2- Use interceptor in RoleController or global.

...
@Controller('roles')
@UseInterceptors(ContextInterceptor)
export class RolesController {

...

}

3- create StripContextPipe to stripe context from request body to drop context after use in constraint.

/*
https://docs.nestjs.com/pipes
*/

import { PipeTransform, Injectable } from '@nestjs/common';

@Injectable()
export class StripContextPipe implements PipeTransform {
  transform(value: any) {
    if (value.context) {
      // drop context key in the desired way
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      const { context, ...rest } = value;
      return rest;
    }
    return value;
  }
}

4- use pipe on post method in RolesController.

  @Post()
  @UsePipes(new ValidationPipe({ whitelist: true }))
  async create(
    @Body(StripContextPipe)
    createDto: createRoleDto,
    @Res() res: Response,
  ) {
    const record = await this.roleService.create(      
      createDto,
    );
    return res.success(record);
  }

5- create ContextAwareDto to extend createRoleDto from it.

import { Allow } from 'class-validator';

export class ContextAwareDto {
  @Allow()
  context?: {
    user: any;
  };
}

export class createRoleDto extends ContextAwareDto {
...
@IsUnqiueForEnterprise({model: Role})
label!: string;
...
}

6- read user form context in your constraint validator

...
async validate(dto, validationArguments?: ContextValidationArguments) {
      const user = validationArguments.object.context.user;
      // you can load relations of user with find and repo
      // repo.find({where:{id:user.id},relations: {enterpris:true}})
...
Lesialesion answered 17/10, 2022 at 12:36 Comment(1)
Hi! Does this approach work for GraphQL requests? I'm trying to achieve something similiar for my GraphQL server but I am not being able to attach the user info to the context so that it available in the validationArgumentsPlay

© 2022 - 2024 — McMap. All rights reserved.