GithubHelp home page GithubHelp logo

Comments (4)

emzola avatar emzola commented on May 25, 2024 1

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.

emzola avatar emzola commented on May 25, 2024

Hi @NimJay, I would like to work on this issue. Since no one has commented, may I continue with this work?

from microservices-demo.

NimJay avatar NimJay commented on May 25, 2024

Hi @emzola, thank you for reaching out and your interest in contributing. :)
I just assigned this ticket you.

What I suggest as next steps:

  1. Go through /.github/CONTRIBUTING.md.
  2. 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.
  3. Set up your Online Boutique dev environment. See development guide here.

Other thoughts:

from microservices-demo.

NimJay avatar NimJay commented on May 25, 2024

@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 and validator_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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.