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
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.
Why not use?
Make sure you have Go installed on your machine.
go mod init example.com/learning
Use go get to install the necessary packages.
go get -u github.com/gin-gonic/gin
go get -u github.com/bube054/ginvalidator
One of the best ways to learn something is by example! So let's roll the sleeves up and get some coding happening.
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 }
Now run this file by executing go run main.go on your terminal.
go mod init example.com/learning
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!
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
? 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?
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
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 }
Now, what this is telling us is that
This is a better scenario, but it can still be improved. Let's continue.
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
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() }
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
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.
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