encore icon indicating copy to clipboard operation
encore copied to clipboard

encore.go - default to using `validator/v10` for validation

Open mistermoe opened this issue 9 months ago • 0 comments

Hi! i wanted to make a suggestion for encore.go that could potentially improve developer experience for validation. Apologies if there's already been discussion about this elsewhere that i wasn't able to find

Problem

Currently, per encore's validation docs, request body validation requires developers to implement the Validate() method on the request type. This design decision comes with tradeoffs:

Benefits

  • allows developers to choose and use their own validation library.

Cost

  • every request type that needs to be validated must now have a Validate() method. This can start to add quite a bit of code in large systems
  • depending on the validation library being used, each Validate() method may end up looking 99% the same e.g.
type StartIDVRequest struct {
  DOB         string `json:"dob" validate:"required,datetime=2006-01-02"`
  PhoneNumber string `json:"phone_number" validate:"required"`
  CountryCode string `json:"country_code" validate:"required,iso3166_1_alpha2"`
}

func (req StartIDVRequest) Validate() error {
  // validator := validator.New(validator.WithRequiredStructEnabled()) defined elsewhere
  return validator.Struct(req)
}

Proposal

adopt https://github.com/go-playground/validator as the default validation library while still allowing developers to implement their own Validate() method if they want to use a different validation library or include additional validation steps.

This would reduce boilerplate code for developers that are ok with using validator/v10. those wanting to use their own validation library still have the ability to do so by implementing their own Validate() methods.

I did notice at the bottom of the validation docs that there may be plans to provide out-of-the-box validation. Presumably y'all would take the same approach? as in - provide default validation but also a way to "bail" by implementing Validate()

mistermoe avatar Feb 07 '25 07:02 mistermoe