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 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) DefaultFloat(f *float64, val float64) Validator
- func (v Validator) DefaultInt(i *int, val int) Validator
- func (v Validator) DefaultString(s *string, val string) Validator
- func (v Validator) Email(s, field, msg string) Validator
- func (v Validator) Errors() map[string]string
- 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 have 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 have 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) DefaultFloat ¶ added in v2.0.3
DefaultFloat sets a default value for any pointer to a float that is passed if value does not exist, will set the default specified as the new value.
Example:
v := validator.New() var f float64 v.DefaultFloat(&f, 3.14)
func (Validator) DefaultInt ¶ added in v2.0.3
DefaultInt sets a default value for any pointer to an int that is passed if value does not exist, will set the default specified as the new value.
Example:
v := validator.New() var zeroKelvin int64 v.DefaultInt(&zeroKelvin, -273)
func (Validator) DefaultString ¶ added in v2.0.3
DefaultString sets a default value for any pointer to a string that is passed. if value does not exist, will set the default specified as the new value.
Example:
v := validator.New() var lang string v.DefaultString(&lang, "persian")
func (Validator) Email ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 have less than 5 characters.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (Validator) MinFloat ¶
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 ¶
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 ¶
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 have more than 5 characters.") if v.IsFailed() { fmt.Printf("validation errors: %#v\n", v.Errors()) }
func (Validator) NotExists ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (v Validator) WithRepo(r Repository) Validator
WithRepo sets the desired repository for use in the Exists validation rule.
Example:
validator := New().WithRepo(myRepository)