feat(common): add new flatten options to validation pipe#14359
Open
civilcoder55 wants to merge 1 commit intonestjs:v12.0.0from
Open
feat(common): add new flatten options to validation pipe#14359civilcoder55 wants to merge 1 commit intonestjs:v12.0.0from
civilcoder55 wants to merge 1 commit intonestjs:v12.0.0from
Conversation
Pull Request Test Coverage Report for Build 67eb60ef-09a9-4432-a64b-abc893259f20Details
💛 - Coveralls |
63fdf67 to
7fbfa8a
Compare
09d5963 to
0976795
Compare
Hardanish-Singh
approved these changes
Jul 28, 2025
Member
|
Could you rebase to v12.0.0 branch? |
0976795 to
529df13
Compare
529df13 to
1b49432
Compare
Author
Done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Currently, when the validation pipe throws an error, the error messages are always flattened into an array of strings. For example:
[ "name must be a string", "number must be an integer number" ]This behavior lacks flexibility for scenarios where the original structured error format is needed.
Also for custom
exceptionFactorythe errors are always returned as an array of ValidationError. For example:[ { "target": {}, "property": "name", "children": [], "constraints": { "isString": "name must be a string" } }, { "target": {}, "property": "number", "children": [], "constraints": { "isInt": "number must be an integer number" } } ]What is the new behavior?
This PR introduces a new two options
disableFlattenErrorMessagesandflatExceptionFactoryMessagein the ValidationPipe to control the formatting of error messages.disableFlattenErrorMessagesWhen this option is enabled, the errors will retain their original structure instead of being flattened.
For example:
The errors will now be returned in the
ValidationErrorstructured format:[ { "target": {}, "property": "name", "children": [], "constraints": { "isString": "name must be a string" } }, { "target": {}, "property": "number", "children": [], "constraints": { "isInt": "number must be an integer number" } } ]flatExceptionFactoryMessageWhen this option is enabled, and
exceptionFactoryis provided, the errors passed to exception factory will be flattened as array of stringsFor example:
The errors will now be in format:
[ "name must be a string", "number must be an integer number" ]Does this PR introduce a breaking change?
Other information
Motivation
The motivation behind this change stems from a specific use case with WebSocket validation. When using the validation pipe with WebSockets (WebSocket Pipes Documentation), the error messages were not flattened by default. However, when I needed to flatten them, I ended up duplicating the same flattening logic in my exception handler. so I made it configurable.
Docs PR: nestjs/docs.nestjs.com#3173