Comments (4)
Great! I’ve looked at the following validation packages:
https://github.com/asaskevich/govalidator
https://github.com/go-ozzo/ozzo-validation
https://github.com/go-playground/validator
I decided to settle for go playground (like you suggested) because in addition to being promising and used by lots of projects, I think the package provides functions and tags that are easier to understand.
I’ve also looked through the codebase of the frontend microservice. I see that there are 3 handlers in src/frontend/handlers.go that require validation (addToCartHandler, placeOrderHandler and setCurrencyHandler).
In terms of how to go about implementing a validation layer, I’m thinking about creating a validator package in the src/frontend folder. In this package, there will be maybe 2 files: validator.go and validator_test.go. In validator.go, we could have 3 structs. Each struct will represent the HTTP form data expected from each handler. Of course, we specify the validation rules for each struct field using tags, and each struct will have a Validate method that actually performs the validation based on the rules set in the tags.
Then in src/frontend/handlers.go, in each handler that requires validation, we initialize a validator struct and populate it with data from r.FormValue. We call the Validate method on the struct and return a HTTP error with 422 status code if validation fails.
That’s a rough idea of things. I could translate this to code for just the placeOrderHandler and make a PR so you see.
from microservices-demo.
Hi @NimJay, I would like to work on this issue. Since no one has commented, may I continue with this work?
from microservices-demo.
Hi @emzola, thank you for reaching out and your interest in contributing. :)
I just assigned this ticket you.
What I suggest as next steps:
- Go through /.github/CONTRIBUTING.md.
- Research best practices for validating HTTP input in Golang. Share you findings in this GitHub issue. I would look into https://github.com/go-playground/validator. It seems promising.
- Set up your Online Boutique dev environment. See development guide here.
Other thoughts:
- If input is invalid, the response should return a 422 HTTP status.
- I would start small. For instance, after briefly researching and agreeing on an implementation (e.g., which libraries to use), we can work on a PR that performs validation for just one handler such as the
placeOrderHandler
handler.
from microservices-demo.
@emzola, thank you for that very clear explanation and excellent progress! 💯
Let's go with what you suggested! :)
- A separate validator package containing
validator.go
andvalidator_test.go
. - Respond with 422 if there is an invalid field.
- 3 structs inside
validator.go
(for each handler).
Optional: Ideally, the body of the 422 HTTP error response would state which field is invalid (e.g., "The streetAddress in your request is invalid."). But this is totally optional — since I don't think it will add a lot of value.
from microservices-demo.
Related Issues (20)
- Running GKE in Cloud Shell Fails HOT 2
- Introduce Python linter for consistent code style
- ENABLE_STATS doesn't do anything HOT 1
- Latency spikes observed in frontend HOT 1
- Update copyright to be Time.now.year (or equivalent) HOT 3
- Replace workload identity with workload identity federation HOT 1
- Refactor ShoppingAssistantService kustomize components HOT 2
- Comandos sequenciais
- Split Skaffold manifest into sub-modules
- Improve substitution of variables in Kustomize components
- Release v0.10.0 HOT 1
- Remove the ability to use default service accounts in Helm
- Recreate the release GKE cluster
- Clean up and deduplicate manifests throughout the repo
- Adservice crashing with segfault HOT 6
- Expose metrics to my local kube-prometheus.
- Fix the adservice profiler
- Images are not diplaying in k8s cluster HOT 3
- Unable to fetch Bootstrap from CDN (ERR_DNS_NO_MATCHING_SUPPORTED_ALPN) HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from microservices-demo.