-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValidationPipe with custom error #1267
Comments
What about (2 ways):
|
Thanks for the response. Here are my attempts at both approaches for reference. Extend
|
As for my initial "feature request": I propose nest/packages/common/pipes/validation.pipe.ts Lines 46 to 48 in a95b3d6
It would default to Then usage would be just this: async function bootstrap () {
const app = await NestFactory.create(AppModule)
app.useGlobalPipes(new ValidationPipe({
exceptionConstructor: UnprocessableEntityException,
}))
await app.listen(3000)
} |
Since 5.5.0 you can override exception's factory: new ValidationPipe({
exceptionFactory: (errors: ValidationError[]) =>
new BadRequestException('Validation error'),
}); |
There's one tiny bit missing that drove me mad... this code would end in throwing "undefined". exceptionFactory: (errors: ValidationError[]) => RETURN new BadRequestException('Validation error'), ... then everything will be fine :) |
@jbjhjm It shouldn't, unless you added |
Sometimes I feel stupid. Of course. Didn't notice the brackets were skipped. Thanks for clearing up! |
What about passing |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm submitting a...
Current behavior
A validation error returns a response with status code 400.
Expected behavior
The
ValidationPipe
is configurable and accepts a function used to create an error instance which will be thrown.Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
Per-field validation errors are, in my opinion, a 422, not a 400. I'd also suggest changing this framework-wide as a default, but that might be a breaking change that people might not welcome with open arms.
Environment
The text was updated successfully, but these errors were encountered: