tiaguinho / required

Validate value of field

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Required Build Status GoDoc Go Report Card codecov

Required is used to validate if the field value is empty

Install

go get github.com/tiaguinho/required

Example

Simplest way to use the package:

package main

import (
    "github.com/tiaguinho/required"
    "log"
)

type Test struct {
    FirstName string `json:"first_name" required:"-"`
    LastName  string `json:"last_name" required:"last name is required"`
}

func main()  {
    t := Test{
        FirstName: "Go",
        LastName: "Required",
    }
    
    if err := required.Validate(t); err != nil {
        log.Println(err)
    }
}

If you like to get all the validation messages with field's name to return in some API, just change to this:

func main() {
    t := Test{
         FirstName: "Go",
         LastName: "Required",
     }
     
     if msg, err := required.ValidateWithMessage(t); err != nil {
         log.Println(err, msg)
     }
}

About

Validate value of field

License:MIT License


Languages

Language:Go 100.0%