validator

package module
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 8 Imported by: 2

README

Go Struct Validator

Introduction
  • The purpose of this project is to create error messages with proper structure that are easy for both users and developers to understand and use.
  • Our goal is to provide error messages that include the name of the property in response and are simple to use in frontend applications, specifically in forms to display error messages for specific fields.
  • This will help improve the user experience by making it easier for users to understand and resolve errors that may occur while using the system.
Installation

Use go get

go get -u github.com/shariarfaisal/validator

Import into your project

import "github.com/shariarfaisal/validator"
Tags
Tag Description
required Field is not empty or undefined
email Email validity
date Date format validity
min Minimum value for number and minimum length for string value
max Maximum value for number and maximum length for string value
enum Is input data is enum
include Is data contains or not in between given values
eq Is data equal
ne Is data not eqal
gt Is data greater than
gte Is data greater than or equal
lt Is data less than
lte Is data less than or equal
url Is valid url
ip Is valid ip address
ipv4 is valid ip address v4
item.[tag] Map & slice items validation (Only for int value and strings types)
Functions
Func Arguments Description Return
IsValidDate (date string) Check date's format validity bool
IsValidEmail (email string) Check email validity bool
IsValidURL (url string) Check url validity bool
IsValidIP (ip string) Check ip address validity bool
IsValidIpV4 (ip string) Check ip address v4 validity bool
Example
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/shariarfaisal/validator"
)

type Address struct {
	Label string `json:"label" v:"enum=Home,Office"`
	Street string `json:"street" v:"required"`
	City string `json:"city" v:"required"`
}

type User struct {
	Name  string `json:"name" v:"required;min=3;max=20"`
	Age   int    `json:"age" v:"required;min=18;max=60"`
	Email string `json:"email" v:"required;email"`
	Addresses []Address `json:"addresses" v:"required;min=1;max=3"`
	DateOfBirth string `json:"dateOfBirth" v:"required;date"`
	Gender string `json:"gender" v:"enum=male,female"`
	Major []string `json:"major" v:"required;item.enum=Math,Physics,English"`
}

func main() {
	user := User{
		Name:  "test",
		Age:   17,
		Email: "example@gmail.com",
		Addresses: []Address{
			{
				Label: "Home",
			},
		},
		Gender: "mole",
		Major: "Math",
	}

	// Validate the user
	ok, errors := validator.Validate(user)
	if !ok {
		fmt.Println(json.NewEncoder(os.Stdout).Encode(errors))
	}

	// Email validation using function
	if isValid := validator.IsValidEmail(user.Email); isValid {
		fmt.Println("Email is valid")
	}

}

Console Output
{
   "addresses":{
      "0":{
         "city":"This field is required",
         "street":"This field is required"
      }
   },
   "age":"This field must be at least 18",
   "dateOfBirth":"This field is required",
   "gender":"This field must be one of the following values: male,female"
}

Email is valid
Contributing

We welcome contributions to this project! If you have an idea for a new feature or have found a bug, please open an issue on GitHub. If you would like to contribute code, please follow these guidelines:

  • Fork the repository and create a new branch for your changes.
  • Make your changes, including appropriate tests.
  • Submit a pull request for review.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidDate

func IsValidDate(date string) bool

Date string's validation

func IsValidEmail

func IsValidEmail(v string) bool

Email validation

func IsValidIP

func IsValidIP(v string) bool

IP validation

func IsValidIpV4

func IsValidIpV4(v string) bool

IPv4 validation

func IsValidURL

func IsValidURL(v string) bool

URL validation

func Validate

func Validate(s interface{}) (valid bool, errors map[string]interface{})

Validate struct @return bool - true if no error @return map[string]interface{} - errors

Types

type Field added in v1.3.0

type Field struct {
	Name     string
	Value    reflect.Value
	Prefix   string
	TagValue string
}

func NewField added in v1.3.0

func NewField(name string, value reflect.Value, tagValue string, prefix string) *Field

func (*Field) Enum added in v1.3.0

func (f *Field) Enum() (err string)

func (*Field) Equal added in v1.3.0

func (f *Field) Equal() (err string)

func (*Field) GreaterThan added in v1.3.0

func (f *Field) GreaterThan() (err string)

func (*Field) GreaterThanOrEqual added in v1.3.0

func (f *Field) GreaterThanOrEqual() (err string)

func (*Field) Include added in v1.3.0

func (f *Field) Include() (err string)

func (*Field) LessThan added in v1.3.0

func (f *Field) LessThan() (err string)

func (*Field) LessThanOrEqual added in v1.3.0

func (f *Field) LessThanOrEqual() (err string)

func (*Field) Max added in v1.3.0

func (f *Field) Max() (err string)

func (*Field) Min added in v1.3.0

func (f *Field) Min() (err string)

func (*Field) NotEqual added in v1.3.0

func (f *Field) NotEqual() (err string)

func (*Field) Required added in v1.3.0

func (f *Field) Required() (err string)

func (*Field) Validate added in v1.3.0

func (f *Field) Validate() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL