v

package module
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: BSD-2-Clause Imports: 6 Imported by: 0

README

Validation library

Allows define custom validation rule

  1. Implement callback according to the ValidationCallback signature
    func(val reflect.Value, args ...string) bool
    
  2. Define custom validation map
    var MyValidation = map[string]ValidationCallback{"custom": func(val reflect.Value, args ...string) bool{return true}}
    
  3. Call PrepareActualValidationRules at start of application
    v.PrepareActualValidationRules(MyValidation)
    

How to use with validation rules

Form validation use combination of validation rules. Possible rules as parts of 'valid' tag:

  • required. Filed is required
  • rx. Regular expression
  • range. Range if values
  • enum. Predefined enum
  • min. Minimum value or length
  • max. Maximum value or length
  • digit. Only digits in value. Can specify length
  • notnull. Filed must be not null

Example: valid:"required;rx~[0-5]+;range~1:50;enum~5,10,15,20,25;digit~4,10;min~3;max~10"

You can ignore field for validation specify valid tag as "-" Example: valid:"-"

type ComplexStruct struct {
	Cool bool
}
type AliasOnTypeString string
type Nested struct {
	Foo int32 `json:"foo"`
	Bar *bool `json:"bar" valid:"required"`
}
type TestValidationStruct struct {
	Name      string            `json:"name" valid:"required;rx~[a-z]+"`
	Number    int               `json:"number" valid:"notnull;rx~[0-5]+;range~1:50;enum~5,10,15,20,25"`
	IsTrue    *bool             `json:"isTrue"`
	Complex   *ComplexStruct    `json:"complex" valid:"required"`
	Sl        []int64           `json:"sl"`
	SuperName AliasOnTypeString `json:"superName" valid:"required"`
	Nested    Nested            `json:"nested"`
}
v := TestValidationStruct{Complex: &ComplexStruct{}}
e := ValidateStruct(&v)
if e != nil {
   panic(e)
}
If you find this project useful or want to support the author, you can send tokens to any of these wallets
  • Bitcoin: bc1qgx5c3n7q26qv0tngculjz0g78u6mzavy2vg3tf
  • Ethereum: 0x62812cb089E0df31347ca32A1610019537bbFe0D
  • Dogecoin: DET7fbNzZftp4sGRrBehfVRoi97RiPKajV

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDigits

func IsDigits(val reflect.Value, args ...string) bool

IsDigits check for digits

func IsEnumValid

func IsEnumValid(val reflect.Value, args ...string) bool

IsEnumValid In list validation rule

func IsMaxValid

func IsMaxValid(val reflect.Value, args ...string) bool

IsMaxValid check max

func IsMinValid

func IsMinValid(val reflect.Value, args ...string) bool

IsMinValid check min

func IsNotNullValid added in v0.1.2

func IsNotNullValid(val reflect.Value, args ...string) bool

IsNotNullValid Not null validation rule

func IsRangeValid

func IsRangeValid(val reflect.Value, args ...string) bool

IsRangeValid Range list validation rule

func IsRegularValid

func IsRegularValid(val reflect.Value, args ...string) bool

IsRegularValid check regular expression

func IsRequiredValid

func IsRequiredValid(val reflect.Value, args ...string) bool

IsRequiredValid Required validation rule

func PrepareActualValidationRules

func PrepareActualValidationRules(customValidationRules map[string]ValidationCallback)

PrepareActualValidationRules func to append basicValidationRules or replace existing rules customValidationRules If you want to use your own validation rules add the rules in to customValidationRules var

func ValidateStruct

func ValidateStruct(v interface{}) porterr.IError

ValidateStruct struct fields validation

Types

type ValidationCallback

type ValidationCallback func(val reflect.Value, args ...string) bool

ValidationCallback function that performs validation rule

type ValidationRule

type ValidationRule struct {
	// Validator name
	Name string
	// Validator argument
	Args []string
}

ValidationRule validation params

type ValidationRules added in v0.3.0

type ValidationRules []ValidationRule

ValidationRules list of validation rules

func ParseValidTag

func ParseValidTag(validTag string) ValidationRules

ParseValidTag parse validation tag for rule and arguments Example valid:"rx~[0-5]+;range~1-50;enum~5,10,15,20,25"`

Jump to

Keyboard shortcuts

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