惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
C
Check Point Blog
Vercel News
Vercel News
Y
Y Combinator Blog
G
Google Developers Blog
P
Proofpoint News Feed
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
博客园 - 司徒正美
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
L
LangChain Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
博客园_首页
B
Blog RSS Feed
The Cloudflare Blog
MyScale Blog
MyScale Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Security Blog
Microsoft Security Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
I built ginvalidator — middleware-based request validatio...
Gbubemi Atta · 2026-05-13 · via DEV Community

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.NewBodyChain("username", nil).
            Trim("").
            Not().Empty(nil).
            Alphanumeric(nil).
            Validate(),
        func(ctx *gin.Context) {
            result, _ := gv.ValidationResult(ctx)
            if len(result) > 0 {
                ctx.AbortWithStatusJSON(422, gin.H{"errors": result})
                return
            }

            data, _ := gv.GetMatchedData(ctx)
            email, _ := data.Get(gv.BodyLocation, "email")
            username, _ := data.Get(gv.BodyLocation, "username")
            // both are validated, sanitized, ready to use

            ctx.JSON(200, gin.H{"email": email, "username": username})
        },
    )

    r.Run(":8080")
}

Enter fullscreen mode Exit fullscreen mode

Each chain is its own middleware. Gin runs them left to right. Errors don't reject the request — they're collected, and you decide what to do with them in the handler.

What goes in a chain

Validators. Email, URL, UUID, Alpha, Alphanumeric, Numeric, Empty, Contains, Equals, Matches, CreditCard, MobilePhone, PostalCode, StrongPassword, JSON, Boolean, Date, ISO8601, and dozens more. All powered by my other validatorgo.

Sanitizers. Trim, Escape, Unescape, Blacklist, Whitelist, NormalizeEmail, StripLow, ToBoolean, ToInt, ToFloat, ToDate. Sanitized values flow forward through the chain and get stored for you to retrieve after validation.

Modifiers. Not() flips the next validator's result. Bail() stops the chain on first failure. Optional() skips the chain if the field is empty. If() and Skip() give you conditional control via a function.

Custom validators and sanitizers for when the built-ins don't cover what you need:

gv.NewBodyChain("email", nil).
    Email(nil).
    CustomValidator(func(r *http.Request, initialValue, sanitizedValue string) bool {
        return !userExistsByEmail(sanitizedValue)
    }).
    Validate()

Enter fullscreen mode Exit fullscreen mode

Five places to look

Different chain constructor for each part of an HTTP request. Same chain semantics:

  • NewBodyChain — JSON, form, multipart body. JSON paths use GJSON syntax, so "user.profile.email" works for nested fields.
  • NewQueryChain — URL query parameters
  • NewParamChain — Gin route parameters (:id)
  • NewHeaderChain — HTTP headers
  • NewCookieChain — cookies

A few things I'm really happy with

OneOf for either/or validation. Login that accepts either an email or a phone number:

gv.OneOf(
    []gv.ValidationChain{
        gv.NewBodyChain("email", nil).Not().Empty(nil).Email(nil),
    },
    []gv.ValidationChain{
        gv.NewBodyChain("phone", nil).Not().Empty(nil).MobilePhone(nil, ""),
    },
)

Enter fullscreen mode Exit fullscreen mode

If at least one group passes, the request passes. The matched data from the winning group is what GetMatchedData returns.

CheckSchema for routes with a lot of fields. Twelve fields is twelve chains. That gets verbose. Schemas collapse them into a map:

gv.CheckSchema(gv.Schema{
    "email": {
        In: gv.BodyLocation,
        Build: func(vc gv.ValidationChain) gv.ValidationChain {
            return vc.Not().Empty(nil).Bail().Email(nil)
        },
    },
    "username": {
        In: gv.BodyLocation,
        Build: func(vc gv.ValidationChain) gv.ValidationChain {
            return vc.Not().Empty(nil).Bail().Alphanumeric(nil)
        },
    },
    "bio": {
        In:       gv.BodyLocation,
        Optional: true,
        Build: func(vc gv.ValidationChain) gv.ValidationChain {
            return vc.Trim("").Escape()
        },
    },
})

Enter fullscreen mode Exit fullscreen mode

One middleware. Same semantics. Easier to scan than ten chained calls.

Error reading helpers for different UI shapes. ValidationResult(ctx) gives you everything as a slice, but there's also:

  • HasErrors(ctx) — bool, for the "did anything fail" check
  • FirstError(ctx) — pointer to the first error, for "show one at a time" UIs
  • ErrorsByField(ctx) — map of field to errors, for forms with per-field error lists
  • FirstErrorByField(ctx) — same map, but at most one error per field I find myself reaching for FirstErrorByField constantly when wiring up form UIs. The shape lines up with how you'd render it.

How errors look

Standard JSON, with location info:

{
  "errors": [
    {
      "location": "body",
      "field": "email",
      "value": "nope",
      "message": "invalid email",
      "code": "invalid_format"
    },
    {
      "location": "body",
      "field": "username",
      "value": "",
      "message": "Invalid value"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

The code and message come from validatorgo when a built-in validator fails, so they're stable strings you can map for i18n or client-side handling. You can override messages per-chain, or globally with DefaultErrFmtFunc.

Where this came from

This is actually the project that spawned validatorgo. I started building a Gin port of express-validator, realized I needed validator.js's whole API surface to do it properly, and ended up extracting that into its own library. That library got finished and published first; this one is the original goal it was built to serve.

So if you've seen validatorgo, you've seen the engine. ginvalidator is what I actually wanted to build all along.

If this is useful to you

A star on the repo helps more than I'd like to admit. Gin middleware is a niche-within-a-niche, and discoverability is probably the biggest blocker for libraries like this.

Forking it and trying it on a real handler is the best way to find rough edges. The five-locations-of-chain thing means there are a lot of permutations, and edge cases tend to hide in the boring ones (cookies with weird characters, multipart fields with the same name twice, that kind of thing).

If you've used express-validator and you notice behavior that doesn't match, open an issue. Spec parity is something I actively want, and "this works in express-validator but not here" is a clear bug to me.

PRs welcome — additional sanitizers, more validators that pass through to validatorgo, doc improvements, all fair game.

Links

@bube054