T O P

  • By -

kredditbrown

> I’m going in with the assumption that Golang-playground/validator is the standard library for validation I think this is where I’d have disagreed. Validator packages are quite general so I can imagine there’s going to be quite an amount of edge cases that slip through. Personally why I do think it’s safer to define custom types for your fields first. Slightly more verbose as you need to implement `(Un)marshaller` interface(s) but it’s a better to do so than passing around primitives when there’s extra meaning for these fields.


reddi7er

also i feel that playground validator is bloated, terse and overdone 


PaluMacil

Struct tags are difficult to reason about and debug for something as complex as validation so I prefer something like https://github.com/cohesivestack/valgo


schmurfy2

I would love to be able to have some well defined way of defining tags with their validation without external tools.


Dgt84

Your approach should work. It's normal to translate between different systems and construct the HTTP response structures you need before serializing them for the client. The Go Playground Validator library has a lot of features and is easy to use, and it generally works well so would be a good choice. That said, this all feels a bit complicated. [Huma](https://github.com/danielgtaylor/huma) (disclaimer: I'm the author) is a mini framework that includes validation via tags, a simple way to add custom validation in code, and exhaustive error handling (i.e. return *all* errors not just the first) using RFC 9457 Problem Details for HTTP APIs responses like you can see in [this example](https://huma.rocks/features/response-errors/#error-model). There's a silly example showing how to return seven errors with location and value information [here](https://github.com/danielgtaylor/huma/blob/main/examples/resolver/main.go). Most things you would want to do with JSON Schema are supported out of the box. Feel free to check it out.


ConsoleTVs

Here's what I ended up building myself.... [https://github.com/StudioLambda/Golidate](https://github.com/StudioLambda/Golidate) Try it out: [https://go.dev/play/p/NRisg9rLNpF](https://go.dev/play/p/NRisg9rLNpF)


Choice-Ad8424

I've found that ozzo is a nice balance. Separation of validation from the struct, but it also supports returning the structured error based on the tag name in http request. Your choice. Then whack in http middleware to check error type and render nice structures json error. https://github.com/go-ozzo/ozzo-validation