Documentation ¶
Overview ¶
Package govalidator provides configurable rules for validating data of various types.
Index ¶
- Constants
- Variables
- func In[T comparable](value T, acceptableValues ...T) bool
- func Unique[T comparable](values []T) bool
- type Err
- type Repository
- type Validator
- func (v *Validator) After(t, u time.Time, field, msg string) *Validator
- func (v *Validator) Before(t, u time.Time, field, msg string) *Validator
- func (v *Validator) BetweenFloat(f, min, max float64, field, msg string) *Validator
- func (v *Validator) BetweenInt(i, min, max int, field, msg string) *Validator
- func (v *Validator) Check(ok bool, field, msg string)
- func (v *Validator) Date(layout, d, field, msg string) *Validator
- func (v *Validator) Email(s, field, msg string) *Validator
- func (v *Validator) Errors() Err
- func (v *Validator) Exists(value any, table, column, field, msg string) *Validator
- func (v *Validator) IP4(s, field, msg string) *Validator
- func (v *Validator) IsFailed() bool
- func (v *Validator) IsPassed() bool
- func (v *Validator) LenInt(i, size int, field, msg string) *Validator
- func (v *Validator) LenSlice(s []any, size int, field, msg string) *Validator
- func (v *Validator) LenString(s string, size int, field, msg string) *Validator
- func (v *Validator) MaxFloat(f, max float64, field, msg string) *Validator
- func (v *Validator) MaxInt(i, max int, field, msg string) *Validator
- func (v *Validator) MaxString(s string, maxLen int, field, msg string) *Validator
- func (v *Validator) MinFloat(f, min float64, field, msg string) *Validator
- func (v *Validator) MinInt(i, min int, field, msg string) *Validator
- func (v *Validator) MinString(s string, minLen int, field, msg string) *Validator
- func (v *Validator) NotExists(value any, table, column, field, msg string) *Validator
- func (v *Validator) RegexMatches(s string, pattern string, field, msg string) *Validator
- func (v *Validator) RequiredFloat(f float64, field string, msg string) *Validator
- func (v *Validator) RequiredInt(i int, field string, msg string) *Validator
- func (v *Validator) RequiredSlice(s []any, field string, msg string) *Validator
- func (v *Validator) RequiredString(s, field string, msg string) *Validator
- func (v *Validator) URL(s, field, msg string) *Validator
- func (v *Validator) UUID(u, field, msg string) *Validator
- func (v *Validator) When(condition bool, f func()) *Validator
- func (v *Validator) WithRepo(r Repository) *Validator
Constants ¶
const ( // After represents rule name which will be used to find the default error message. After = "after" // AfterMsg is the default error message format for fields with After validation rule. AfterMsg = "%s should be after %v" )
const ( // Before represents rule name which will be used to find the default error message. Before = "before" // BeforeMsg is the default error message format for fields with Before validation rule. BeforeMsg = "%s should be before %v" )
const ( // Between represents rule name which will be used to find the default error message. Between = "between" // BetweenMsg is the default error message format for fields with Between rule. BetweenMsg = "%s should be greater than or equal %v and less than or equal %v" )
const ( // Date represents rule name which will be used to find the default error message. Date = "date" // DateMsg is the default error message format for fields with Date validation rule. DateMsg = "%s has wrong date format" )
const ( // Email represents rule name which will be used to find the default error message. Email = "email" // EmailMsg is the default error message format for fields with Email validation rule. EmailMsg = "%s is not valid" // EmailRegex is the default pattern to validate email field by RFC 5322 rule. EmailRegex = "" /* 453-byte string literal not displayed */ )
const ( // Exists represents rule name which will be used to find the default error message. Exists = "exists" // ExistsMsg is default error message format for fields with Exists validation rule. ExistsMsg = "%s does not exist" )
const ( // IP4 represents rule name which will be used to find the default error message. IP4 = "ip4" // IP4Msg is the default error message format for fields with IP4 validation rule. IP4Msg = "%s should be a valid ipv4" )
const ( // Len represents rule name which will be used to find the default error message. Len = "len" // LenList represents rule name which will be used to find the default error message. LenList = "lenList" // LenMsg is the default error message format for fields with Len validation rule. LenMsg = "%s should be %d characters" // LenListMsg is the default error message format for fields with LenList validation rule. LenListMsg = "%s should have %d items" )
const ( // Max represents rule name which will be used to find the default error message. Max = "max" // MaxString represents rule name which will be used to find the default error message. MaxString = "maxString" // MaxMsg is the default error message format for fields with Max validation rule. MaxMsg = "%s should be less than %v" // MaxStringMsg is the default error message format for fields with MaxString validation rule. MaxStringMsg = "%s should has less than %v characters" )
const ( // Min represents rule name which will be used to find the default error message. Min = "min" // MinString represents the rule name which will be used to find the default error message. MinString = "minString" // MinMsg is the default error message format for fields with Min validation rule. MinMsg = "%s should be more than %v" // MinStringMsg is the default error message format for fields with MinString validation rule. MinStringMsg = "%s should has more than %v characters" )
const ( // NotExists represents rule name which will be used to find the default error message. NotExists = "notExists" // NotExistsMsg is default error message format for fields with with NotExists validation rule. NotExistsMsg = "%s already exists" )
const ( // Regex represents rule name which will be used to find the default error message. Regex = "regex" // RegexMsg is the default error message format for fields with Regex validation rule. RegexMsg = "%s is not valid" )
const ( // Required represents rule name which will be used to find the default error message. Required = "required" // RequiredMsg is the default error message format for fields with required validation rule. RequiredMsg = "%s is required" )
const ( // URL represents rule name which will be used to find the default error message. URL = "url" // URLMsg is the default error message format for fields with Url validation rule. URLMsg = "%s should be a valid url" )
const ( // UUID represents rule name which will be used to find the default error message. UUID = "uuid" // UUIDMsg is the default error message format for fields with UUID validation rule. UUIDMsg = "%s is not a valid UUID" )
Variables ¶
var ( // ErrMethodMessageNotFound is the default message when a method does not have any error message on methodToErrorMessage. ErrMethodMessageNotFound = errors.New("method default validation message does not exist in methodToErrorMessage") )
Functions ¶
func In ¶ added in v0.0.17
func In[T comparable](value T, acceptableValues ...T) bool
In checks if the value under validation must be included in the given list of values.
Example:
result := validator.In("apple", "banana", "orange", "apple") // result will be true because "apple" is included in the list of acceptable values.
func Unique ¶ added in v0.0.17
func Unique[T comparable](values []T) bool
Unique checks whether the values in the provided slice are unique.
Example:
values := []int{1, 2, 3, 4, 5} result := validator.Unique(values) // result will be true because all values in the slice are unique.
Types ¶
type Repository ¶ added in v0.0.14
Repository represent a repository for using in rules that needs a database connection to check a record exists on database or not.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator represents the validator structure
func (*Validator) After ¶ added in v1.1.3
After checks if given time instant t is after u.
Example:
v := validator.New() t, _ := time.Parse("2006-01-02", "2009-01-02") // error ignored for simplicity u, _ := time.Parse("2006-01-02", "2012-01-01") // error ignored for simplicity v.After(t, u, "birth_date", "birth_date should be after 2012-01-01.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) Before ¶ added in v1.1.3
Before checks if given time instant t is before u.
Example:
v := validator.New() t, _ := time.Parse("2006-01-02", "2009-01-02") // error ignored for simplicity u, _ := time.Parse("2006-01-02", "2012-01-01") // error ignored for simplicity v.Before(t, u, "birth_date", "birth_date should be before 2012-01-01.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) BetweenFloat ¶ added in v0.0.14
BetweenFloat checks the field under validation to have a float value between the given min and max.
Example:
v := validator.New() v.BetweenFloat(3.5, 2.0, 5.0, "height", "height must be between 2.0 and 5.0 meters.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) BetweenInt ¶ added in v0.0.14
BetweenInt checks the value under validation to have an integer value between the given min and max.
Example:
v := validator.New() v.BetweenInt(21, 1, 10, "age", "age must be between 1 and 10.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) Check ¶
Check is a dynamic method to define any custom validator rule by passing a rule as a function or expression which will return a boolean.
func (*Validator) Date ¶ added in v0.0.19
Date checks the value under validation to be a valid, non-relative date with give layout.
Example:
v := validator.New() v.Date("2006-01-02", "2024-03-09","birthdate", "birthdate must be a valid date in the format YYYY-MM-DD.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) Email ¶ added in v0.0.17
Email checks the value under validation must match the EmailRegex regular expression.
Example:
v := validator.New() v.Email("john.doe@example.com", "email", "email address is not valid.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) Exists ¶ added in v0.0.14
Exists checks if given value exists in the desired table or not.
Example:
v := validator.New() v.Exists(42, "users", "id", "user_id", "user with id 42 does not exist.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) IP4 ¶ added in v1.1.3
IP4 checks if given string is a valid IPV4.
Example:
v := validator.New() v.IP4("127.0.0.1", "server_ip", "server_ip should be a valid ipv4.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) LenInt ¶ added in v0.0.12
LenInt checks if the length of the given integer is equal to the given size or not.
Example:
v := validator.New() v.LenInt(12345, 5, "zipcode", "Zip code must be 5 digits long.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) LenSlice ¶ added in v0.0.14
LenSlice checks if the length of the given slice is equal to the given size or not.
Example:
v := validator.New() v.LenSlice([]int{1, 2, 3, 4, 5}, 5, "numbers", "the list must contain exactly 5 numbers.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) LenString ¶ added in v0.0.12
LenString checks if the length of a string is equal to the given size or not.
Example:
v := validator.New() v.LenString("rez", 5, "username", "username must be 5 characters.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) MaxFloat ¶ added in v0.0.14
MaxFloat checks if the given float value is less than or equal the given max value.
Example:
v := validator.New() v.MaxFloat(3.5, 5.0, "height", "height must be less than 5.0 meters.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) MaxInt ¶ added in v0.0.9
MaxInt checks if the integer value is less than or equal the given max value.
Example:
v := validator.New() v.MaxInt(10, 100, "age", "age must be less than 100.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) MaxString ¶ added in v0.0.20
MaxString checks if the length of given string is less than or equal the given max value.
Example:
v := validator.New() v.MaxString("rey", 5, "name", "name should has less than 5 characters.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) MinFloat ¶ added in v0.0.14
MinFloat checks if the given float value is greater than or equal the given min value.
Example:
v := validator.New() v.MinFloat(5.0, 0.0, "height", "height must be at least 0.0 meters.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) MinInt ¶ added in v0.0.9
MinInt checks if the given integer value is greater than or equal the given min value.
Example:
v := validator.New() v.MinInt(18, 0, "age", "age must be at least 0.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) MinString ¶ added in v0.0.20
MinString checks if the length of given string is greater than or equal the given min value.
Example:
v := validator.New() v.MinString("rey", 5, "name", "name should has more than 5 characters.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) NotExists ¶ added in v0.0.17
NotExists checks if the given value doesn't exist in the desired table.
Example:
v := validator.New() v.NotExists(42, "users", "id", "user_id", "user with id 42 already exists.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) RegexMatches ¶ added in v0.0.17
RegexMatches checks if the given value of s under validation matches the given regular expression pattern.
Example:
v := validator.New() v.RegexMatches("example123", "[a-z]+[0-9]+", "input", "input must contain letters followed by numbers.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) RequiredFloat ¶ added in v0.0.17
RequiredFloat checks if a float value is provided or not.
Example:
v := validator.New() v.RequiredFloat(3.5, "weight", "weight is required.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) RequiredInt ¶ added in v0.0.8
RequiredInt checks if an integer value is provided or not.
Example:
v := validator.New() v.RequiredInt(42, "age", "age is required.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) RequiredSlice ¶ added in v0.0.14
RequiredSlice checks if a slice has any value or not.
Example:
v := validator.New() v.RequiredSlice([]string{"apple", "banana", "orange"}, "fruits", "at least one fruit must be provided.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) RequiredString ¶ added in v0.0.8
RequiredString checks if a string value is empty or not.
Example:
v := validator.New() v.RequiredString("hello", "username", "username is required.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) URL ¶ added in v1.1.3
URL checks if a string value is a valid url or not.
Example:
v := validator.New() v.URL("https://go.dev/play", "path", "path should be a valid url.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) UUID ¶ added in v0.0.18
UUID validates that the field under validation is a valid RFC 4122 universally unique identifier (UUID). It accepts non-standard strings such as raw hex encoding xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and 38 byte "Microsoft style" encodings, e.g., {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}.
Example:
v := validator.New() v.UUID("f47ac10b-58cc-4372-a567-0e02b2c3d479", "uuid", "Invalid UUID format.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) When ¶ added in v0.0.18
When method will execute the given closure if the given condition is true.
Example:
v := validator.New() v.When(len(username) > 0, func() { validator.RequiredString(username, "username", "username is required.") }) if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (*Validator) WithRepo ¶ added in v0.0.14
func (v *Validator) WithRepo(r Repository) *Validator
WithRepo sets the desired repository for use in the Exists validation rule.
Example:
validator := New().WithRepo(myRepository)