Menu

Post image 1
Post image 2
1 / 2
0

I built ginvalidator — middleware-based request validation for Gin, modeled on express-validator

DEV Community·Gbubemi Attah·21 days ago
#RmWE7eT8
#go#opensource#webdev#showdev#email#chain
Reading 0:00
15s threshold

Here's how validation in a Gin handler usually goes. You bind JSON to a struct. You write binding:"required,email" tags. It mostly works, until you need something the tags can't express — "this email is already taken," or "this field is only required if that other field has a specific value." Then you're back to writing it by hand. Or you skip struct binding entirely, pull values out of the request manually, write validation inline, and ten endpoints later you've got the same regex copied four different places. If you've used express-validator in Node, you know there's a cleaner shape. Chain validators on a field, mix in sanitizers, decide what to do with the errors, move on. ginvalidator is that, for Gin. Repo: github.com/bube054/ginvalidator Quick taste package main import ( gv "github.com/bube054/ginvalidator" "github.com/gin-gonic/gin" ) func main () { r := gin . Default () r . POST ( "/signup" , gv . NewBodyChain ( "email" , nil ) . Not () . Empty ( nil ) . Bail () . Email ( nil ) . Validate (), gv .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More