# v2 ```go import "github.com/cinar/checker/v2" ``` Package v2 Checker is a Go library for validating user input through checker rules provided in struct tags. ## Index - [Constants](<#constants>) - [Variables](<#variables>) - [func Check\[T any\]\(value T, checks ...CheckFunc\[T\]\) \(T, error\)](<#Check>) - [func CheckStruct\(st any\) \(map\[string\]error, bool\)](<#CheckStruct>) - [func CheckWithConfig\[T any\]\(value T, config string\) \(T, error\)](<#CheckWithConfig>) - [func HTMLEscape\(value string\) \(string, error\)](<#HTMLEscape>) - [func HTMLUnescape\(value string\) \(string, error\)](<#HTMLUnescape>) - [func IsASCII\(value string\) \(string, error\)](<#IsASCII>) - [func IsAlphanumeric\(value string\) \(string, error\)](<#IsAlphanumeric>) - [func IsAmexCreditCard\(number string\) \(string, error\)](<#IsAmexCreditCard>) - [func IsAnyCreditCard\(number string\) \(string, error\)](<#IsAnyCreditCard>) - [func IsCIDR\(value string\) \(string, error\)](<#IsCIDR>) - [func IsDigits\(value string\) \(string, error\)](<#IsDigits>) - [func IsDinersCreditCard\(number string\) \(string, error\)](<#IsDinersCreditCard>) - [func IsDiscoverCreditCard\(number string\) \(string, error\)](<#IsDiscoverCreditCard>) - [func IsEmail\(value string\) \(string, error\)](<#IsEmail>) - [func IsFQDN\(value string\) \(string, error\)](<#IsFQDN>) - [func IsGte\[T cmp.Ordered\]\(value, n T\) \(T, error\)](<#IsGte>) - [func IsHex\(value string\) \(string, error\)](<#IsHex>) - [func IsIP\(value string\) \(string, error\)](<#IsIP>) - [func IsIPv4\(value string\) \(string, error\)](<#IsIPv4>) - [func IsIPv6\(value string\) \(string, error\)](<#IsIPv6>) - [func IsISBN\(value string\) \(string, error\)](<#IsISBN>) - [func IsJcbCreditCard\(number string\) \(string, error\)](<#IsJcbCreditCard>) - [func IsLUHN\(value string\) \(string, error\)](<#IsLUHN>) - [func IsLte\[T cmp.Ordered\]\(value, n T\) \(T, error\)](<#IsLte>) - [func IsMAC\(value string\) \(string, error\)](<#IsMAC>) - [func IsMasterCardCreditCard\(number string\) \(string, error\)](<#IsMasterCardCreditCard>) - [func IsRegexp\(expression, value string\) \(string, error\)](<#IsRegexp>) - [func IsTime\(params, value string\) \(string, error\)](<#IsTime>) - [func IsURL\(value string\) \(string, error\)](<#IsURL>) - [func IsUnionPayCreditCard\(number string\) \(string, error\)](<#IsUnionPayCreditCard>) - [func IsVisaCreditCard\(number string\) \(string, error\)](<#IsVisaCreditCard>) - [func Lower\(value string\) \(string, error\)](<#Lower>) - [func ReflectCheckWithConfig\(value reflect.Value, config string\) \(reflect.Value, error\)](<#ReflectCheckWithConfig>) - [func RegisterLocale\(locale string, messages map\[string\]string\)](<#RegisterLocale>) - [func RegisterMaker\(name string, maker MakeCheckFunc\)](<#RegisterMaker>) - [func Required\[T any\]\(value T\) \(T, error\)](<#Required>) - [func Title\(value string\) \(string, error\)](<#Title>) - [func TrimLeft\(value string\) \(string, error\)](<#TrimLeft>) - [func TrimRight\(value string\) \(string, error\)](<#TrimRight>) - [func TrimSpace\(value string\) \(string, error\)](<#TrimSpace>) - [func URLEscape\(value string\) \(string, error\)](<#URLEscape>) - [func URLUnescape\(value string\) \(string, error\)](<#URLUnescape>) - [func Upper\(value string\) \(string, error\)](<#Upper>) - [type CheckError](<#CheckError>) - [func NewCheckError\(code string\) \*CheckError](<#NewCheckError>) - [func NewCheckErrorWithData\(code string, data map\[string\]interface\{\}\) \*CheckError](<#NewCheckErrorWithData>) - [func \(c \*CheckError\) Error\(\) string](<#CheckError.Error>) - [func \(c \*CheckError\) ErrorWithLocale\(locale string\) string](<#CheckError.ErrorWithLocale>) - [func \(c \*CheckError\) Is\(target error\) bool](<#CheckError.Is>) - [type CheckFunc](<#CheckFunc>) - [func MakeRegexpChecker\(expression string, invalidError error\) CheckFunc\[reflect.Value\]](<#MakeRegexpChecker>) - [func MaxLen\[T any\]\(n int\) CheckFunc\[T\]](<#MaxLen>) - [func MinLen\[T any\]\(n int\) CheckFunc\[T\]](<#MinLen>) - [type MakeCheckFunc](<#MakeCheckFunc>) ## Constants ```go const ( // DefaultLocale is the default locale. DefaultLocale = locales.EnUS ) ``` ## Variables ```go var ( // ErrGte indicates that the value is not greater than or equal to the given value. ErrGte = NewCheckError("NOT_GTE") ) ``` ```go var ( // ErrLte indicates that the value is not less than or equal to the given value. ErrLte = NewCheckError("NOT_LTE") ) ``` ```go var ( // ErrMaxLen indicates that the value's length is greater than the specified maximum. ErrMaxLen = NewCheckError("NOT_MAX_LEN") ) ``` ```go var ( // ErrMinLen indicates that the value's length is less than the specified minimum. ErrMinLen = NewCheckError("NOT_MIN_LEN") ) ``` ```go var ( // ErrNotASCII indicates that the given string contains non-ASCII characters. ErrNotASCII = NewCheckError("NOT_ASCII") ) ``` ```go var ( // ErrNotAlphanumeric indicates that the given string contains non-alphanumeric characters. ErrNotAlphanumeric = NewCheckError("NOT_ALPHANUMERIC") ) ``` ```go var ( // ErrNotCIDR indicates that the given value is not a valid CIDR. ErrNotCIDR = NewCheckError("NOT_CIDR") ) ``` ```go var ( // ErrNotCreditCard indicates that the given value is not a valid credit card number. ErrNotCreditCard = NewCheckError("NOT_CREDIT_CARD") ) ``` ```go var ( // ErrNotDigits indicates that the given value is not a valid digits string. ErrNotDigits = NewCheckError("NOT_DIGITS") ) ``` ```go var ( // ErrNotEmail indicates that the given value is not a valid email address. ErrNotEmail = NewCheckError("NOT_EMAIL") ) ``` ```go var ( // ErrNotFQDN indicates that the given value is not a valid FQDN. ErrNotFQDN = NewCheckError("FQDN") ) ``` ```go var ( // ErrNotHex indicates that the given string contains hex characters. ErrNotHex = NewCheckError("NOT_HEX") ) ``` ```go var ( // ErrNotIP indicates that the given value is not a valid IP address. ErrNotIP = NewCheckError("NOT_IP") ) ``` ```go var ( // ErrNotIPv4 indicates that the given value is not a valid IPv4 address. ErrNotIPv4 = NewCheckError("NOT_IPV4") ) ``` ```go var ( // ErrNotIPv6 indicates that the given value is not a valid IPv6 address. ErrNotIPv6 = NewCheckError("NOT_IPV6") ) ``` ```go var ( // ErrNotISBN indicates that the given value is not a valid ISBN. ErrNotISBN = NewCheckError("NOT_ISBN") ) ``` ```go var ( // ErrNotLUHN indicates that the given value is not a valid LUHN number. ErrNotLUHN = NewCheckError("NOT_LUHN") ) ``` ```go var ( // ErrNotMAC indicates that the given value is not a valid MAC address. ErrNotMAC = NewCheckError("NOT_MAC") ) ``` ErrNotMatch indicates that the given string does not match the regexp pattern. ```go var ErrNotMatch = NewCheckError("REGEXP") ``` ```go var ( // ErrNotURL indicates that the given value is not a valid URL. ErrNotURL = NewCheckError("NOT_URL") ) ``` ```go var ( // ErrRequired indicates that a required value was missing. ErrRequired = NewCheckError("REQUIRED") ) ``` ```go var ( // ErrTime indicates that the value is not a valid based on the given time format. ErrTime = NewCheckError("NOT_TIME") ) ``` ## func [Check]() ```go func Check[T any](value T, checks ...CheckFunc[T]) (T, error) ``` Check applies the given check functions to a value sequentially. It returns the final value and the first encountered error, if any.
Example

