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

feat: Implement validation for HTTP FormValue(s) in frontend microser… #2438

Merged
merged 5 commits into from
Mar 25, 2024

Conversation

emzola
Copy link
Contributor

@emzola emzola commented Mar 19, 2024

Add validation layer to validate HTTP FormValue data in frontend microservice

Background

The frontend microservice didn't have a validation layer for form data expected in some handlers. This PR implements this validation layer.

Fixes

#2416

Change Summary

Created a validator package in frontend microservice.

Additional Notes

Testing Procedure

Related PRs or Issues

@emzola emzola requested review from yoshi-approver and a team as code owners March 19, 2024 07:34
@@ -0,0 +1,185 @@
// Copyright 2018 Google LLC
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be:

Suggested change
// Copyright 2018 Google LLC
// Copyright 2024 Google LLC

@@ -0,0 +1,96 @@
// Copyright 2018 Google LLC
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be:

Suggested change
// Copyright 2018 Google LLC
// Copyright 2024 Google LLC

{"invalid ccMonth (month > 12)", "[email protected]", "12345 example street", 10004, "New York", "New York", "United States", "5272940000751666", 13, 2024, 584},
{"invalid ccYear (not provided)", "[email protected]", "12345 example street", 10004, "New York", "New York", "United States", "5272940000751666", 12, 0, 584},
{"invalid ccCVV (not provided)", "[email protected]", "12345 example street", 10004, "New York", "New York", "United States", "5272940000751666", 12, 2024, 0},
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Praise: Excellent set of test cases. Thorough! 💯

msg += fmt.Sprintf("the %s in your request is invalid.\n", err.Field())
}
return fmt.Errorf(msg)
}
Copy link
Collaborator

@NimJay NimJay Mar 20, 2024

Choose a reason for hiding this comment

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

Suggestion: Could we reduce the duplication inside each Validate() function by moving this chunk into errorResponse():

  err := validate.Struct(...)
	if err == nil {
		return nil
	}
	validationErr, ok := err.(validator.ValidationErrors)
	if !ok {
		return err
	}

If we do this, we should probably also rename the errorResponse() function to something like validateAndGetErrorMessage().

CcCVV: ccCVV,
}
if err := payload.Validate(); err != nil {
renderHTTPError(log, r, w, err, http.StatusUnprocessableEntity)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Praise: Thanks for responding with http.StatusUnprocessableEntity!

Copy link
Collaborator

@NimJay NimJay Mar 20, 2024

Choose a reason for hiding this comment

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

Request: Could you please update line 52 in /.github/workflows/ci-pr.yaml to also run frontend Go tests?

Copy link
Collaborator

@NimJay NimJay left a comment

Choose a reason for hiding this comment

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

@emzola, thank you so much for doing this! :)
I left a few comments. Things are looking great.

CC: @bourgeoisor, @Mukamik - Just a heads-up: this PR adds a medium-sized change to the frontend service. Once this PR is merged, you might have to deal with some merge conflicts when merging in main to your demo branch. No action needed.

Copy link
Collaborator

@NimJay NimJay left a comment

Choose a reason for hiding this comment

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

Approved!
Thanks so much for addressing my feedback, @emzola!

The tests you added are passing.
Merging.

Excellent work. :)

@NimJay NimJay merged commit 9862459 into GoogleCloudPlatform:main Mar 25, 2024
6 checks passed
azinneera pushed a commit to azinneera/microservices-demo that referenced this pull request Mar 29, 2024
GoogleCloudPlatform#2438)

* feat: Implement validation for HTTP FormValue(s) in frontend microservice

* feat: Implement validation layer in frontend microservice

* Reduce loadgenerator "addToCart" quantity values

* Update ci-pr.yaml to test Go packages

---------

Co-authored-by: Nim Jayawardena <[email protected]>
Co-authored-by: Nim Jayawardena <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants