package
1.12.5-sonek
Repository: https://github.com/openstars/beego.git
Documentation: pkg.go.dev

# README

validation

validation is a form validation for a data validation and error collecting using Go.

Installation and tests

Install:

go get github.com/OpenStars/beego/validation

Test:

go test github.com/OpenStars/beego/validation

Example

Direct Use:

import (
	"github.com/OpenStars/beego/validation"
	"log"
)

type User struct {
	Name string
	Age int
}

func main() {
	u := User{"man", 40}
	valid := validation.Validation{}
	valid.Required(u.Name, "name")
	valid.MaxSize(u.Name, 15, "nameMax")
	valid.Range(u.Age, 0, 140, "age")
	if valid.HasErrors() {
		// validation does not pass
		// print invalid message
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}
	// or use like this
	if v := valid.Max(u.Age, 140, "ageMax"); !v.Ok {
		log.Println(v.Error.Key, v.Error.Message)
	}
}

Struct Tag Use:

import (
	"github.com/OpenStars/beego/validation"
)

// validation function follow with "valid" tag
// functions divide with ";"
// parameters in parentheses "()" and divide with ","
// Match function's pattern string must in "//"
type user struct {
	Id   int
	Name string `valid:"Required;Match(/^(test)?\\w*@;com$/)"`
	Age  int    `valid:"Required;Range(1, 140)"`
}

func main() {
	valid := validation.Validation{}
	// ignore empty field valid
	// see CanSkipFuncs
	// valid := validation.Validation{RequiredFirst:true}
	u := user{Name: "test", Age: 40}
	b, err := valid.Valid(u)
	if err != nil {
		// handle error
	}
	if !b {
		// validation does not pass
		// blabla...
	}
}

Use custom function:

import (
	"github.com/OpenStars/beego/validation"
)

type user struct {
	Id   int
	Name string `valid:"Required;IsMe"`
	Age  int    `valid:"Required;Range(1, 140)"`
}

func IsMe(v *validation.Validation, obj interface{}, key string) {
	name, ok:= obj.(string)
	if !ok {
		// wrong use case?
		return
	}

	if name != "me" {
		// valid false
		v.SetError("Name", "is not me!")
	}
}

func main() {
	valid := validation.Validation{}
	if err := validation.AddCustomFunc("IsMe", IsMe); err != nil {
		// hadle error
	}
	u := user{Name: "test", Age: 40}
	b, err := valid.Valid(u)
	if err != nil {
		// handle error
	}
	if !b {
		// validation does not pass
		// blabla...
	}
}

Struct Tag Functions:

Required
Min(min int)
Max(max int)
Range(min, max int)
MinSize(min int)
MaxSize(max int)
Length(length int)
Alpha
Numeric
AlphaNumeric
Match(pattern string)
AlphaDash
Email
IP
Base64
Mobile
Tel
Phone
ZipCode

LICENSE

BSD License http://creativecommons.org/licenses/BSD/

# Functions

AddCustomFunc Add a custom function to validation The name can not be: Clear HasErrors ErrorMap Error Check Valid NoMatch If the name is same with exists function, it will replace the origin valid function.
SetDefaultMessage set default messages if not set, the default messages are "Required": "Can not be empty", "Min": "Minimum is %d", "Max": "Maximum is %d", "Range": "Range is %d to %d", "MinSize": "Minimum size is %d", "MaxSize": "Maximum size is %d", "Length": "Required length is %d", "Alpha": "Must be valid alpha characters", "Numeric": "Must be valid numeric characters", "AlphaNumeric": "Must be valid alpha or numeric characters", "Match": "Must match %s", "NoMatch": "Must not match %s", "AlphaDash": "Must be valid alpha or numeric or dash(-_) characters", "Email": "Must be a valid email address", "IP": "Must be a valid ip address", "Base64": "Must be valid base64 characters", "Mobile": "Must be valid mobile number", "Tel": "Must be valid telephone number", "Phone": "Must be valid telephone or mobile phone number", "ZipCode": "Must be valid zipcode",.

# Constants

No description provided by the author
ValidTag struct tag.

# Variables

CanSkipFuncs will skip valid if RequiredFirst is true and the struct field's value is empty.
ErrInt64On32 show 32 bit platform not support int64.
MessageTmpls store commond validate template.

# Structs

Alpha check the alpha.
AlphaDash check not Alpha.
AlphaNumeric check alpha and number.
Base64 check struct.
Email check struct.
Error show the error.
IP check struct.
Length Requires an array or string to be exactly a given length.
Match Requires a string to match a given regex.
Max validate struct.
MaxSize Requires an array or string to be at most a given length.
Min check struct.
MinSize Requires an array or string to be at least a given length.
Mobile check struct.
NoMatch Requires a string to not match a given regex.
Numeric check number.
Phone just for chinese telephone or mobile phone number.
Range Requires an integer to be within Min, Max inclusive.
Required struct.
Result is returned from every validation method.
Tel check telephone struct.
A Validation context manages data validation and error messages.
ValidFunc Valid function type.
ZipCode check the zip struct.

# Interfaces

Validator interface.
ValidFormer valid interface.

# Type aliases

CustomFunc is for custom validate function.
Funcs Validate function map.