go
42 lines · 5 steps
Custom validators in Gin
Register reusable struct-tag validators on Gin's binding engine, safely and exactly once.
Explained by
highlit
1package validators
2
3import (
4 "regexp"
5 "sync"
6 "time"
7
8 "github.com/gin-gonic/gin/binding"
9 "github.com/go-playground/validator/v10"
10)
11
12var (
13 slugPattern = regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*$`)
14 once sync.Once
15)
16
17func Register() {
18 once.Do(func() {
19 v, ok := binding.Validator.Engine().(*validator.Validate)
20 if !ok {
21 return
22 }
23 v.RegisterValidation("slug", validateSlug)
24 v.RegisterValidation("notpast", validateNotPast)
25 })
26}
27
28func validateSlug(fl validator.FieldLevel) bool {
29 s := fl.Field().String()
30 if s == "" {
31 return true
32 }
33 return slugPattern.MatchString(s)
34}
35
36func validateNotPast(fl validator.FieldLevel) bool {
37 ts, ok := fl.Field().Interface().(time.Time)
38 if !ok {
39 return false
40 }
41 return !ts.Before(time.Now())
42}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Registering validators through sync.Once keeps setup idempotent even if called from multiple init paths.
- 2Custom validators are plain functions that read fl.Field() and return a bool verdict.
- 3Treating an empty string as valid lets you compose a format rule with a separate required rule.
Related explainers
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
php
<?php namespace App\Support;
Merging query params onto a URL in PHP
url-parsing
query-strings
immutability
Intermediate
8 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
go
package model import ( "encoding/json"
Custom JSON marshaling in Go
json
serialization
interfaces
Intermediate
5 steps
javascript
const transitions = { cart: { checkout: 'shipping' }, shipping: { submitAddress: 'payment', back: 'cart' }, payment: { submitPayment: 'review', back: 'shipping' },
A finite state machine for checkout flow
state-machine
event-driven
data-driven-design
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/custom-validators-in-gin-explained-go-2263/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.