Documentation ¶
Overview ¶
Package checker is a Go library for validating user input through struct tags.
Copyright (c) 2023-2024 Onur Cinar. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file. https://github.com/cinar/checker
Index ¶
- Variables
- func FailIfNoPanic(t *testing.T)
- func IsASCII(value string) error
- func IsAlphanumeric(value string) error
- func IsAmexCreditCard(number string) error
- func IsAnyCreditCard(number string) error
- func IsCidr(value string) error
- func IsDigits(value string) error
- func IsDinersCreditCard(number string) error
- func IsDiscoverCreditCard(number string) error
- func IsEmail(email string) error
- func IsFqdn(domain string) error
- func IsIP(value string) error
- func IsIPV4(value string) error
- func IsIPV6(value string) error
- func IsISBN(value string) error
- func IsISBN10(value string) error
- func IsISBN13(value string) error
- func IsJcbCreditCard(number string) error
- func IsLuhn(number string) error
- func IsMac(value string) error
- func IsMasterCardCreditCard(number string) error
- func IsMax(value interface{}, max float64) error
- func IsMaxLength(value interface{}, maxLength int) error
- func IsMin(value interface{}, min float64) error
- func IsMinLength(value interface{}, minLength int) error
- func IsRequired(v interface{}) error
- func IsURL(value string) error
- func IsUnionPayCreditCard(number string) error
- func IsVisaCreditCard(number string) error
- func Register(name string, maker MakeFunc)
- type CheckFunc
- type Errors
- type MakeFunc
Examples ¶
- IsASCII
- IsAlphanumeric
- IsAmexCreditCard
- IsAnyCreditCard
- IsCidr
- IsDigits
- IsDinersCreditCard
- IsDiscoverCreditCard
- IsEmail
- IsFqdn
- IsIP
- IsIPV4
- IsIPV6
- IsISBN
- IsISBN10
- IsISBN13
- IsJcbCreditCard
- IsLuhn
- IsMac
- IsMasterCardCreditCard
- IsMax
- IsMaxLength
- IsMin
- IsMinLength
- IsRequired
- IsURL
- IsUnionPayCreditCard
- IsVisaCreditCard
Constants ¶
This section is empty.
Variables ¶
var ErrNotASCII = errors.New("please use standard English characters only")
ErrNotASCII indicates that the given string contains non-ASCII characters.
var ErrNotAlphanumeric = errors.New("please use only letters and numbers")
ErrNotAlphanumeric indicates that the given string contains non-alphanumeric characters.
var ErrNotCidr = errors.New("please enter a valid CIDR")
ErrNotCidr indicates that the given value is not a valid CIDR.
var ErrNotCreditCard = errors.New("please enter a valid credit card number")
ErrNotCreditCard indicates that the given value is not a valid credit card number.
var ErrNotDigits = errors.New("please enter a valid number")
ErrNotDigits indicates that the given string contains non-digit characters.
var ErrNotEmail = errors.New("please enter a valid email address")
ErrNotEmail indicates that the given string is not a valid email.
var ErrNotFqdn = errors.New("please enter a valid domain name")
ErrNotFqdn indicates that the given string is not a valid FQDN.
var ErrNotIP = errors.New("please enter a valid IP address")
ErrNotIP indicates that the given value is not an IP address.
var ErrNotIPV4 = errors.New("please enter a valid IPv4 address")
ErrNotIPV4 indicates that the given value is not an IPv4 address.
var ErrNotIPV6 = errors.New("please enter a valid IPv6 address")
ErrNotIPV6 indicates that the given value is not an IPv6 address.
var ErrNotISBN = errors.New("please enter a valid ISBN number")
ErrNotISBN indicates that the given value is not a valid ISBN.
var ErrNotLuhn = errors.New("please enter a valid LUHN")
ErrNotLuhn indicates that the given number is not valid based on the Luhn algorithm.
var ErrNotMac = errors.New("please enter a valid MAC address")
ErrNotMac indicates that the given value is not an MAC address.
var ErrNotMatch = errors.New("please enter a valid input")
ErrNotMatch indicates that the given string does not match the regexp pattern.
var ErrNotSame = errors.New("does not match the other")
ErrNotSame indicates that the given two values are not equal to each other.
var ErrNotURL = errors.New("please enter a valid URL")
ErrNotURL indicates that the given value is not a valid URL.
var ErrRequired = errors.New("is required")
ErrRequired indicates that the required value is missing.
Functions ¶
func FailIfNoPanic ¶
FailIfNoPanic fails if test didn't panic. Use this function with the defer.
func IsASCII ¶
IsASCII checks if the given string consists of only ASCII characters.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsASCII("Checker") if err != nil { // Send the errors back to the user } }
Output:
func IsAlphanumeric ¶
IsAlphanumeric checks if the given string consists of only alphanumeric characters.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsAlphanumeric("ABcd1234") if err != nil { // Send the errors back to the user } }
Output:
func IsAmexCreditCard ¶
IsAmexCreditCard checks if the given valie is a valid AMEX credit card.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.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 ( "github.com/cinar/checker" ) func main() { err := checker.IsAnyCreditCard("6011111111111117") if err != nil { // Send the errors back to the user } }
Output:
func IsCidr ¶
IsCidr checker checks if the value is a valid CIDR notation IP address and prefix length.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsCidr("2001:db8::/32") if err != nil { // Send the errors back to the user } }
Output:
func IsDigits ¶
IsDigits checks if the given string consists of only digit characters.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsDigits("1234") if err != nil { // Send the errors back to the user } }
Output:
func IsDinersCreditCard ¶
IsDinersCreditCard checks if the given valie is a valid Diners credit card.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsDinersCreditCard("36227206271667") if err != nil { // Send the errors back to the user } }
Output:
func IsDiscoverCreditCard ¶ added in v1.2.0
IsDiscoverCreditCard checks if the given valie is a valid Discover credit card.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsDiscoverCreditCard("6011111111111117") if err != nil { // Send the errors back to the user } }
Output:
func IsEmail ¶
IsEmail checks if the given string is an email address.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsEmail("user@zdo.com") if err != nil { // Send the errors back to the user } }
Output:
func IsFqdn ¶
IsFqdn checks if the given string is a fully qualified domain name.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsFqdn("zdo.com") if err != nil { // Send the errors back to the user } }
Output:
func IsIP ¶
IsIP checks if the given value is an IP address.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsIP("2001:db8::68") if err != nil { // Send the errors back to the user } }
Output:
func IsIPV4 ¶
IsIPV4 checks if the given value is an IPv4 address.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsIPV4("192.168.1.1") if err != nil { // Send the errors back to the user } }
Output:
func IsIPV6 ¶
IsIPV6 checks if the given value is an IPv6 address.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsIPV6("2001:db8::68") if err != nil { // Send the errors back to the user } }
Output:
func IsISBN ¶ added in v1.1.0
IsISBN checks if the given value is a valid ISBN number.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsISBN("1430248270") if err != nil { // Send the errors back to the user } }
Output:
func IsISBN10 ¶ added in v1.1.0
IsISBN10 checks if the given value is a valid ISBN-10 number.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsISBN10("1430248270") if err != nil { // Send the errors back to the user } }
Output:
func IsISBN13 ¶ added in v1.1.0
IsISBN13 checks if the given value is a valid ISBN-13 number.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsISBN13("9781430248279") if err != nil { // Send the errors back to the user } }
Output:
func IsJcbCreditCard ¶
IsJcbCreditCard checks if the given valie is a valid JCB 15 credit card.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsJcbCreditCard("3530111333300000") if err != nil { // Send the errors back to the user } }
Output:
func IsLuhn ¶
IsLuhn checks if the given number is valid based on the Luhn algorithm.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsLuhn("4012888888881881") if err != nil { // Send the errors back to the user } }
Output:
func IsMac ¶
IsMac checks if the given value is a valid an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsMac("00:00:5e:00:53:01") if err != nil { // Send the errors back to the user } }
Output:
func IsMasterCardCreditCard ¶
IsMasterCardCreditCard checks if the given valie is a valid MasterCard credit card.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsMasterCardCreditCard("5555555555554444") if err != nil { // Send the errors back to the user } }
Output:
func IsMax ¶
IsMax checks if the given value is below than the given maximum.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { quantity := 5 err := checker.IsMax(quantity, 10) if err != nil { // Send the errors back to the user } }
Output:
func IsMaxLength ¶
IsMaxLength checks if the length of the given value is less than the given maximum length.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { s := "1234" err := checker.IsMaxLength(s, 4) if err != nil { // Send the errors back to the user } }
Output:
func IsMin ¶
IsMin checks if the given value is above than the given minimum.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { age := 45 err := checker.IsMin(age, 21) if err != nil { // Send the errors back to the user } }
Output:
func IsMinLength ¶
IsMinLength checks if the length of the given value is greather than the given minimum length.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { s := "1234" err := checker.IsMinLength(s, 4) if err != nil { // Send the errors back to the user } }
Output:
func IsRequired ¶
func IsRequired(v interface{}) error
IsRequired checks if the given required value is present.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { var name string err := checker.IsRequired(name) if err != nil { // Send the err back to the user } }
Output:
func IsURL ¶ added in v1.1.0
IsURL checks if the given value is a valid URL.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.IsURL("https://zdo.com") if err != nil { // Send the errors back to the user } }
Output:
func IsUnionPayCreditCard ¶
IsUnionPayCreditCard checks if the given valie is a valid UnionPay credit card.
Example ¶
package main import ( "github.com/cinar/checker" ) func main() { err := checker.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 ( "github.com/cinar/checker" ) func main() { err := checker.IsVisaCreditCard("4111111111111111") if err != nil { // Send the errors back to the user } }
Output:
Types ¶
type CheckFunc ¶
CheckFunc defines the signature for the checker functions.
func MakeRegexpChecker ¶
MakeRegexpChecker makes a regexp checker for the given regexp expression with the given invalid result.
type Errors ¶ added in v1.4.0
Errors provides a mapping of the checker errors keyed by the field names.
type MakeFunc ¶
MakeFunc defines the signature for the checker maker functions.
func MakeRegexpMaker ¶
MakeRegexpMaker makes a regexp checker maker for the given regexp expression with the given invalid result.
Source Files ¶
- alphanumeric.go
- ascii.go
- checker.go
- cidr.go
- credit_card.go
- digits.go
- email.go
- fqdn.go
- html_escape.go
- html_unescape.go
- ip.go
- ipv4.go
- ipv6.go
- isbn.go
- lower.go
- luhn.go
- mac.go
- max.go
- maxlength.go
- min.go
- minlenght.go
- regexp.go
- required.go
- same.go
- test_helper.go
- title.go
- trim.go
- trim_left.go
- trim_right.go
- upper.go
- url.go
- url_escape.go
- url_unescape.go