feat(common): add error format option to validation pipe #16329
+126
−0
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?
When using
ValidationPipewith nested object validation, custom error messages are prepended with the parent property path. For example, a custom message like"name should not be empty"becomes"parent.name should not be empty", which modifies the user-defined message unexpectedly.Issue Number: #16268
What is the new behavior?
Add a new
errorFormatoption toValidationPipeOptionswith two modes:'list'(default): Current behavior - returns an array of error message strings with parent path prepended to messages. No breaking change.'grouped': Returns aRecord<string, string[]>where keys are dot-notation property paths and values are arrays of unmodified constraint messages.Example with
errorFormat: 'grouped':This separates the property path from the message content, so custom messages are preserved as-is.
Does this PR introduce a breaking change?
Other information
This approach is consistent with how other frameworks like Laravel and express-validator handle nested validation errors. The new groupValidationErrors method is
added alongside the existing
flattenValidationErrors, and the choice is made increateExceptionFactorybased on theerrorFormatoption.