Home > Backend Development > Golang > Simplify Gin Input Validation in Go with ginvalidator

Simplify Gin Input Validation in Go with ginvalidator

Linda Hamilton
Release: 2024-12-01 11:52:11
Original
896 people have browsed it

Simplify Gin Input Validation in Go with ginvalidator

Overview

ginvalidator is a set of Gin middlewares that wraps the extensive collection of validators and sanitizers offered by my other open source package validatorgo. It also uses the popular open-source package gjson for JSON field syntax, providing efficient querying and extraction of data from JSON objects.

It allows you to combine them in many ways so that you can validate and sanitize your Gin requests, and offers tools to determine if the request is valid or not, which data was matched according to your validators.

It is based on the popular js/express library express-validator

Support

This version of ginvalidator requires that your application is running on Go 1.16 .
It's also verified to work with Gin 1.x.x.

Rationale

Why not use?

  • Handwritten Validators: You could write your own validation logic manually, but that gets repetitive and messy fast. Every time you need a new validation, you’re writing the same kind of code over and over. It’s easy to make mistakes, and it’s a pain to maintain.
  • Gin's Built-in Model Binding and Validation: Gin has validation built in, but it’s not ideal for everyone. Struct tags are limiting and make your code harder to read, especially when you need complex rules. Plus, the validation gets tied too tightly to your models, which isn't great for flexibility.
  • Other Libraries (like Galidator): There are other libraries out there, but they often feel too complex for what they do. They require more setup and work than you’d expect, especially when you just want a simple, straightforward solution for validation.

Installation

Make sure you have Go installed on your machine.

Step 1: Create a New Go Module

  1. Create an empty folder with a name of your choice.
  2. Open a terminal, navigate (cd) into that folder, and initialize a new Go module:
go mod init example.com/learning
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Step 2: Install Required Packages

Use go get to install the necessary packages.

  1. Install Gin:
go get -u github.com/gin-gonic/gin
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. Install ginvalidator:
go get -u github.com/bube054/ginvalidator
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Getting Started

One of the best ways to learn something is by example! So let's roll the sleeves up and get some coding happening.

Setup

The first thing that one needs is a Gin server running. Let's implement one that says hi to someone; for this, create a main.go then add the following code:

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    r.GET("/hello", func(ctx *gin.Context) {
        person := ctx.Query("person")
        ctx.String(http.StatusOK, "Hello, %s!", person)
    })

    r.Run() // listen and serve on 0.0.0.0:8080
}
Copy after login
Copy after login
Copy after login
Copy after login

Now run this file by executing go run main.go on your terminal.

go mod init example.com/learning
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

The HTTP server should be running, and you can open http://localhost:8080/hello?person=John to salute John!

? Tip:
You can use Air with Go and Gin to implement live reload. These automatically restart the server whenever a file is changed, so you don't have to do this yourself!

Adding a validator

So the server is working, but there are problems with it. Most notably, you don't want to say hello to someone when the person's name is not set.
For example, going to http://localhost:8080/hello will print "Hello, ".

That's where ginvalidator comes in handy. It provides validators, sanitizers and modifiers that are used to validate your request.
Let's add a validator and a modifier that checks that the person query string cannot be empty, with the validator named Empty and modifier named Not:

go get -u github.com/gin-gonic/gin
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

? Note:

For brevity, gv is used as an alias for ginvalidator in the code examples.

Now, restart your server, and go to http://localhost:8080/hello again. Hmm, it still prints "Hello, !"... why?

Handling validation errors

ginvalidator validation chain does not report validation errors to users automatically.
The reason for this is simple: as you add more validators, or for more fields, how do you want to collect the errors? Do you want a list of all errors, only one per field, only one overall...?

So the next obvious step is to change the above code again, this time verifying the validation result with the ValidationResult function:

go get -u github.com/bube054/ginvalidator
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Now, if you access http://localhost:8080/hello again, you’ll see the following JSON content, formatted for clarity:

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    r.GET("/hello", func(ctx *gin.Context) {
        person := ctx.Query("person")
        ctx.String(http.StatusOK, "Hello, %s!", person)
    })

    r.Run() // listen and serve on 0.0.0.0:8080
}
Copy after login
Copy after login
Copy after login
Copy after login

Now, what this is telling us is that

  • there's been exactly one error in this request;
  • this field is called person;
  • it's located in the query string (location: "queries");
  • the error message that was given was "Invalid value".

This is a better scenario, but it can still be improved. Let's continue.

Creating better error messages

All request location validators accept an optional second argument, which is a function used to format the error message. If nil is provided, a default, generic error message will be used, as shown in the example above.

go run main.go
Copy after login
Copy after login

Now if you access http://localhost:8080/hello again, what you'll see is the following JSON content, with the new error message:

package main

import (
    "net/http"

    gv "github.com/bube054/ginvalidator"
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    r.GET("/hello", gv.NewQuery("person", nil).
        Chain().
        Not().
        Empty(nil).
        Validate(), func(ctx *gin.Context) {
            person := ctx.Query("person")
            ctx.String(http.StatusOK, "Hello, %s!", person)
        })

    r.Run()
}
Copy after login
Copy after login

Accessing validated/sanitized data

You can use GetMatchedData, which automatically collects all data that ginvalidator has validated and/or sanitized. This data can then be accessed using the Get method of MatchedData:

go mod init example.com/learning
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

open http://localhost:8080/hello?person=John to salute John!

The available locations are BodyLocation, CookieLocation QueryLocation, ParamLocation and HeaderLocation.
Each of these locations includes a String method that returns the location where validated/sanitized data is being stored.

Sanitizing inputs

While the user can no longer send empty person names, it can still inject HTML into your page! This is known as the Cross-Site Scripting vulnerability (XSS).
Let's see how it works. Go to http://localhost:8080/hello?person=John, and you should see "Hello, John!".
While this example is fine, an attacker could change the person query string to a

Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template