Skip to content

Latest commit

 

History

History
1523 lines (1061 loc) · 29 KB

DOC.md

File metadata and controls

1523 lines (1061 loc) · 29 KB

v2

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

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")
)

ErrNotMatch indicates that the given string does not match the regexp pattern.

var ErrNotMatch = NewCheckError("REGEXP")

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")
)

var (
    // ErrTime indicates that the value is not a valid based on the given time format.
    ErrTime = NewCheckError("NOT_TIME")
)

func Check

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

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(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

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[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(value string) (string, error)

HTMLEscape applies HTML escaping to special characters.

func HTMLUnescape(value string) (string, error)

HTMLUnescape applies HTML unescaping to special characters.

func IsASCII

func IsASCII(value string) (string, error)

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

func IsAlphanumeric(value string) (string, error)

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

func IsAmexCreditCard(number string) (string, error)

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

func IsAnyCreditCard(number string) (string, error)

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

func IsCIDR

func IsCIDR(value string) (string, error)

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

func IsDigits(value string) (string, error)

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

func IsDinersCreditCard(number string) (string, error)

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

func IsDiscoverCreditCard(number string) (string, error)

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

func IsEmail

func IsEmail(value string) (string, error)

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

func IsFQDN

func IsFQDN(value string) (string, error)

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

func IsGte

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

func IsHex(value string) (string, error)

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

func IsIP

func IsIP(value string) (string, error)

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

func IsIPv4

func IsIPv4(value string) (string, error)

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

func IsIPv6

func IsIPv6(value string) (string, error)

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

func IsISBN

func IsISBN(value string) (string, error)

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

func IsJcbCreditCard(number string) (string, error)

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

func IsLUHN

func IsLUHN(value string) (string, error)

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

func IsLte

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

func IsMAC(value string) (string, error)

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

func IsMasterCardCreditCard(number string) (string, error)

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

func IsRegexp(expression, value string) (string, error)

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

func IsTime

func IsTime(params, value string) (string, error)

IsTime checks if the given value is a valid time based on the given layout.

Example

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)

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

func IsURL(value string) (string, error)

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

func IsUnionPayCreditCard(number string) (string, error)

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

func IsVisaCreditCard(number string) (string, error)

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

func Lower

func Lower(value string) (string, error)

Lower maps all Unicode letters in the given value to their lower case.

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(locale string, messages map[string]string)

RegisterLocale registers the localized error messages for the given locale.

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

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

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(value string) (string, error)

TrimLeft returns the value of the string with whitespace removed from the beginning.

func TrimRight(value string) (string, error)

TrimRight returns the value of the string with whitespace removed from the end.

func TrimSpace(value string) (string, error)

TrimSpace returns the value of the string with whitespace removed from both ends.

func URLEscape(value string) (string, error)

URLEscape applies URL escaping to special characters.

func URLUnescape(value string) (string, error)

URLUnescape applies URL unescaping to special characters.

func Upper

func Upper(value string) (string, error)

Upper maps all Unicode letters in the given value to their upper case.

CheckError defines the check error.

type CheckError struct {
    // Code is the error code.
    Code string

    // data is the error data.
    Data map[string]interface{}
}

func NewCheckError(code string) *CheckError

NewCheckError creates a new check error with the given code.

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.

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.

type CheckFunc[T any] func(value T) (T, error)

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

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

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.

MakeCheckFunc is a function that returns a check function using the given params.

type MakeCheckFunc func(params string) CheckFunc[reflect.Value]

Generated by gomarkdoc