Skip to content
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

fix(common): enforce string type in validationpipe #14241

Merged

Conversation

LhonRafaat
Copy link
Contributor

@LhonRafaat LhonRafaat commented Nov 28, 2024

strictly parse the value to string to avoid nested objects being passed if the type is explicitly set to string.

The solution was already provided in the issue, I thought I could quickly submit a PR :) , however I didnt include the part for empty value, because if the value is set to string type and an empty value is provided, it should just return an empty string, it will be useful in some cases.

closes #14234

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

The pipeline doesnt pass the value to string if an nested object is provided and the type is explicitly set to string.
Screenshot from 2024-11-28 17-17-36
Screenshot from 2024-11-28 17-18-09

Issue Number: #14234

What is the new behavior?

if the value of foo query set to string, it will parse it to string no matter the value.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

strictly parse the value to string to avoid nested objects being passed if the type is explicitly set to string

closes nestjs#14234
@coveralls
Copy link

coveralls commented Nov 28, 2024

Pull Request Test Coverage Report for Build 235ee298-c027-4827-8b3d-0f52882777ec

Details

  • 0 of 2 (0.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.03%) to 91.932%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/common/pipes/validation.pipe.ts 0 2 0.0%
Files with Coverage Reduction New Missed Lines %
packages/common/pipes/validation.pipe.ts 1 91.07%
Totals Coverage Status
Change from base Build 271165de-8df9-42fd-b648-740fe88b6368: -0.03%
Covered Lines: 6814
Relevant Lines: 7412

💛 - Coveralls

@@ -207,6 +207,9 @@ export class ValidationPipe implements PipeTransform<any> {
return +value;
}
if (metatype === String) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (metatype === String) {
if (metatype === String && !isUndefined(value)) {

@@ -207,6 +207,9 @@ export class ValidationPipe implements PipeTransform<any> {
return +value;
}
if (metatype === String) {
if (isUndefined(value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this nested if can be removed then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this for sake of consistency, but changed it to the suggested implementation

@kamilmysliwiec kamilmysliwiec merged commit 526f3d4 into nestjs:master Nov 29, 2024
3 checks passed
@kamilmysliwiec
Copy link
Member

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ValidationPipe] query params becomes object in runtime when declared with string typescript type
3 participants