```go 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]() ```go func CheckStruct(st any) (map[string]error, bool) ``` 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

```go 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]() ```go func CheckWithConfig[T any](value T, config string) (T, error) ``` 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]() ```go func HTMLEscape(value string) (string, error) ``` HTMLEscape applies HTML escaping to special characters. ## func [HTMLUnescape]() ```go func HTMLUnescape(value string) (string, error) ``` HTMLUnescape applies HTML unescaping to special characters. ## func [IsASCII]() ```go func IsASCII(value string) (string, error) ``` IsASCII checks if the given string consists of only ASCII characters.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsASCII("Checker") if err != nil { fmt.Println(err) } } ```

## func [IsAlphanumeric]() ```go func IsAlphanumeric(value string) (string, error) ``` IsAlphanumeric checks if the given string consists of only alphanumeric characters.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsAlphanumeric("ABcd1234") if err != nil { fmt.Println(err) } } ```

## func [IsAmexCreditCard]() ```go func IsAmexCreditCard(number string) (string, error) ``` IsAmexCreditCard checks if the given valie is a valid AMEX credit card.
Example

```go 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 } } ```

## func [IsAnyCreditCard]() ```go func IsAnyCreditCard(number string) (string, error) ``` IsAnyCreditCard checks if the given value is a valid credit card number.
Example

```go 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 } } ```

## func [IsCIDR]() ```go func IsCIDR(value string) (string, error) ``` IsCIDR checks if the value is a valid CIDR notation IP address and prefix length.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsCIDR("2001:db8::/32") if err != nil { fmt.Println(err) } } ```

## func [IsDigits]() ```go func IsDigits(value string) (string, error) ``` IsDigits checks if the value contains only digit characters.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsDigits("123456") if err != nil { fmt.Println(err) } } ```

## func [IsDinersCreditCard]() ```go func IsDinersCreditCard(number string) (string, error) ``` IsDinersCreditCard checks if the given valie is a valid Diners credit card.
Example

```go 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 } } ```

## func [IsDiscoverCreditCard]() ```go func IsDiscoverCreditCard(number string) (string, error) ``` IsDiscoverCreditCard checks if the given valie is a valid Discover credit card.
Example

```go 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 } } ```

## func [IsEmail]() ```go func IsEmail(value string) (string, error) ``` IsEmail checks if the value is a valid email address.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsEmail("test@example.com") if err != nil { fmt.Println(err) } } ```

## func [IsFQDN]() ```go func IsFQDN(value string) (string, error) ``` IsFQDN checks if the value is a valid fully qualified domain name \(FQDN\).
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsFQDN("example.com") if err != nil { fmt.Println(err) } } ```

## func [IsGte]() ```go func IsGte[T cmp.Ordered](value, n T) (T, error) ``` IsGte checks if the value is greater than or equal to the given value. ## func [IsHex]() ```go func IsHex(value string) (string, error) ``` IsHex checks if the given string consists of only hex characters.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsHex("0123456789abcdefABCDEF") if err != nil { fmt.Println(err) } } ```

## func [IsIP]() ```go func IsIP(value string) (string, error) ``` IsIP checks if the value is a valid IP address.
Example

```go 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) } } ```

## func [IsIPv4]() ```go func IsIPv4(value string) (string, error) ``` IsIPv4 checks if the value is a valid IPv4 address.
Example

```go 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) } } ```

## func [IsIPv6]() ```go func IsIPv6(value string) (string, error) ``` IsIPv6 checks if the value is a valid IPv6 address.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsIPv6("2001:db8::1") if err != nil { fmt.Println(err) } } ```

## func [IsISBN]() ```go func IsISBN(value string) (string, error) ``` IsISBN checks if the value is a valid ISBN\-10 or ISBN\-13.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsISBN("1430248270") if err != nil { fmt.Println(err) } } ```

## func [IsJcbCreditCard]() ```go func IsJcbCreditCard(number string) (string, error) ``` IsJcbCreditCard checks if the given valie is a valid JCB 15 credit card.
Example

```go 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 } } ```

## func [IsLUHN]() ```go func IsLUHN(value string) (string, error) ``` IsLUHN checks if the value is a valid LUHN number.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsLUHN("4012888888881881") if err != nil { fmt.Println(err) } } ```

## func [IsLte]() ```go func IsLte[T cmp.Ordered](value, n T) (T, error) ``` IsLte checks if the value is less than or equal to the given value. ## func [IsMAC]() ```go func IsMAC(value string) (string, error) ``` IsMAC checks if the value is a valid MAC address.
Example

```go 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) } } ```

## func [IsMasterCardCreditCard]() ```go func IsMasterCardCreditCard(number string) (string, error) ``` IsMasterCardCreditCard checks if the given valie is a valid MasterCard credit card.
Example

```go 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 } } ```

## func [IsRegexp]() ```go func IsRegexp(expression, value string) (string, error) ``` IsRegexp checks if the given string matches the given regexp expression.
Example

```go 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) } } ```

## func [IsTime]() ```go func IsTime(params, value string) (string, error) ``` IsTime checks if the given value is a valid time based on the given layout.
Example

```go package main import ( v2 "github.com/cinar/checker/v2" ) func main() { value := "2024-12-31" _, err := v2.IsTime("DateOnly", value) if err != nil { panic(err) } } ```

Example (Custom)

```go package main import ( v2 "github.com/cinar/checker/v2" ) func main() { rfc3339Layout := "2006-01-02T15:04:05Z07:00" value := "2024-12-31T10:20:00Z07:00" _, err := v2.IsTime(rfc3339Layout, value) if err != nil { panic(err) } } ```

## func [IsURL]() ```go func IsURL(value string) (string, error) ``` IsURL checks if the value is a valid URL.
Example

```go package main import ( "fmt" v2 "github.com/cinar/checker/v2" ) func main() { _, err := v2.IsURL("https://example.com") if err != nil { fmt.Println(err) } } ```

## func [IsUnionPayCreditCard]() ```go func IsUnionPayCreditCard(number string) (string, error) ``` IsUnionPayCreditCard checks if the given valie is a valid UnionPay credit card.
Example

```go 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 } } ```

## func [IsVisaCreditCard]() ```go func IsVisaCreditCard(number string) (string, error) ``` IsVisaCreditCard checks if the given valie is a valid Visa credit card.
Example

```go 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 } } ```

## func [Lower]() ```go func Lower(value string) (string, error) ``` Lower maps all Unicode letters in the given value to their lower case. ## func [ReflectCheckWithConfig]() ```go func ReflectCheckWithConfig(value reflect.Value, config string) (reflect.Value, error) ``` 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]() ```go func RegisterLocale(locale string, messages map[string]string) ``` RegisterLocale registers the localized error messages for the given locale. ## func [RegisterMaker]() ```go func RegisterMaker(name string, maker MakeCheckFunc) ``` RegisterMaker registers a new maker function with the given name.
Example

```go 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) } } ```

## func [Required]() ```go func Required[T any](value T) (T, error) ``` Required checks if the given value of type T is its zero value. It returns an error if the value is zero. ## func [Title]() ```go func Title(value string) (string, error) ``` Title returns the value of the string with the first letter of each word in upper case. ## func [TrimLeft]() ```go func TrimLeft(value string) (string, error) ``` TrimLeft returns the value of the string with whitespace removed from the beginning. ## func [TrimRight]() ```go func TrimRight(value string) (string, error) ``` TrimRight returns the value of the string with whitespace removed from the end. ## func [TrimSpace]() ```go func TrimSpace(value string) (string, error) ``` TrimSpace returns the value of the string with whitespace removed from both ends. ## func [URLEscape]() ```go func URLEscape(value string) (string, error) ``` URLEscape applies URL escaping to special characters. ## func [URLUnescape]() ```go func URLUnescape(value string) (string, error) ``` URLUnescape applies URL unescaping to special characters. ## func [Upper]() ```go func Upper(value string) (string, error) ``` Upper maps all Unicode letters in the given value to their upper case. ## type [CheckError]() CheckError defines the check error. ```go type CheckError struct { // Code is the error code. Code string // data is the error data. Data map[string]interface{} } ``` ### func [NewCheckError]() ```go func NewCheckError(code string) *CheckError ``` NewCheckError creates a new check error with the given code. ### func [NewCheckErrorWithData]() ```go func NewCheckErrorWithData(code string, data map[string]interface{}) *CheckError ``` NewCheckErrorWithData creates a new check error with the given code and data. ### func \(\*CheckError\) [Error]() ```go func (c *CheckError) Error() string ``` Error returns the error message for the check. ### func \(\*CheckError\) [ErrorWithLocale]() ```go func (c *CheckError) ErrorWithLocale(locale string) string ``` ErrorWithLocale returns the localized error message for the check with the given locale. ### func \(\*CheckError\) [Is]() ```go 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. ```go type CheckFunc[T any] func(value T) (T, error) ``` ### func [MakeRegexpChecker]() ```go func MakeRegexpChecker(expression string, invalidError error) CheckFunc[reflect.Value] ``` MakeRegexpChecker makes a regexp checker for the given regexp expression with the given invalid result. ### func [MaxLen]() ```go func MaxLen[T any](n int) CheckFunc[T] ``` MaxLen checks if the length of the given value \(string, slice, or map\) is at most n. Returns an error if the length is greater than n. ### func [MinLen]() ```go func MinLen[T any](n int) CheckFunc[T] ``` MinLen checks if the length of the given value \(string, slice, or map\) is at least n. Returns an error if the length is less than n. ## type [MakeCheckFunc]() MakeCheckFunc is a function that returns a check function using the given params. ```go type MakeCheckFunc func(params string) CheckFunc[reflect.Value] ``` Generated by [gomarkdoc]()