DEV Community

Harry Douglas
Harry Douglas

Posted on

Api Requests and Validators in Go

I know there are hundreds of ways to implement it better but for learning purposes I wrote a simple request and validator mechanism here in this commit:

https://github.com/hrrydgls/snug/commit/ca07cdfd38828de8901a6b7bdfdf0e35fa6e2d88

It is gonna be better for sure in future but for now I just wanted to keep it simple and clean.

What I did is simple. First I created a package named requests with this struct:

package requests 

type RegisterRequest struct {
    Email string `json:"email"`
    Name string `json:"name"`
    Password string `json:"password"`
}
Enter fullscreen mode Exit fullscreen mode

Then another package named validators and a new validator inside it:

package validators

import (
    "net/mail"

    "github.com/hrrydgls/snug/exceptions"
    "github.com/hrrydgls/snug/requests"
)

func RegisterValidator (registerRequest *requests.RegisterRequest) error {
    if _, error := mail.ParseAddress(registerRequest.Email); error != nil {
        panic(exceptions.Unprocessable("Email is required!"))
    }

    if len(registerRequest.Name) < 3 {
        panic(exceptions.Unprocessable("Name should be at least 3 characters!"))
    }

    if len(registerRequest.Password) < 6 {
        panic(exceptions.Unprocessable("Password should be at least 6 characters!"))
    }

    return nil
}
Enter fullscreen mode Exit fullscreen mode

And finally I used this request and validator in my handler like this:

package auth

import (
    "net/http"

    "github.com/hrrydgls/snug/requests"
    "github.com/hrrydgls/snug/validators"
)

func (handler *Handler) Register(w http.ResponseWriter, r *http.Request) {

    r.ParseForm()

    email := r.FormValue("email")
    name := r.FormValue("name")
    password := r.FormValue("password")


    registerRequest := requests.RegisterRequest {
        Email: email,
        Name: name,
        Password: password,
    }

    validators.RegisterValidator(&registerRequest)

}
Enter fullscreen mode Exit fullscreen mode

I know there are better packages like go-playground/validator that I could use it but I wanted to suffer a bit before using these already working packages :)

As someone from PHP world, I feel better with Golang types... In Laravel we had Requests but while referencing them, you had no idea if this field exist in the request or not. It was not intelligence enough! Everytime I had to go back to request to see if the field exists or not.

I don't wanna complain more about PHP. It is a old language and still alive but I just feel better with this :)

Enjoy!

Top comments (2)

Collapse
 
sonzai profile image
Sơn Zai

i am learn GO . i and understand you. before i code php codeigniter and when i learn golang it make me feel "OMG I love it " simple and powerful

Collapse
 
hrrydgls profile image
Harry Douglas

Exactly!

One another thing that I like about Go compared to PHP is that Go is super friendly with mircoservice architecture! You build your binary and deploy it on bare OS! You don't need to install hundreds of stuff like extensions, interpreter and even web server... The final binary is small and ready to deploy!

It is ready to production! You don't need to have Nginx before it!

Isn't it crazy?