Welcome To Golang By Example

Validate the range of the integer in an HTTP request body in Go (Golang)

Table of Contents

Overview

The below library can be used to validate the range of an integer in an incoming JSON HTTP request body

For this tutorial, we will try to parse the incoming JSON request body into the below employee struct

type employee struct {
    Age int `validate:"required,gte=10,lte=20"`
}

Notice here that we need to associate meta tags with fields of the struct to let the validator know that you want to validate this field. In the above example, we added the tag with the Age field. This tag is interpreted by the playground validate library. Notice we added three validations for the Age field

Program

package main
import (
    "encoding/json"
    "net/http"
    "github.com/go-playground/validator"
)
var validate *validator.Validate
type employee struct {
    Age int `validate:"required,gte=10,lte=20"`
}
func main() {
    createEmployeeHanlder := http.HandlerFunc(createEmployee)
    http.Handle("/employee", createEmployeeHanlder)
    http.ListenAndServe(":8080", nil)
}
func createEmployee(w http.ResponseWriter, r *http.Request) {
    headerContentTtype := r.Header.Get("Content-Type")
    if headerContentTtype != "application/json" {
        errorResponse(w, "Content Type is not application/json", http.StatusUnsupportedMediaType)
        return
    }
    var e employee
    decoder := json.NewDecoder(r.Body)
    err := decoder.Decode(&e)
    if err != nil {
        errorResponse(w, "Bad Request "+err.Error(), http.StatusBadRequest)
    }
    err = validateStruct(e)
    if err != nil {
        errorResponse(w, "Bad Request "+err.Error(), http.StatusBadRequest)
    }
    errorResponse(w, "Success", http.StatusOK)
    return
}
func errorResponse(w http.ResponseWriter, message string, httpStatusCode int) {
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(httpStatusCode)
    resp := make(map[string]string)
    resp["message"] = message
    jsonResp, _ := json.Marshal(resp)
    w.Write(jsonResp)
}
func validateStruct(e employee) error {
    validate = validator.New()
    err := validate.Struct(e)
    if err != nil {
        return err
    }
    return nil
}

Run the program.

go run main.go

It will start an HTTP server listening on port 8080. Now make some curl API calls

curl  -X POST -H "content-type: application/json" http://localhost:8080/employee -d '{"Age": 5}'

gives below response as 5 is less than the minimum which is 10

{"message":"Bad Request Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'gte' tag"}
curl  -X POST -H "content-type: application/json" http://localhost:8080/employee -d '{"Age": 10}'

gives below response as 10 is greater than the maximum which is 20

{"message":"Bad Request Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'lte' tag"}
curl  -X POST -H "content-type: application/json" http://localhost:8080/employee -d '{"Age": 15}'

gives response as success as 15 is greater than 10 and less than 20.

{"message":"Success"}