Skip to content

tiaguinho/required

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
     }
}