简体   繁体   中英

Idiomatic way to validate http request in go

I need to validate that my http request has two parameters, Start and End. Currently, I set a default value that should not appear as either of the parameters and check for it along with other invalid values. However, this feels like a hack. What should be the proper way to do this?

Here is my code:

type Request struct {
    Start int `json: "start"`
    End int `json: "end"`
}


func HandlePost(w http.ResponseWriter, r *http.Request) {
    body , _ := ioutil.ReadAll(r.Body)
    reqData := Request{Start: -1, End: -1} // < whats the correct way to do this
    json.Unmarshal(body, &reqData)  

    if reqData.Start < 0 && reqData.End < 0 {
        w.WriteHeader(http.StatusBadRequest)
        return
    }
    // rest of the logic
}

You can use https://github.com/asaskevich/govalidator for basic way of validating the request. But in case you want something more sophisticated, you need to write your own custom validator function. eg

type Request struct {
    Start int `json: "start"`
    End int `json: "end"`
}

func (a *Request) validate() url.Values {
    err := url.Values{}

    // Write your own validation rules
    if a.Start < 0 {
        err.Add("Start", "Start cannot be less than 0");
    }

    return err;
}

func handler(w http.ResponseWriter, r *http.Request) {
    requestBody := &Request{}

    defer r.Body.Close()
    if err := json.NewDecoder(r.Body).Decode(requestBody); err != nil {
        panic(err)
    }

    if errors := requestBody.validate(); len(errors) > 0 {
        err := map[string]interface{}{"validationError": errors}
        w.Header().Set("Content-type", "application/json")
        w.WriteHeader(http.StatusBadRequest)
        json.NewEncoder(w).Encode(err)
    }

    fmt.Fprint(w, "success request scenario!")
}

You can use https://github.com/buger/jsonparser getInt. You'll get an error if the json is missing the expected key.

I recommend using benchmark and not decided by the code beauty or any other hunch

Here's another way to validate structures using struct tags and pointers. Note that if 0 is a valid thing to pass, then this solution will not work. omitempty considers the 0 value to be empty. If you want this to work will considering 0 to be valid remove the pointers and modify the IsValid method

package main

import (
    "encoding/json"
    "fmt"
)

type Request struct {
    Start *int `json: "start,omitempty"`
    End   *int `json: "end,omitempty"`
}

func (r Request) IsValid() (bool, error) {
    if r.Start == nil {
        return false, fmt.Errorf("start is missing")
    }

    if r.End == nil {
        return false, fmt.Errorf("end is missing")
    }

    return true, nil
}

var (
    invalidStartb = `{"end": 1}`
    invalidEndb   = `{"start": 1}`
    valid         = `{"start": 1, "end": 1}`
)

func main() {
    var r Request

    _ = json.Unmarshal([]byte(invalidStartb), &r)
    fmt.Println(r.IsValid())

    r = Request{}
    _ = json.Unmarshal([]byte(invalidEndb), &r)
    fmt.Println(r.IsValid())

    r = Request{}
    _ = json.Unmarshal([]byte(valid), &r)
    fmt.Println(r.IsValid())

}

runnable version here https://goplay.space/#Z0eqLpEHO37

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM