Documentation ¶
Overview ¶
Package v2 Checker is a Go library for validating user input through checker rules provided in struct tags.
Index ¶
- Constants
- Variables
- func Check[T any](value T, checks ...CheckFunc[T]) (T, error)
- func CheckStruct(st any) (map[string]error, bool)
- func CheckWithConfig[T any](value T, config string) (T, error)
- func HTMLEscape(value string) (string, error)
- func HTMLUnescape(value string) (string, error)
- func IsASCII(value string) (string, error)
- func IsAlphanumeric(value string) (string, error)
- func IsAmexCreditCard(number string) (string, error)
- func IsAnyCreditCard(number string) (string, error)
- func IsCIDR(value string) (string, error)
- func IsDigits(value string) (string, error)
- func IsDinersCreditCard(number string) (string, error)
- func IsDiscoverCreditCard(number string) (string, error)
- func IsEmail(value string) (string, error)
- func IsFQDN(value string) (string, error)
- func IsGte[T cmp.Ordered](value, n T) (T, error)
- func IsHex(value string) (string, error)
- func IsIP(value string) (string, error)
- func IsIPv4(value string) (string, error)
- func IsIPv6(value string) (string, error)
- func IsISBN(value string) (string, error)
- func IsJcbCreditCard(number string) (string, error)
- func IsLUHN(value string) (string, error)
- func IsLte[T cmp.Ordered](value, n T) (T, error)
- func IsMAC(value string) (string, error)
- func IsMasterCardCreditCard(number string) (string, error)
- func IsRegexp(expression, value string) (string, error)
- func IsURL(value string) (string, error)
- func IsUnionPayCreditCard(number string) (string, error)
- func IsVisaCreditCard(number string) (string, error)
- func Lower(value string) (string, error)
- func ReflectCheckWithConfig(value reflect.Value, config string) (reflect.Value, error)
- func RegisterLocale(locale string, messages map[string]string)
- func RegisterMaker(name string, maker MakeCheckFunc)
- func Required[T any](value T) (T, error)
- func Title(value string) (string, error)
- func TrimLeft(value string) (string, error)
- func TrimRight(value string) (string, error)
- func TrimSpace(value string) (string, error)
- func URLEscape(value string) (string, error)
- func URLUnescape(value string) (string, error)
- func Upper(value string) (string, error)
- type CheckError
- type CheckFunc
- type MakeCheckFunc
Examples ¶
Constants ¶
const ( // DefaultLocale is the default locale. DefaultLocale = locales.EnUS )
Variables ¶
var ( // ErrGte indicates that the value is not greater than or equal to the given value. ErrGte = NewCheckError("NOT_GTE") )
var ( // ErrLte indicates that the value is not less than or equal to the given value. ErrLte = NewCheckError("NOT_LTE") )
var ( // ErrMaxLen indicates that the value's length is greater than the specified maximum. ErrMaxLen = NewCheckError("NOT_MAX_LEN") )
var ( // ErrMinLen indicates that the value's length is less than the specified minimum. ErrMinLen = NewCheckError("NOT_MIN_LEN") )
var ( // ErrNotASCII indicates that the given string contains non-ASCII characters. ErrNotASCII = NewCheckError("NOT_ASCII") )
var ( // ErrNotAlphanumeric indicates that the given string contains non-alphanumeric characters. ErrNotAlphanumeric = NewCheckError("NOT_ALPHANUMERIC") )
var ( // ErrNotCIDR indicates that the given value is not a valid CIDR. ErrNotCIDR = NewCheckError("NOT_CIDR") )
var ( // ErrNotCreditCard indicates that the given value is not a valid credit card number. ErrNotCreditCard = NewCheckError("NOT_CREDIT_CARD") )
var ( // ErrNotDigits indicates that the given value is not a valid digits string. ErrNotDigits = NewCheckError("NOT_DIGITS") )
var ( // ErrNotEmail indicates that the given value is not a valid email address. ErrNotEmail = NewCheckError("NOT_EMAIL") )
var ( // ErrNotFQDN indicates that the given value is not a valid FQDN. ErrNotFQDN = NewCheckError("FQDN") )
var ( // ErrNotHex indicates that the given string contains hex characters. ErrNotHex = NewCheckError("NOT_HEX") )
var ( // ErrNotIP indicates that the given value is not a valid IP address. ErrNotIP = NewCheckError("NOT_IP") )
var ( // ErrNotIPv4 indicates that the given value is not a valid IPv4 address. ErrNotIPv4 = NewCheckError("NOT_IPV4") )
var ( // ErrNotIPv6 indicates that the given value is not a valid IPv6 address. ErrNotIPv6 = NewCheckError("NOT_IPV6") )
var ( // ErrNotISBN indicates that the given value is not a valid ISBN. ErrNotISBN = NewCheckError("NOT_ISBN") )
var ( // ErrNotLUHN indicates that the given value is not a valid LUHN number. ErrNotLUHN = NewCheckError("NOT_LUHN") )
var ( // ErrNotMAC indicates that the given value is not a valid MAC address. ErrNotMAC = NewCheckError("NOT_MAC") )
var ErrNotMatch = NewCheckError("REGEXP")
ErrNotMatch indicates that the given string does not match the regexp pattern.
var ( // ErrNotURL indicates that the given value is not a valid URL. ErrNotURL = NewCheckError("NOT_URL") )
var ( // ErrRequired indicates that a required value was missing. ErrRequired = NewCheckError("REQUIRED") )
Functions ¶
func Check ¶
Check applies the given check functions to a value sequentially. It returns the final value and the first encountered error, if any.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { name := " Onur Cinar " name, err := v2.Check(name, v2.TrimSpace, v2.Required) if err != nil { fmt.Println(err) return } fmt.Println(name) }
Output: Onur Cinar
func CheckStruct ¶
CheckStruct checks the given struct based on the validation rules specified in the "checker" tag of each struct field. It returns a map of field names to their corresponding errors, and a boolean indicating if all checks passed.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { type Person struct { Name string `checkers:"trim required"` } person := &Person{ Name: " Onur Cinar ", } errs, ok := v2.CheckStruct(person) if !ok { fmt.Println(errs) return } fmt.Println(person.Name) }
Output: Onur Cinar
func CheckWithConfig ¶
CheckWithConfig applies the check functions specified by the config string to the given value. It returns the modified value and the first encountered error, if any.
func HTMLEscape ¶
HTMLEscape applies HTML escaping to special characters.
func HTMLUnescape ¶
HTMLUnescape applies HTML unescaping to special characters.
func IsASCII ¶
IsASCII checks if the given string consists of only ASCII characters.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsASCII("Checker") if err != nil { fmt.Println(err) } }
Output:
func IsAlphanumeric ¶
IsAlphanumeric checks if the given string consists of only alphanumeric characters.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsAlphanumeric("ABcd1234") if err != nil { fmt.Println(err) } }
Output:
func IsAmexCreditCard ¶
IsAmexCreditCard checks if the given valie is a valid AMEX credit card.
Example ¶
package main import ( v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsAmexCreditCard("378282246310005") if err != nil { // Send the errors back to the user } }
Output:
func IsAnyCreditCard ¶
IsAnyCreditCard checks if the given value is a valid credit card number.
Example ¶
package main import ( v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsAnyCreditCard("6011111111111117") if err != nil { // Send the errors back to the user } }
Output:
func IsCIDR ¶
IsCIDR checks if the value is a valid CIDR notation IP address and prefix length.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsCIDR("2001:db8::/32") if err != nil { fmt.Println(err) } }
Output:
func IsDigits ¶
IsDigits checks if the value contains only digit characters.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsDigits("123456") if err != nil { fmt.Println(err) } }
Output:
func IsDinersCreditCard ¶
IsDinersCreditCard checks if the given valie is a valid Diners credit card.
Example ¶
package main import ( v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsDinersCreditCard("36227206271667") if err != nil { // Send the errors back to the user } }
Output:
func IsDiscoverCreditCard ¶
IsDiscoverCreditCard checks if the given valie is a valid Discover credit card.
Example ¶
package main import ( v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsDiscoverCreditCard("6011111111111117") if err != nil { // Send the errors back to the user } }
Output:
func IsEmail ¶
IsEmail checks if the value is a valid email address.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsEmail("test@example.com") if err != nil { fmt.Println(err) } }
Output:
func IsFQDN ¶
IsFQDN checks if the value is a valid fully qualified domain name (FQDN).
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsFQDN("example.com") if err != nil { fmt.Println(err) } }
Output:
func IsHex ¶
IsHex checks if the given string consists of only hex characters.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsHex("0123456789abcdefABCDEF") if err != nil { fmt.Println(err) } }
Output:
func IsIP ¶
IsIP checks if the value is a valid IP address.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsIP("192.168.1.1") if err != nil { fmt.Println(err) } }
Output:
func IsIPv4 ¶
IsIPv4 checks if the value is a valid IPv4 address.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsIPv4("192.168.1.1") if err != nil { fmt.Println(err) } }
Output:
func IsIPv6 ¶
IsIPv6 checks if the value is a valid IPv6 address.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsIPv6("2001:db8::1") if err != nil { fmt.Println(err) } }
Output:
func IsISBN ¶
IsISBN checks if the value is a valid ISBN-10 or ISBN-13.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsISBN("1430248270") if err != nil { fmt.Println(err) } }
Output:
func IsJcbCreditCard ¶
IsJcbCreditCard checks if the given valie is a valid JCB 15 credit card.
Example ¶
package main import ( v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsJcbCreditCard("3530111333300000") if err != nil { // Send the errors back to the user } }
Output:
func IsLUHN ¶
IsLUHN checks if the value is a valid LUHN number.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsLUHN("4012888888881881") if err != nil { fmt.Println(err) } }
Output:
func IsMAC ¶
IsMAC checks if the value is a valid MAC address.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsMAC("00:1A:2B:3C:4D:5E") if err != nil { fmt.Println(err) } }
Output:
func IsMasterCardCreditCard ¶
IsMasterCardCreditCard checks if the given valie is a valid MasterCard credit card.
Example ¶
package main import ( v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsMasterCardCreditCard("5555555555554444") if err != nil { // Send the errors back to the user } }
Output:
func IsRegexp ¶
IsRegexp checks if the given string matches the given regexp expression.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsRegexp("^[0-9a-fA-F]+$", "ABcd1234") if err != nil { fmt.Println(err) } }
Output:
func IsURL ¶
IsURL checks if the value is a valid URL.
Example ¶
package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsURL("https://example.com") if err != nil { fmt.Println(err) } }
Output:
func IsUnionPayCreditCard ¶
IsUnionPayCreditCard checks if the given valie is a valid UnionPay credit card.
Example ¶
package main import ( v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsUnionPayCreditCard("6200000000000005") if err != nil { // Send the errors back to the user } }
Output:
func IsVisaCreditCard ¶
IsVisaCreditCard checks if the given valie is a valid Visa credit card.
Example ¶
package main import ( v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsVisaCreditCard("4111111111111111") if err != nil { // Send the errors back to the user } }
Output:
func ReflectCheckWithConfig ¶
ReflectCheckWithConfig applies the check functions specified by the config string to the given reflect.Value. It returns the modified reflect.Value and the first encountered error, if any.
func RegisterLocale ¶
RegisterLocale registers the localized error messages for the given locale.
func RegisterMaker ¶
func RegisterMaker(name string, maker MakeCheckFunc)
RegisterMaker registers a new maker function with the given name.
Example ¶
package main import ( "fmt" "reflect" "github.com/cinar/checker/v2/locales" v2 "github.com/cinar/checker/v2" ) func main() { locales.EnUSMessages["NOT_FRUIT"] = "Not a fruit name." v2.RegisterMaker("is-fruit", func(params string) v2.CheckFunc[reflect.Value] { return func(value reflect.Value) (reflect.Value, error) { stringValue := value.Interface().(string) if stringValue == "apple" || stringValue == "banana" { return value, nil } return value, v2.NewCheckError("NOT_FRUIT") } }) type Item struct { Name string `checkers:"is-fruit"` } person := &Item{ Name: "banana", } err, ok := v2.CheckStruct(person) if !ok { fmt.Println(err) } }
Output:
func Required ¶
Required checks if the given value of type T is its zero value. It returns an error if the value is zero.
func Title ¶
Title returns the value of the string with the first letter of each word in upper case.
func TrimLeft ¶
TrimLeft returns the value of the string with whitespace removed from the beginning.
func URLUnescape ¶
URLUnescape applies URL unescaping to special characters.
Types ¶
type CheckError ¶
type CheckError struct { // Code is the error code. Code string // data is the error data. Data map[string]interface{} }
CheckError defines the check error.
func NewCheckError ¶
func NewCheckError(code string) *CheckError
NewCheckError creates a new check error with the given code.
func NewCheckErrorWithData ¶
func NewCheckErrorWithData(code string, data map[string]interface{}) *CheckError
NewCheckErrorWithData creates a new check error with the given code and data.
func (*CheckError) Error ¶
func (c *CheckError) Error() string
Error returns the error message for the check.
func (*CheckError) ErrorWithLocale ¶
func (c *CheckError) ErrorWithLocale(locale string) string
ErrorWithLocale returns the localized error message for the check with the given locale.
func (*CheckError) Is ¶
func (c *CheckError) Is(target error) bool
Is reports whether the check error is the same as the target error.
type CheckFunc ¶
CheckFunc is a function that takes a value of type T and performs a check on it. It returns the resulting value and any error that occurred during the check.
func MakeRegexpChecker ¶
MakeRegexpChecker makes a regexp checker for the given regexp expression with the given invalid result.
Source Files ¶
- alphanumeric.go
- ascii.go
- check_error.go
- check_func.go
- checker.go
- cidr.go
- credit_card.go
- digits.go
- email.go
- fqdn.go
- gte.go
- hex.go
- html_escape.go
- html_unescape.go
- ip.go
- ipv4.go
- ipv6.go
- isbn.go
- lower.go
- lte.go
- luhn.go
- mac.go
- maker.go
- max_len.go
- min_len.go
- regexp.go
- required.go
- title.go
- trim_left.go
- trim_right.go
- trim_space.go
- upper.go
- url.go
- url_escape.go
- url_unescape.go