Dependency injection with class-validator
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[ ] Feature request
[x ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
As mentioned in the documention NestJS is supposed to works well very with class validator, however i’m not successful trying to implement a custom validation class ( https://github.com/typestack/class-validator#custom-validation-classes ) or decorator. This usecase is more than usefull to implement complex validation logic that require injecting services As its a pretty standart usecase, i guess that there is a way to do it, but i’m not able to find it! could be a great addition to the online documentation imo !
Expected behavior
I’ve tried to add my Validator class as a component but not working. In the class-validator documentation they mentioned that we can register a DI, is this doable with Nest ?
Minimal reproduction of the problem with instructions
@Controller('cars')
export class CarController {
@Get()
public test(@Res() res) {
const dto = new CarInsertDTO();
dto.brand = 'toyota';
validate(dto).then(errs => {
if (errs && errs.length > 0) {
throw new BadRequestException(errs);
}
res.send(200);
});
}
}
export class CarInsertDTO {
@IsString()
public name: string;
@Validate(BrandValidator, { message: 'Invalid brand'})
public readonly strategy;
}
@ValidatorConstraint()
export class BrandValidator implements ValidatorConstraintInterface {
constructor(private readonly brandService: BrandService) {}
validate(brandName: any, args: ValidationArguments) {
// can be a http service , database service or whatever
const brands: string[] = this.brandService.all();
return brands.indexOf(brandName) >= 0;
}
}
Should lead to something like
[Nest] 1736 - 2018-3-26 12:16:38 [ExceptionsHandler] Cannot read property ‘all’ of undefined TypeError: Cannot read property ‘all’ of undefined
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:45 (8 by maintainers)
Top GitHub Comments
Hi guys, I found a solution for that, you can check my repo: https://github.com/neoteric-eu/nestjs-auth (btw, yesterday I was making a tech-workshops for developers in Gdańsk about how to proper handle authorisation and authentication with nest 😄 ), so, yeah @kamilmysliwiec I spread the love for Nest.js around the world 😸
@yamid-ny the solution for that in particular is done in two steps, first one:
The
{fallbackOnErrors: true}
is required, because Nest throw Exception when DI doesn’t have required class.Second thing:
IsUserAlreadyExist
have to be Injetable and registered in nest moduleLike so:
Then when I try to do
POST /users
twice with payload as follow:I have got following response:
Hell yeah!
@fmeynard @jmaicaaan @hershko765 @roeehershko - you also guys might be interested how to solve this problem.
Regards 😉
@kamilmysliwiec
and
behaves different. In first case I’ve got few errors spitted into the console like this
while container tries to get Validator and MetadataStorage classes. But finally code works correctly, and I got all injected services as expected;
In the second case there are no errors and everything works like a charm.
Can you explain why?