diff --git a/.gomarkdoc.yml b/.gomarkdoc.yml new file mode 100644 index 0000000..86a76b1 --- /dev/null +++ b/.gomarkdoc.yml @@ -0,0 +1,3 @@ +output: "{{.Dir}}/README.md" +repository: + url: https://github.com/cinar/checker diff --git a/README.md b/README.md index 6f209e4..d2e4aeb 100644 --- a/README.md +++ b/README.md @@ -72,42 +72,42 @@ type Person struct { This package currently provides the following checkers: -- [alphanumeric](doc/checkers/alphanumeric.md) checks if the given string consists of only alphanumeric characters. -- [ascii](doc/checkers/ascii.md) checks if the given string consists of only ASCII characters. -- [cidr](doc/checkers/cidr.md) checker checks if the value is a valid CIDR notation IP address and prefix length. -- [credit-card](doc/checkers/credit_card.md) checks if the given value is a valid credit card number. -- [digits](doc/checkers/digits.md) checks if the given string consists of only digit characters. -- [email](doc/checkers/email.md) checks if the given string is an email address. -- [fqdn](doc/checkers/fqdn.md) checks if the given string is a fully qualified domain name. -- [ip](doc/checkers/ip.md) checks if the given value is an IP address. -- [ipv4](doc/checkers/ipv4.md) checks if the given value is an IPv4 address. -- [ipv6](doc/checkers/ipv6.md) checks if the given value is an IPv6 address. -- [isbn](doc/checkers/isbn.md) checks if the given value is a valid ISBN number. -- [luhn](doc/checkers/luhn.md) checks if the given number is valid based on the Luhn algorithm. -- [mac](doc/checkers/mac.md) 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. -- [max](doc/checkers/max.md) checks if the given value is less than the given maximum. -- [max-length](doc/checkers/maxlength.md) checks if the length of the given value is less than the given maximum length. -- [min](doc/checkers/min.md) checks if the given value is greather than the given minimum. -- [min-length](doc/checkers/minlength.md) checks if the length of the given value is greather than the given minimum length. -- [regexp](doc/checkers/regexp.md) checks if the given string matches the regexp pattern. -- [required](doc/checkers/required.md) checks if the required value is provided. -- [same](doc/checkers/same.md) checks if the given value is equal to the value of the field with the given name. -- [url](doc/checkers/url.md) checks if the given value is a valid URL. +- `alphanumeric` checks if the given string consists of only alphanumeric characters. +- `ascii` checks if the given string consists of only ASCII characters. +- `cidr` checker checks if the value is a valid CIDR notation IP address and prefix length. +- `credit-card` checks if the given value is a valid credit card number. +- `digits` checks if the given string consists of only digit characters. +- `email` checks if the given string is an email address. +- `fqdn` checks if the given string is a fully qualified domain name. +- `ip` checks if the given value is an IP address. +- `ipv4` checks if the given value is an IPv4 address. +- `ipv6` checks if the given value is an IPv6 address. +- `isbn` checks if the given value is a valid ISBN number. +- `luhn` checks if the given number is valid based on the Luhn algorithm. +- `mac` 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. +- `max` checks if the given value is less than the given maximum. +- `max-length` checks if the length of the given value is less than the given maximum length. +- `min` checks if the given value is greather than the given minimum. +- `min-length` checks if the length of the given value is greather than the given minimum length. +- `regexp` checks if the given string matches the regexp pattern. +- `required` checks if the required value is provided. +- `same` checks if the given value is equal to the value of the field with the given name. +- `url` checks if the given value is a valid URL. # Normalizers Provided This package currently provides the following normalizers. They can be mixed with the checkers when defining the validation steps for user data. -- [html-escape](doc/normalizers/html_escape.md) applies HTML escaping to special characters. -- [html-unescape](doc/normalizers/html_unescape.md) applies HTML unescaping to special characters. -- [lower](doc/normalizers/lower.md) maps all Unicode letters in the given value to their lower case. -- [upper](doc/normalizers/upper.md) maps all Unicode letters in the given value to their upper case. -- [title](doc/normalizers/title.md) maps the first letter of each word to their upper case. -- [trim](doc/normalizers/trim.md) removes the whitespaces at the beginning and at the end of the given value. -- [trim-left](doc/normalizers/trim_left.md) removes the whitespaces at the beginning of the given value. -- [trim-right](doc/normalizers/trim_right.md) removes the whitespaces at the end of the given value. -- [url-escape](doc/normalizers/url_escape.md) applies URL escaping to special characters. -- [url-unescape](doc/normalizers/url_unescape.md) applies URL unescaping to special characters. +- `html-escape` applies HTML escaping to special characters. +- `html-unescape` applies HTML unescaping to special characters. +- `lower` maps all Unicode letters in the given value to their lower case. +- `upper` maps all Unicode letters in the given value to their upper case. +- `title` maps the first letter of each word to their upper case. +- `trim` removes the whitespaces at the beginning and at the end of the given value. +- `trim-left` removes the whitespaces at the beginning of the given value. +- `trim-right` removes the whitespaces at the end of the given value. +- `url-escape` applies URL escaping to special characters. +- `url-unescape` applies URL unescaping to special characters. # Custom Checkers @@ -141,6 +141,1157 @@ type User struct { } ``` + + + + +# checker + +```go +import "github.com/cinar/checker" +``` + +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](<#variables>) +- [func FailIfNoPanic\(t \*testing.T\)](<#FailIfNoPanic>) +- [func IsASCII\(value string\) error](<#IsASCII>) +- [func IsAlphanumeric\(value string\) error](<#IsAlphanumeric>) +- [func IsAmexCreditCard\(number string\) error](<#IsAmexCreditCard>) +- [func IsAnyCreditCard\(number string\) error](<#IsAnyCreditCard>) +- [func IsCidr\(value string\) error](<#IsCidr>) +- [func IsDigits\(value string\) error](<#IsDigits>) +- [func IsDinersCreditCard\(number string\) error](<#IsDinersCreditCard>) +- [func IsDiscoverCreditCard\(number string\) error](<#IsDiscoverCreditCard>) +- [func IsEmail\(email string\) error](<#IsEmail>) +- [func IsFqdn\(domain string\) error](<#IsFqdn>) +- [func IsIP\(value string\) error](<#IsIP>) +- [func IsIPV4\(value string\) error](<#IsIPV4>) +- [func IsIPV6\(value string\) error](<#IsIPV6>) +- [func IsISBN\(value string\) error](<#IsISBN>) +- [func IsISBN10\(value string\) error](<#IsISBN10>) +- [func IsISBN13\(value string\) error](<#IsISBN13>) +- [func IsJcbCreditCard\(number string\) error](<#IsJcbCreditCard>) +- [func IsLuhn\(number string\) error](<#IsLuhn>) +- [func IsMac\(value string\) error](<#IsMac>) +- [func IsMasterCardCreditCard\(number string\) error](<#IsMasterCardCreditCard>) +- [func IsMax\(value interface\{\}, max float64\) error](<#IsMax>) +- [func IsMaxLength\(value interface\{\}, maxLength int\) error](<#IsMaxLength>) +- [func IsMin\(value interface\{\}, min float64\) error](<#IsMin>) +- [func IsMinLength\(value interface\{\}, minLength int\) error](<#IsMinLength>) +- [func IsRequired\(v interface\{\}\) error](<#IsRequired>) +- [func IsURL\(value string\) error](<#IsURL>) +- [func IsUnionPayCreditCard\(number string\) error](<#IsUnionPayCreditCard>) +- [func IsVisaCreditCard\(number string\) error](<#IsVisaCreditCard>) +- [func Register\(name string, maker MakeFunc\)](<#Register>) +- [type CheckFunc](<#CheckFunc>) + - [func MakeRegexpChecker\(expression string, invalidError error\) CheckFunc](<#MakeRegexpChecker>) +- [type Errors](<#Errors>) + - [func Check\(s interface\{\}\) \(Errors, bool\)](<#Check>) +- [type MakeFunc](<#MakeFunc>) + - [func MakeRegexpMaker\(expression string, invalidError error\) MakeFunc](<#MakeRegexpMaker>) + + +## Variables + +ErrNotASCII indicates that the given string contains non\-ASCII characters. + +```go +var ErrNotASCII = errors.New("please use standard English characters only") +``` + +ErrNotAlphanumeric indicates that the given string contains non\-alphanumeric characters. + +```go +var ErrNotAlphanumeric = errors.New("please use only letters and numbers") +``` + +ErrNotCidr indicates that the given value is not a valid CIDR. + +```go +var ErrNotCidr = errors.New("please enter a valid CIDR") +``` + +ErrNotCreditCard indicates that the given value is not a valid credit card number. + +```go +var ErrNotCreditCard = errors.New("please enter a valid credit card number") +``` + +ErrNotDigits indicates that the given string contains non\-digit characters. + +```go +var ErrNotDigits = errors.New("please enter a valid number") +``` + +ErrNotEmail indicates that the given string is not a valid email. + +```go +var ErrNotEmail = errors.New("please enter a valid email address") +``` + +ErrNotFqdn indicates that the given string is not a valid FQDN. + +```go +var ErrNotFqdn = errors.New("please enter a valid domain name") +``` + +ErrNotIP indicates that the given value is not an IP address. + +```go +var ErrNotIP = errors.New("please enter a valid IP address") +``` + +ErrNotIPV4 indicates that the given value is not an IPv4 address. + +```go +var ErrNotIPV4 = errors.New("please enter a valid IPv4 address") +``` + +ErrNotIPV6 indicates that the given value is not an IPv6 address. + +```go +var ErrNotIPV6 = errors.New("please enter a valid IPv6 address") +``` + +ErrNotISBN indicates that the given value is not a valid ISBN. + +```go +var ErrNotISBN = errors.New("please enter a valid ISBN number") +``` + +ErrNotLuhn indicates that the given number is not valid based on the Luhn algorithm. + +```go +var ErrNotLuhn = errors.New("please enter a valid LUHN") +``` + +ErrNotMac indicates that the given value is not an MAC address. + +```go +var ErrNotMac = errors.New("please enter a valid MAC address") +``` + +ErrNotMatch indicates that the given string does not match the regexp pattern. + +```go +var ErrNotMatch = errors.New("please enter a valid input") +``` + +ErrNotSame indicates that the given two values are not equal to each other. + +```go +var ErrNotSame = errors.New("does not match the other") +``` + +ErrNotURL indicates that the given value is not a valid URL. + +```go +var ErrNotURL = errors.New("please enter a valid URL") +``` + +ErrRequired indicates that the required value is missing. + +```go +var ErrRequired = errors.New("is required") +``` + + +## func [FailIfNoPanic]() + +```go +func FailIfNoPanic(t *testing.T) +``` + +FailIfNoPanic fails if test didn't panic. Use this function with the defer. + + +## func [IsASCII]() + +```go +func IsASCII(value string) error +``` + +IsASCII checks if the given string consists of only ASCII characters. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsASCII("Checker") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsAlphanumeric("ABcd1234") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsAmexCreditCard("378282246310005") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsAnyCreditCard("6011111111111117") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsCidr("2001:db8::/32") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsDigits]() + +```go +func IsDigits(value string) error +``` + +IsDigits checks if the given string consists of only digit characters. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsDigits("1234") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsDinersCreditCard("36227206271667") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsDiscoverCreditCard("6011111111111117") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsEmail]() + +```go +func IsEmail(email string) error +``` + +IsEmail checks if the given string is an email address. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsEmail("user@zdo.com") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsFqdn]() + +```go +func IsFqdn(domain string) error +``` + +IsFqdn checks if the given string is a fully qualified domain name. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsFqdn("zdo.com") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsIP]() + +```go +func IsIP(value string) error +``` + +IsIP checks if the given value is an IP address. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsIP("2001:db8::68") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsIPV4]() + +```go +func IsIPV4(value string) error +``` + +IsIPV4 checks if the given value is an IPv4 address. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsIPV4("192.168.1.1") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsIPV6]() + +```go +func IsIPV6(value string) error +``` + +IsIPV6 checks if the given value is an IPv6 address. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsIPV6("2001:db8::68") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsISBN]() + +```go +func IsISBN(value string) error +``` + +IsISBN checks if the given value is a valid ISBN number. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsISBN("1430248270") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsISBN10]() + +```go +func IsISBN10(value string) error +``` + +IsISBN10 checks if the given value is a valid ISBN\-10 number. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsISBN10("1430248270") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsISBN13]() + +```go +func IsISBN13(value string) error +``` + +IsISBN13 checks if the given value is a valid ISBN\-13 number. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsISBN13("9781430248279") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsJcbCreditCard("3530111333300000") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsLuhn]() + +```go +func IsLuhn(number string) error +``` + +IsLuhn checks if the given number is valid based on the Luhn algorithm. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsLuhn("4012888888881881") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsMac]() + +```go +func IsMac(value string) error +``` + +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 +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsMac("00:00:5e:00:53:01") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsMasterCardCreditCard("5555555555554444") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsMax]() + +```go +func IsMax(value interface{}, max float64) error +``` + +IsMax checks if the given value is below than the given maximum. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + quantity := 5 + + err := checker.IsMax(quantity, 10) + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsMaxLength]() + +```go +func IsMaxLength(value interface{}, maxLength int) error +``` + +IsMaxLength checks if the length of the given value is less than the given maximum length. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + s := "1234" + + err := checker.IsMaxLength(s, 4) + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsMin]() + +```go +func IsMin(value interface{}, min float64) error +``` + +IsMin checks if the given value is above than the given minimum. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + age := 45 + + err := checker.IsMin(age, 21) + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsMinLength]() + +```go +func IsMinLength(value interface{}, minLength int) error +``` + +IsMinLength checks if the length of the given value is greather than the given minimum length. + +
Example +

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + s := "1234" + + err := checker.IsMinLength(s, 4) + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [IsRequired]() + +```go +func IsRequired(v interface{}) error +``` + +IsRequired checks if the given required value is present. + +
Example +

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

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsURL("https://zdo.com") + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsUnionPayCreditCard("6200000000000005") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

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

+ + + +```go +package main + +import ( + "github.com/cinar/checker" +) + +func main() { + err := checker.IsVisaCreditCard("4111111111111111") + + if err != nil { + // Send the mistakes back to the user + } +} +``` + +

+
+ + +## func [Register]() + +```go +func Register(name string, maker MakeFunc) +``` + +Register registers the given checker name and the maker function. + + +## type [CheckFunc]() + +CheckFunc defines the signature for the checker functions. + +```go +type CheckFunc func(value, parent reflect.Value) error +``` + + +### func [MakeRegexpChecker]() + +```go +func MakeRegexpChecker(expression string, invalidError error) CheckFunc +``` + +MakeRegexpChecker makes a regexp checker for the given regexp expression with the given invalid result. + + +## type [Errors]() + +Errors provides a mapping of the checker errors keyed by the field names. + +```go +type Errors map[string]error +``` + + +### func [Check]() + +```go +func Check(s interface{}) (Errors, bool) +``` + +Check function checks the given struct based on the checkers listed in field tag names. + + +## type [MakeFunc]() + +MakeFunc defines the signature for the checker maker functions. + +```go +type MakeFunc func(params string) CheckFunc +``` + + +### func [MakeRegexpMaker]() + +```go +func MakeRegexpMaker(expression string, invalidError error) MakeFunc +``` + +MakeRegexpMaker makes a regexp checker maker for the given regexp expression with the given invalid result. + +Generated by [gomarkdoc]() + + + + # Contributing to the Project Anyone can contribute to Checkers library. Please make sure to read our [Contributor Covenant Code of Conduct](./CODE_OF_CONDUCT.md) guide first. Follow the [How to Contribute to Checker](./CONTRIBUTING.md) to contribute. diff --git a/alphanumeric.go b/alphanumeric.go index 8b87ace..0eb4f52 100644 --- a/alphanumeric.go +++ b/alphanumeric.go @@ -11,8 +11,8 @@ import ( "unicode" ) -// CheckerAlphanumeric is the name of the checker. -const CheckerAlphanumeric = "alphanumeric" +// tagAlphanumeric is the tag of the checker. +const tagAlphanumeric = "alphanumeric" // ErrNotAlphanumeric indicates that the given string contains non-alphanumeric characters. var ErrNotAlphanumeric = errors.New("please use only letters and numbers") diff --git a/ascii.go b/ascii.go index 32f68a2..b9b4318 100644 --- a/ascii.go +++ b/ascii.go @@ -11,8 +11,8 @@ import ( "unicode" ) -// CheckerASCII is the name of the checker. -const CheckerASCII = "ascii" +// tagASCII is the tag of the checker. +const tagASCII = "ascii" // ErrNotASCII indicates that the given string contains non-ASCII characters. var ErrNotASCII = errors.New("please use standard English characters only") diff --git a/checker.go b/checker.go index afbef50..595649c 100644 --- a/checker.go +++ b/checker.go @@ -30,37 +30,37 @@ type checkerJob struct { // makers provides a mapping of the maker functions keyed by the respective checker names. var makers = map[string]MakeFunc{ - CheckerAlphanumeric: makeAlphanumeric, - CheckerASCII: makeASCII, - CheckerCreditCard: makeCreditCard, - CheckerCidr: makeCidr, - CheckerDigits: makeDigits, - CheckerEmail: makeEmail, - CheckerFqdn: makeFqdn, - CheckerIP: makeIP, - CheckerIPV4: makeIPV4, - CheckerIPV6: makeIPV6, - CheckerISBN: makeISBN, - CheckerLuhn: makeLuhn, - CheckerMac: makeMac, - CheckerMax: makeMax, - CheckerMaxLength: makeMaxLength, - CheckerMin: makeMin, - CheckerMinLength: makeMinLength, - CheckerRegexp: makeRegexp, - CheckerRequired: makeRequired, - CheckerSame: makeSame, - CheckerURL: makeURL, - NormalizerHTMLEscape: makeHTMLEscape, - NormalizerHTMLUnescape: makeHTMLUnescape, - NormalizerLower: makeLower, - NormalizerUpper: makeUpper, - NormalizerTitle: makeTitle, - NormalizerTrim: makeTrim, - NormalizerTrimLeft: makeTrimLeft, - NormalizerTrimRight: makeTrimRight, - NormalizerURLEscape: makeURLEscape, - NormalizerURLUnescape: makeURLUnescape, + tagAlphanumeric: makeAlphanumeric, + tagASCII: makeASCII, + tagCreditCard: makeCreditCard, + tagCidr: makeCidr, + tagDigits: makeDigits, + tagEmail: makeEmail, + tagFqdn: makeFqdn, + tagIP: makeIP, + tagIPV4: makeIPV4, + tagIPV6: makeIPV6, + tagISBN: makeISBN, + tagLuhn: makeLuhn, + tagMac: makeMac, + tagMax: makeMax, + tagMaxLength: makeMaxLength, + tagMin: makeMin, + tagMinLength: makeMinLength, + tagRegexp: makeRegexp, + tagRequired: makeRequired, + tagSame: makeSame, + tagURL: makeURL, + tagHTMLEscape: makeHTMLEscape, + tagHTMLUnescape: makeHTMLUnescape, + tagLower: makeLower, + tagUpper: makeUpper, + tagTitle: makeTitle, + tagTrim: makeTrim, + tagTrimLeft: makeTrimLeft, + tagTrimRight: makeTrimRight, + tagURLEscape: makeURLEscape, + tagURLUnescape: makeURLUnescape, } // Register registers the given checker name and the maker function. diff --git a/cidr.go b/cidr.go index 275427c..0bad38d 100644 --- a/cidr.go +++ b/cidr.go @@ -11,8 +11,8 @@ import ( "reflect" ) -// CheckerCidr is the name of the checker. -const CheckerCidr = "cidr" +// tagCidr is the tag of the checker. +const tagCidr = "cidr" // ErrNotCidr indicates that the given value is not a valid CIDR. var ErrNotCidr = errors.New("please enter a valid CIDR") diff --git a/credit_card.go b/credit_card.go index 9f7cc01..5016153 100644 --- a/credit_card.go +++ b/credit_card.go @@ -12,8 +12,8 @@ import ( "strings" ) -// CheckerCreditCard is the name of the checker. -const CheckerCreditCard = "credit-card" +// tagCreditCard is the tag of the checker. +const tagCreditCard = "credit-card" // ErrNotCreditCard indicates that the given value is not a valid credit card number. var ErrNotCreditCard = errors.New("please enter a valid credit card number") diff --git a/digits.go b/digits.go index 9098622..83e41b7 100644 --- a/digits.go +++ b/digits.go @@ -11,8 +11,8 @@ import ( "unicode" ) -// CheckerDigits is the name of the checker. -const CheckerDigits = "digits" +// tagDigits is the tag of the checker. +const tagDigits = "digits" // ErrNotDigits indicates that the given string contains non-digit characters. var ErrNotDigits = errors.New("please enter a valid number") diff --git a/doc/checkers/alphanumeric.md b/doc/checkers/alphanumeric.md deleted file mode 100644 index ace92b0..0000000 --- a/doc/checkers/alphanumeric.md +++ /dev/null @@ -1,28 +0,0 @@ -# Alphanumeric Checker - -The `alphanumeric` checker checks if the given string consists of only alphanumeric characters. If the string contains non-alphanumeric characters, the checker will return the `NOT_ALPHANUMERIC` result. Here is an example: - -```golang -type User struct { - Username string `checkers:"alphanumeric"` -} - -user := &User{ - Username: "ABcd1234", -} - -_, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `alphanumeric` checker function [`IsAlphanumeric`](https://pkg.go.dev/github.com/cinar/checker#IsAlphanumeric) to validate the user input. Here is an example: - -```golang -result := checker.IsAlphanumeric("ABcd1234") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/ascii.md b/doc/checkers/ascii.md deleted file mode 100644 index cb5b9f8..0000000 --- a/doc/checkers/ascii.md +++ /dev/null @@ -1,28 +0,0 @@ -# ASCII Checker - -The `ascii` checker checks if the given string consists of only ASCII characters. If the string contains non-ASCII characters, the checker will return the `NOT_ASCII` result. Here is an example: - -```golang -type User struct { - Username string `checkers:"ascii"` -} - -user := &User{ - Username: "checker", -} - -_, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `ascii` checker function [`IsASCII`](https://pkg.go.dev/github.com/cinar/checker#IsASCII) to validate the user input. Here is an example: - -```golang -result := checker.IsASCII("Checker") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/cidr.md b/doc/checkers/cidr.md deleted file mode 100644 index d496f29..0000000 --- a/doc/checkers/cidr.md +++ /dev/null @@ -1,28 +0,0 @@ -# CIDR Checker - -The `cidr` checker checks if the value is a valid CIDR notation IP address and prefix length, like `192.0.2.0/24` or `2001:db8::/32`, as defined in [RFC 4632](https://rfc-editor.org/rfc/rfc4632.html) and [RFC 4291](https://rfc-editor.org/rfc/rfc4291.html). If the value is not a valid CIDR notation, the checker will return the `NOT_CIDR` result. Here is an example: - -```golang -type Network struct { - Subnet string `checkers:"cidr"` -} - -network := &Network{ - Subnet: "192.0.2.0/24", -} - -_, valid := checker.Check(network) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `cidr` checker function [`IsCidr`](https://pkg.go.dev/github.com/cinar/checker#IsASCII) to validate the user input. Here is an example: - -```golang -result := checker.IsCidr("2001:db8::/32") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/credit_card.md b/doc/checkers/credit_card.md deleted file mode 100644 index 19989d2..0000000 --- a/doc/checkers/credit_card.md +++ /dev/null @@ -1,62 +0,0 @@ -# Credit Card Checker - -The `credit-card` checker checks if the given value is a valid credit card number. If the given value is not a valid credit card number, the checker will return the `NOT_CREDIT_CARD` result. - -Here is an example: - -```golang -type Order struct { - CreditCard string `checkers:"credit-card"` -} - -order := &Order{ - CreditCard: invalidCard, -} - -_, valid := checker.Check(order) -if valid { - // Send the mistakes back to the user -} -``` - -The checker currently knows about AMEX, Diners, Discover, JCB, MasterCard, UnionPay, and VISA credit card numbers. - -If you would like to check for a subset of those credit cards, you can specify them through the checker config parameter. Here is an example: - -```golang -type Order struct { - CreditCard string `checkers:"credit-card:amex,visa"` -} - -order := &Order{ - CreditCard: "6011111111111117", -} - -_, valid := checker.Check(order) -if valid { - // Send the mistakes back to the user -} -``` - -If you would like to verify a credit card that is not listed here, please use the [luhn](luhn.md) checker to use the Luhn Algorithm to verify the check digit. - -In your custom checkers, you can call the `credit-card` checker functions below to validate the user input. - -- [`IsAnyCreditCard`](https://pkg.go.dev/github.com/cinar/checker#IsAnyCreditCard): checks if the given value is a valid credit card number. -- [`IsAmexCreditCard`](https://pkg.go.dev/github.com/cinar/checker#IsAmexCreditCard): checks if the given value is a valid AMEX credit card number. -- [`IsDinersCreditCard`](https://pkg.go.dev/github.com/cinar/checker#IsDinersCreditCard): checks if the given value is a valid Diners credit card number. -- [`IsDiscoverCreditCard`](https://pkg.go.dev/github.com/cinar/checker#IsDiscoverCreditCard): checks if the given value is a valid Discover credit card number. -- [`IsJcbCreditCard`](https://pkg.go.dev/github.com/cinar/checker#IsJcbCreditCard): checks if the given value is a valid JCB credit card number. -- [`IsMasterCardCreditCard`](https://pkg.go.dev/github.com/cinar/checker#IsMasterCardCreditCard): checks if the given value is a valid MasterCard credit card number. -- [`IsUnionPayCreditCard`](https://pkg.go.dev/github.com/cinar/checker#IsUnionPayCreditCard): checks if the given value is a valid UnionPay credit card number. -- [`IsVisaCreditCard`](https://pkg.go.dev/github.com/cinar/checker#IsVisaCreditCard): checks if the given value is a valid VISA credit card number. - -Here is an example: - -```golang -result := checker.IsAnyCreditCard("6011111111111117") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/digits.md b/doc/checkers/digits.md deleted file mode 100644 index 03fb89a..0000000 --- a/doc/checkers/digits.md +++ /dev/null @@ -1,28 +0,0 @@ -# Digits Checker - -The `digits` checker checks if the given string consists of only digit characters. If the string contains non-digit characters, the checker will return the `NOT_DIGITS` result. Here is an example: - -```golang -type User struct { - Id string `checkers:"digits"` -} - -user := &User{ - Id: "1234", -} - -_, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `digits` checker function [`IsDigits`](https://pkg.go.dev/github.com/cinar/checker#IsDigits) to validate the user input. Here is an example: - -```golang -result := checker.IsDigits("1234") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/email.md b/doc/checkers/email.md deleted file mode 100644 index db41c6a..0000000 --- a/doc/checkers/email.md +++ /dev/null @@ -1,28 +0,0 @@ -# Email Checker - -The `email` checker checks if the given string is an email address. If the given string is not a valid email address, the checker will return the `NOT_EMAIL` result. Here is an example: - -```golang -type User struct { - Email string `checkers:"email"` -} - -user := &User{ - Email: "user@zdo.com", -} - -_, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `email` checker function [`IsEmail`](https://pkg.go.dev/github.com/cinar/checker#IsEmail) to validate the user input. Here is an example: - -```golang -result := checker.IsEmail("user@zdo.com") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/fqdn.md b/doc/checkers/fqdn.md deleted file mode 100644 index e63565f..0000000 --- a/doc/checkers/fqdn.md +++ /dev/null @@ -1,28 +0,0 @@ -# FQDN Checker - -The Full Qualified Domain Name (FQDN) is the complete domain name for a computer or host on the internet. The `fqdn` checker checks if the given string consists of a FQDN. If the string is not a valid FQDN, the checker will return the `NOT_FQDN` result. Here is an example: - -```golang -type Request struct { - Domain string `checkers:"fqdn"` -} - -request := &Request{ - Domain: "zdo.com", -} - -_, valid := checker.Check(request) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `fqdn` checker function [`IsFqdn`](https://pkg.go.dev/github.com/cinar/checker#IsFqdn) to validate the user input. Here is an example: - -```golang -result := checker.IsFqdn("zdo.com") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/ip.md b/doc/checkers/ip.md deleted file mode 100644 index 4b01a51..0000000 --- a/doc/checkers/ip.md +++ /dev/null @@ -1,28 +0,0 @@ -# IP Checker - -The `ip` checker checks if the value is an IP address. If the value is not an IP address, the checker will return the `NOT_IP` result. Here is an example: - -```golang -type Request struct { - RemoteIP string `checkers:"ip"` -} - -request := &Request{ - RemoteIP: "192.168.1.1", -} - -_, valid := checker.Check(request) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `ip` checker function [`IsIP`](https://pkg.go.dev/github.com/cinar/checker#IsIP) to validate the user input. Here is an example: - -```golang -result := checker.IsIP("2001:db8::68") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/ipv4.md b/doc/checkers/ipv4.md deleted file mode 100644 index d3ac8a5..0000000 --- a/doc/checkers/ipv4.md +++ /dev/null @@ -1,28 +0,0 @@ -# IPv4 Checker - -The `ipv4` checker checks if the value is an IPv4 address. If the value is not an IPv4 address, the checker will return the `NOT_IP_V4` result. Here is an example: - -```golang -type Request struct { - RemoteIP string `checkers:"ipv4"` -} - -request := &Request{ - RemoteIP: "192.168.1.1", -} - -_, valid := checker.Check(request) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `ipv4` checker function [`IsIPV4`](https://pkg.go.dev/github.com/cinar/checker#IsIPV4) to validate the user input. Here is an example: - -```golang -result := checker.IsIPV4("192.168.1.1") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/ipv6.md b/doc/checkers/ipv6.md deleted file mode 100644 index 66626e2..0000000 --- a/doc/checkers/ipv6.md +++ /dev/null @@ -1,28 +0,0 @@ -# IPv6 Checker - -The `ipv6` checker checks if the value is an IPv6 address. If the value is not an IPv6 address, the checker will return the `NOT_IP_V6` result. Here is an example: - -```golang -type Request struct { - RemoteIP string `checkers:"ipv6"` -} - -request := &Request{ - RemoteIP: "2001:db8::68", -} - -_, valid := checker.Check(request) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `ipv6` checker function [`IsIPV6`](https://pkg.go.dev/github.com/cinar/checker#IsIPV6) to validate the user input. Here is an example: - -```golang -result := checker.IsIPV6("2001:db8::68") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/isbn.md b/doc/checkers/isbn.md deleted file mode 100644 index 40a936f..0000000 --- a/doc/checkers/isbn.md +++ /dev/null @@ -1,52 +0,0 @@ -# ISBN Checker - -An [ISBN (International Standard Book Number)](https://en.wikipedia.org/wiki/International_Standard_Book_Number) is a 10 or 13 digit number that is used to identify a book. - -The `isbn` checker checks if the given value is a valid ISBN. If the given value is not a valid ISBN, the checker will return the `NOT_ISBN` result. Here is an example: - -```golang -type Book struct { - ISBN string `checkers:"isbn"` -} - -book := &Book{ - ISBN: "1430248270", -} - -_, valid := checker.Check(book) -if !valid { - // Send the mistakes back to the user -} -``` - -The `isbn` checker can also be configured to check for a 10 digit or a 13 digit number. Here is an example: - -```golang -type Book struct { - ISBN string `checkers:"isbn:13"` -} - -book := &Book{ - ISBN: "9781430248279", -} - -_, valid := checker.Check(book) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `isbn` checker functions below to validate the user input. - -- [`IsISBN`](https://pkg.go.dev/github.com/cinar/checker#IsISBN) checks if the given value is a valid ISBN number. -- [`IsISBN10`](https://pkg.go.dev/github.com/cinar/checker#IsISBN10) checks if the given value is a valid ISBN 10 number. -- [`IsISBN13`](https://pkg.go.dev/github.com/cinar/checker#IsISBN13) checks if the given value is a valid ISBN 13 number. - -Here is an example: - -```golang -result := checker.IsISBN("1430248270") -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/luhn.md b/doc/checkers/luhn.md deleted file mode 100644 index 3148e43..0000000 --- a/doc/checkers/luhn.md +++ /dev/null @@ -1,28 +0,0 @@ -# Luhn Checker - -The `luhn` checker checks if the given number is valid based on the Luhn Algorithm. If the given number is not valid, it will return the `NOT_LUHN` result. Here is an example: - -```golang -type Order struct { - CreditCard string `checkers:"luhn"` -} - -order := &Order{ - CreditCard: "4012888888881881", -} - -_, valid := checker.Check(order) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `luhn` checker function [`IsLuhn`](https://pkg.go.dev/github.com/cinar/checker#IsLuhn) to validate the user input. Here is an example: - -```golang -result := checker.IsLuhn("4012888888881881") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/mac.md b/doc/checkers/mac.md deleted file mode 100644 index ac50fdd..0000000 --- a/doc/checkers/mac.md +++ /dev/null @@ -1,28 +0,0 @@ -# MAC Checker - -The `mac` checker checks if the value is a valid an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address. If the value is not a valid MAC address, the checker will return the `NOT_MAC` result. Here is an example: - -```golang -type Network struct { - HardwareAddress string `checkers:"mac"` -} - -network := &Network{ - HardwareAddress: "00:00:5e:00:53:01", -} - -_, valid := checker.Check(network) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `mac` checker function [`IsMac`](https://pkg.go.dev/github.com/cinar/checker#IsMac) to validate the user input. Here is an example: - -```golang -result := checker.IsMac("00:00:5e:00:53:01") - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/max.md b/doc/checkers/max.md deleted file mode 100644 index b26e714..0000000 --- a/doc/checkers/max.md +++ /dev/null @@ -1,30 +0,0 @@ -# Max Checker - -The `max` checker checks if the given ```int``` or ```float``` value is less than the given maximum. If the value is above the maximum, the checker will return the `NOT_MAX` result. Here is an example: - -```golang -type Order struct { - Quantity int `checkers:"max:10"` -} - -order := &Order{ - Quantity: 5, -} - -mistakes, valid := checker.Check(order) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `max` checker function [`IsMax`](https://pkg.go.dev/github.com/cinar/checker#IsMax) to validate the user input. Here is an example: - -```golang -quantity := 5 - -result := checker.IsMax(quantity, 10) - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/maxlength.md b/doc/checkers/maxlength.md deleted file mode 100644 index af008aa..0000000 --- a/doc/checkers/maxlength.md +++ /dev/null @@ -1,32 +0,0 @@ -# Max Length Checker - -The `max-length` checker checks if the length of the given value is less than the given maximum length. If the length of the value is above the maximum length, the checker will return the `NOT_MAX_LENGTH` result. Here is an example: - -```golang -type User struct { - Password string `checkers:"max-length:4"` -} - -user := &User{ - Password: "1234", -} - -mistakes, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` - -The checker can be applied to all types that are supported by the [reflect.Value.Len()](https://pkg.go.dev/reflect#Value.Len) function. - -If you do not want to validate user input stored in a struct, you can individually call the `max-length` checker function [`IsMaxLength`](https://pkg.go.dev/github.com/cinar/checker#IsMaxLength) to validate the user input. Here is an example: - -```golang -s := "1234" - -result := checker.IsMaxLength(s, 4) - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/min.md b/doc/checkers/min.md deleted file mode 100644 index 8520655..0000000 --- a/doc/checkers/min.md +++ /dev/null @@ -1,30 +0,0 @@ -# Min Checker - -The `min` checker checks if the given ```int``` or ```float``` value is greather than the given minimum. If the value is below the minimum, the checker will return the `NOT_MIN` result. Here is an example: - -```golang -type User struct { - Age int `checkers:"min:21"` -} - -user := &User{ - Age: 45, -} - -mistakes, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `min` checker function [`IsMin`](https://pkg.go.dev/github.com/cinar/checker#IsMin) to validate the user input. Here is an example: - -```golang -age := 45 - -result := checker.IsMin(age, 21) - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/minlength.md b/doc/checkers/minlength.md deleted file mode 100644 index 7a9a680..0000000 --- a/doc/checkers/minlength.md +++ /dev/null @@ -1,32 +0,0 @@ -# Min Length Checker - -The `min-length` checker checks if the length of the given value is greather than the given minimum length. If the length of the value is below the minimum length, the checker will return the `NOT_MIN_LENGTH` result. Here is an example: - -```golang -type User struct { - Password string `checkers:"min-length:4"` -} - -user := &User{ - Password: "1234", -} - -mistakes, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` - -The checker can be applied to all types that are supported by the [reflect.Value.Len()](https://pkg.go.dev/reflect#Value.Len) function. - -If you do not want to validate user input stored in a struct, you can individually call the `min-length` checker function [`IsMinLength`](https://pkg.go.dev/github.com/cinar/checker#IsMinLength) to validate the user input. Here is an example: - -```golang -s := "1234" - -result := checker.IsMinLength(s, 4) - -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/regexp.md b/doc/checkers/regexp.md deleted file mode 100644 index f97a4cb..0000000 --- a/doc/checkers/regexp.md +++ /dev/null @@ -1,48 +0,0 @@ -# Regexp Checker - -The `regexp` checker checks if the given string matches the given regexp. If the given string does not match, the checker will return the `NOT_MATCH` result. Here is an example: - -```golang -type User struct { - Username string `checkers:"regexp:^[A-Za-z]+$"` -} - -user := &User{ - Username: "abcd", -} - -_, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` - -The `regexp` checker can be used to build other checkers for other regexp patterns. In order to do that, you can use the [`MakeRegexpChecker`](https://pkg.go.dev/github.com/cinar/checker#MakeRegexpChecker) function. The function takes an expression and a result to return when the the given string is not a match. Here is an example: - -```golang -checkHex := checker.MakeRegexpChecker("^[A-Fa-f0-9]+$", "NOT_HEX") - -result := checkHex(reflect.ValueOf("f0f0f0"), reflect.ValueOf(nil)) -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` - -To register the new regexp checker to validate user input in struct, [`Register`](https://pkg.go.dev/github.com/cinar/checker#Register) function can be used. Here is an example: - -```golang -checker.Register("hex", checker.MakeRegexpMaker("^[A-Fa-f0-9]+$", "NOT_HEX")) - -type Theme struct { - Color string `checkers:hex` -} - -theme := &Theme{ - Color: "f0f0f0", -} - -_, valid := checker.Check(theme) -if !valid { - // Send the mistakes back to the user -} -``` diff --git a/doc/checkers/required.md b/doc/checkers/required.md deleted file mode 100644 index 5b4638b..0000000 --- a/doc/checkers/required.md +++ /dev/null @@ -1,27 +0,0 @@ -# Required Checker - -The `required` checker checks for the presence of required input. If the input is not present, the checker will return the `REQUIRED` result. Here is an example: - -```golang -type Person struct { - Name string `checkers:"required"` -} - -person := &Person{} - -mistakes, valid := checker.Check(person) -if !valid { - // Send the mistakes back to the user -} -``` - -If you do not want to validate user input stored in a struct, you can individually call the `required` checker function [`IsRequired`](https://pkg.go.dev/github.com/cinar/checker#IsRequired) to validate the user input. Here is an example: - -```golang -var name string - -result := checker.IsRequired(name) -if result != checker.ResultValid { - // Send the result back to the user -} -``` diff --git a/doc/checkers/same.md b/doc/checkers/same.md deleted file mode 100644 index f974dd1..0000000 --- a/doc/checkers/same.md +++ /dev/null @@ -1,20 +0,0 @@ -# Same Checker - -The `same` checker checks if the given value is equal to the value of the other field specified by its name. If they are not equal, the checker will return the `NOT_SAME` result. In the example below, the `same` checker ensures that the value in the `Confirm` field matches the value in the `Password` field. - -```golang -type User struct { - Password string - Confirm string `checkers:"same:Password"` -} - -user := &User{ - Password: "1234", - Confirm: "1234", -} - -mistakes, valid := checker.Check(user) -if !valid { - // Send the mistakes back to the user -} -``` \ No newline at end of file diff --git a/doc/checkers/url.md b/doc/checkers/url.md deleted file mode 100644 index c41b551..0000000 --- a/doc/checkers/url.md +++ /dev/null @@ -1,29 +0,0 @@ -# URL Checker - -The `url` checker checks if the given value is a valid URL. If the given value is not a valid URL, the checker will return the `NOT_URL` result. The checker uses [ParseRequestURI](https://pkg.go.dev/net/url#ParseRequestURI) function to parse the URL, and then checks if the schema or the host are both set. - -Here is an example: - -```golang -type Bookmark struct { - URL string `checkers:"url"` -} - -bookmark := &Bookmark{ - URL: "https://zdo.com", -} - -_, valid := checker.Check(bookmark) -if !valid { - // Send the mistakes back to the user -} -``` - -In your custom checkers, you can call the `url` checker function [`IsURL`](https://pkg.go.dev/github.com/cinar/checker#IsURL) to validate the user input. Here is an example: - -```golang -result := checker.IsURL("https://zdo.com") -if result != checker.ResultValid { - // Send the mistakes back to the user -} -``` diff --git a/doc/normalizers/html_escape.md b/doc/normalizers/html_escape.md deleted file mode 100644 index a8f9f9f..0000000 --- a/doc/normalizers/html_escape.md +++ /dev/null @@ -1,19 +0,0 @@ -# HTML Escape Normalizer - -The `html-escape` normalizer uses [html.EscapeString](https://pkg.go.dev/html#EscapeString) to escape special characters like "<" to become "<". It escapes only five such characters: <, >, &, ' and ". - -```golang -type Comment struct { - Body string `checkers:"html-escape"` -} - -comment := &Comment{ - Body: " \"Checker\" & 'Library' ", -} - -checker.Check(comment) - -// Outputs: -// <tag> "Checker" & 'Library' </tag> -fmt.Println(comment.Body) -``` diff --git a/doc/normalizers/html_unescape.md b/doc/normalizers/html_unescape.md deleted file mode 100644 index ae64303..0000000 --- a/doc/normalizers/html_unescape.md +++ /dev/null @@ -1,22 +0,0 @@ -# HTML Unescape Normalizer - -The `html-unescape` normalizer uses [html.UnescapeString](https://pkg.go.dev/html#UnescapeString) to unescape entities like "<" to become "<". It unescapes a larger range of entities than EscapeString escapes. For example, "á" unescapes to "รก", as does "á" and "á". - -```golang -type Comment struct { - Body string `checkers:"html-unescape"` -} - -comment := &Comment{ - Body: "<tag> "Checker" & 'Library' </tag>", -} - -_, valid := checker.Check(comment) -if !valid { - t.Fail() -} - -// Outputs: -// \"Checker\" & 'Library' -fmt.Println(comment.Body) -``` diff --git a/doc/normalizers/lower.md b/doc/normalizers/lower.md deleted file mode 100644 index 0c8d041..0000000 --- a/doc/normalizers/lower.md +++ /dev/null @@ -1,17 +0,0 @@ -# Lower Case Normalizer - -The `lower` normalizer maps all Unicode letters in the given value to their lower case. It can be mixed with checkers and other normalizers when defining the validation steps for user data. - -```golang -type User struct { - Username string `checkers:"lower"` -} - -user := &User{ - Username: "chECker", -} - -checker.Check(user) - -fmt.Println(user.Username) // checker -``` diff --git a/doc/normalizers/title.md b/doc/normalizers/title.md deleted file mode 100644 index 7eb4710..0000000 --- a/doc/normalizers/title.md +++ /dev/null @@ -1,17 +0,0 @@ -# Title Case Normalizer - -The `title` normalizer maps the first letter of each word to their upper case. It can be mixed with checkers and other normalizers when defining the validation steps for user data. - -```golang -type Book struct { - Chapter string `checkers:"title"` -} - -book := &Book{ - Chapter: "THE checker", -} - -checker.Check(book) - -fmt.Println(book.Chapter) // The Checker -``` diff --git a/doc/normalizers/trim.md b/doc/normalizers/trim.md deleted file mode 100644 index e99f112..0000000 --- a/doc/normalizers/trim.md +++ /dev/null @@ -1,17 +0,0 @@ -# Trim Normalizer - -The `trim` normalizer removes the whitespaces at the beginning and at the end of the given value. It can be mixed with checkers and other normalizers when defining the validation steps for user data. - -```golang -type User struct { - Username string `checkers:"trim"` -} - -user := &User{ - Username: " normalizer ", -} - -checker.Check(user) - -fmt.Println(user.Username) // CHECKER -``` diff --git a/doc/normalizers/trim_left.md b/doc/normalizers/trim_left.md deleted file mode 100644 index 7c49338..0000000 --- a/doc/normalizers/trim_left.md +++ /dev/null @@ -1,17 +0,0 @@ -# Trim Left Normalizer - -The `trim-left` normalizer removes the whitespaces at the beginning of the given value. It can be mixed with checkers and other normalizers when defining the validation steps for user data. - -```golang -type User struct { - Username string `checkers:"trim-left"` -} - -user := &User{ - Username: " normalizer", -} - -checker.Check(user) - -fmt.Println(user.Username) // normalizer -``` diff --git a/doc/normalizers/trim_right.md b/doc/normalizers/trim_right.md deleted file mode 100644 index d2e0ca0..0000000 --- a/doc/normalizers/trim_right.md +++ /dev/null @@ -1,17 +0,0 @@ -# Trim Right Normalizer - -The `trim-right` normalizer removes the whitespaces at the end of the given value. It can be mixed with checkers and other normalizers when defining the validation steps for user data. - -```golang -type User struct { - Username string `checkers:"trim-right"` -} - -user := &User{ - Username: "normalizer ", -} - -checker.Check(user) - -fmt.Println(user.Username) // CHECKER -``` diff --git a/doc/normalizers/upper.md b/doc/normalizers/upper.md deleted file mode 100644 index c57ed01..0000000 --- a/doc/normalizers/upper.md +++ /dev/null @@ -1,15 +0,0 @@ -# Upper Case Normalizer - -The `upper` normalizer maps all Unicode letters in the given value to their upper case. It can be mixed with checkers and other normalizers when defining the validation steps for user data. - -```golang -type User struct { - Username string `checkers:"upper"` -} - -user := &User{ - Username: "chECker", -} - -fmt.Println(user.Username) // CHECKER -``` diff --git a/doc/normalizers/url_escape.md b/doc/normalizers/url_escape.md deleted file mode 100644 index 0d229d5..0000000 --- a/doc/normalizers/url_escape.md +++ /dev/null @@ -1,22 +0,0 @@ -# URL Escape Normalizer - -The `url-escape` normalizer uses [net.url.QueryEscape](https://pkg.go.dev/net/url#QueryEscape) to escape the string so it can be safely placed inside a URL query. - -```golang -type Request struct { - Query string `checkers:"url-escape"` -} - -request := &Request{ - Query: "param1/param2 = 1 + 2 & 3 + 4", -} - -_, valid := checker.Check(request) -if !valid { - t.Fail() -} - -// Outputs: -// param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4 -fmt.Println(request.Query) -``` diff --git a/doc/normalizers/url_unescape.md b/doc/normalizers/url_unescape.md deleted file mode 100644 index 8982a49..0000000 --- a/doc/normalizers/url_unescape.md +++ /dev/null @@ -1,26 +0,0 @@ -# URL Unescape Normalizer - -The `url-unescape` normalizer uses [net.url.QueryUnescape](https://pkg.go.dev/net/url#QueryUnescape) to converte each 3-byte encoded substring of the form "%AB" into the hex-decoded byte 0xAB. - -```golang -type Request struct { - Query string `checkers:"url-unescape"` -} - -request := &Request{ - Query: "param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4", -} - -_, valid := checker.Check(request) -if !valid { - t.Fail() -} - -if request.Query != "param1/param2 = 1 + 2 & 3 + 4" { - t.Fail() -} - -// Outputs: -// param1/param2 = 1 + 2 & 3 + 4 -fmt.Println(comment.Body) -``` diff --git a/doc/optimization.md b/doc/optimization.md deleted file mode 100644 index d0e6dbf..0000000 --- a/doc/optimization.md +++ /dev/null @@ -1,15 +0,0 @@ -# Optimization Guide - -The ```BenchmarkCheck``` function helps profiling the library. Generate a ```profile.out``` my issuing the command below. - -``` -go test -bench=. -benchmem -cpuprofile profile.out -``` - -View the analysis through the ```pprof``` by issuing the command below. - -``` -go tool pprof -http localhost:9000 profile.out -``` - -Use the web interface for further optimization work. diff --git a/email.go b/email.go index b0ffc8f..2c456c4 100644 --- a/email.go +++ b/email.go @@ -12,8 +12,8 @@ import ( "strings" ) -// CheckerEmail is the name of the checker. -const CheckerEmail = "email" +// tagEmail is the tag of the checker. +const tagEmail = "email" // ErrNotEmail indicates that the given string is not a valid email. var ErrNotEmail = errors.New("please enter a valid email address") diff --git a/fqdn.go b/fqdn.go index e227260..c6c816d 100644 --- a/fqdn.go +++ b/fqdn.go @@ -12,8 +12,8 @@ import ( "strings" ) -// CheckerFqdn is the name of the checker. -const CheckerFqdn = "fqdn" +// tagFqdn is the tag of the checker. +const tagFqdn = "fqdn" // ErrNotFqdn indicates that the given string is not a valid FQDN. var ErrNotFqdn = errors.New("please enter a valid domain name") diff --git a/html_escape.go b/html_escape.go index 012448d..0a08372 100644 --- a/html_escape.go +++ b/html_escape.go @@ -10,8 +10,8 @@ import ( "reflect" ) -// NormalizerHTMLEscape is the name of the normalizer. -const NormalizerHTMLEscape = "html-escape" +// tagHTMLEscape is the tag of the normalizer. +const tagHTMLEscape = "html-escape" // makeHTMLEscape makes a normalizer function for the HTML escape normalizer. func makeHTMLEscape(_ string) CheckFunc { diff --git a/html_unescape.go b/html_unescape.go index 3c8a93f..df78575 100644 --- a/html_unescape.go +++ b/html_unescape.go @@ -10,8 +10,8 @@ import ( "reflect" ) -// NormalizerHTMLUnescape is the name of the normalizer. -const NormalizerHTMLUnescape = "html-unescape" +// tagHTMLUnescape is the tag of the normalizer. +const tagHTMLUnescape = "html-unescape" // makeHTMLUnescape makes a normalizer function for the HTML unscape normalizer. func makeHTMLUnescape(_ string) CheckFunc { diff --git a/ip.go b/ip.go index 9fa240b..023dc6c 100644 --- a/ip.go +++ b/ip.go @@ -11,8 +11,8 @@ import ( "reflect" ) -// CheckerIP is the name of the checker. -const CheckerIP = "ip" +// tagIP is the tag of the checker. +const tagIP = "ip" // ErrNotIP indicates that the given value is not an IP address. var ErrNotIP = errors.New("please enter a valid IP address") diff --git a/ipv4.go b/ipv4.go index 93d28ca..4168626 100644 --- a/ipv4.go +++ b/ipv4.go @@ -11,8 +11,8 @@ import ( "reflect" ) -// CheckerIPV4 is the name of the checker. -const CheckerIPV4 = "ipv4" +// tagIPV4 is the tag of the checker. +const tagIPV4 = "ipv4" // ErrNotIPV4 indicates that the given value is not an IPv4 address. var ErrNotIPV4 = errors.New("please enter a valid IPv4 address") diff --git a/ipv6.go b/ipv6.go index 8fe9a07..49c7533 100644 --- a/ipv6.go +++ b/ipv6.go @@ -11,8 +11,8 @@ import ( "reflect" ) -// CheckerIPV6 is the name of the checker. -const CheckerIPV6 = "ipv6" +// tagIPV6 is the tag of the checker. +const tagIPV6 = "ipv6" // ErrNotIPV6 indicates that the given value is not an IPv6 address. var ErrNotIPV6 = errors.New("please enter a valid IPv6 address") diff --git a/isbn.go b/isbn.go index 67fbb10..11ebf28 100644 --- a/isbn.go +++ b/isbn.go @@ -17,8 +17,8 @@ import ( // How to Verify an ISBN // https://www.instructables.com/How-to-verify-a-ISBN/ -// CheckerISBN is the name of the checker. -const CheckerISBN = "isbn" +// tagISBN is the tag of the checker. +const tagISBN = "isbn" // ErrNotISBN indicates that the given value is not a valid ISBN. var ErrNotISBN = errors.New("please enter a valid ISBN number") diff --git a/lower.go b/lower.go index 3843c33..585792d 100644 --- a/lower.go +++ b/lower.go @@ -10,8 +10,8 @@ import ( "strings" ) -// NormalizerLower is the name of the normalizer. -const NormalizerLower = "lower" +// tagLower is the tag of the normalizer. +const tagLower = "lower" // makeLower makes a normalizer function for the lower normalizer. func makeLower(_ string) CheckFunc { diff --git a/luhn.go b/luhn.go index 6e222d3..d1543ef 100644 --- a/luhn.go +++ b/luhn.go @@ -10,8 +10,8 @@ import ( "reflect" ) -// CheckerLuhn is the name of the checker. -const CheckerLuhn = "luhn" +// tagLuhn is the tag of the checker. +const tagLuhn = "luhn" // ErrNotLuhn indicates that the given number is not valid based on the Luhn algorithm. var ErrNotLuhn = errors.New("please enter a valid LUHN") diff --git a/mac.go b/mac.go index 509ecc3..58f6284 100644 --- a/mac.go +++ b/mac.go @@ -11,8 +11,8 @@ import ( "reflect" ) -// CheckerMac is the name of the checker. -const CheckerMac = "mac" +// tagMac is the tag of the checker. +const tagMac = "mac" // ErrNotMac indicates that the given value is not an MAC address. var ErrNotMac = errors.New("please enter a valid MAC address") diff --git a/max.go b/max.go index ed8c2d3..f26246d 100644 --- a/max.go +++ b/max.go @@ -11,8 +11,8 @@ import ( "strconv" ) -// CheckerMax is the name of the checker. -const CheckerMax = "max" +// tagMax is the tag of the checker. +const tagMax = "max" // IsMax checks if the given value is below than the given maximum. func IsMax(value interface{}, max float64) error { diff --git a/maxlength.go b/maxlength.go index 63a0f0a..3b12f35 100644 --- a/maxlength.go +++ b/maxlength.go @@ -11,8 +11,8 @@ import ( "strconv" ) -// CheckerMaxLength is the name of the checker. -const CheckerMaxLength = "max-length" +// tagMaxLength is the tag of the checker. +const tagMaxLength = "max-length" // IsMaxLength checks if the length of the given value is less than the given maximum length. func IsMaxLength(value interface{}, maxLength int) error { diff --git a/min.go b/min.go index 1807b1f..9531db1 100644 --- a/min.go +++ b/min.go @@ -11,8 +11,8 @@ import ( "strconv" ) -// CheckerMin is the name of the checker. -const CheckerMin = "min" +// tagMin is the tag of the checker. +const tagMin = "min" // IsMin checks if the given value is above than the given minimum. func IsMin(value interface{}, min float64) error { diff --git a/minlenght.go b/minlenght.go index b8bf0e1..f1cefac 100644 --- a/minlenght.go +++ b/minlenght.go @@ -11,8 +11,8 @@ import ( "strconv" ) -// CheckerMinLength is the name of the checker. -const CheckerMinLength = "min-length" +// tagMinLength is the tag of the checker. +const tagMinLength = "min-length" // IsMinLength checks if the length of the given value is greather than the given minimum length. func IsMinLength(value interface{}, minLength int) error { diff --git a/regexp.go b/regexp.go index b48ff18..1e95a9b 100644 --- a/regexp.go +++ b/regexp.go @@ -11,8 +11,8 @@ import ( "regexp" ) -// CheckerRegexp is the name of the checker. -const CheckerRegexp = "regexp" +// tagRegexp is the tag of the checker. +const tagRegexp = "regexp" // ErrNotMatch indicates that the given string does not match the regexp pattern. var ErrNotMatch = errors.New("please enter a valid input") diff --git a/required.go b/required.go index 16c092e..01a35f8 100644 --- a/required.go +++ b/required.go @@ -10,8 +10,8 @@ import ( "reflect" ) -// CheckerRequired is the name of the checker. -const CheckerRequired = "required" +// tagRequired is the tag of the checker. +const tagRequired = "required" // ErrRequired indicates that the required value is missing. var ErrRequired = errors.New("is required") diff --git a/same.go b/same.go index 10b67d2..f4a3e06 100644 --- a/same.go +++ b/same.go @@ -10,8 +10,8 @@ import ( "reflect" ) -// CheckerSame is the name of the checker. -const CheckerSame = "same" +// tagSame is the tag of the checker. +const tagSame = "same" // ErrNotSame indicates that the given two values are not equal to each other. var ErrNotSame = errors.New("does not match the other") diff --git a/taskfile.yml b/taskfile.yml index eb18359..c5d8020 100644 --- a/taskfile.yml +++ b/taskfile.yml @@ -7,6 +7,7 @@ tasks: - task: fmt - task: lint - task: test + - task: docs action: deps: [lint, test] @@ -26,3 +27,7 @@ tasks: cmds: - go test -cover -coverprofile=coverage.out ./... + docs: + cmds: + - go run github.com/princjef/gomarkdoc/cmd/gomarkdoc@v1.1.0 -e ./... + diff --git a/title.go b/title.go index 90d2b6b..604ca2c 100644 --- a/title.go +++ b/title.go @@ -11,8 +11,8 @@ import ( "unicode" ) -// NormalizerTitle is the name of the normalizer. -const NormalizerTitle = "title" +// tagTitle is the tag of the normalizer. +const tagTitle = "title" // makeTitle makes a normalizer function for the title normalizer. func makeTitle(_ string) CheckFunc { diff --git a/trim.go b/trim.go index 88cbc82..749cb22 100644 --- a/trim.go +++ b/trim.go @@ -10,8 +10,8 @@ import ( "strings" ) -// NormalizerTrim is the name of the normalizer. -const NormalizerTrim = "trim" +// tagTrim is the tag of the normalizer. +const tagTrim = "trim" // makeTrim makes a normalizer function for the trim normalizer. func makeTrim(_ string) CheckFunc { diff --git a/trim_left.go b/trim_left.go index e3e53ba..f17c699 100644 --- a/trim_left.go +++ b/trim_left.go @@ -10,8 +10,8 @@ import ( "strings" ) -// NormalizerTrimLeft is the name of the normalizer. -const NormalizerTrimLeft = "trim-left" +// tagTrimLeft is the tag of the normalizer. +const tagTrimLeft = "trim-left" // makeTrimLeft makes a normalizer function for the trim left normalizer. func makeTrimLeft(_ string) CheckFunc { diff --git a/trim_right.go b/trim_right.go index 82d4115..e2edb9e 100644 --- a/trim_right.go +++ b/trim_right.go @@ -10,8 +10,8 @@ import ( "strings" ) -// NormalizerTrimRight is the name of the normalizer. -const NormalizerTrimRight = "trim-right" +// tagTrimRight is the tag of the normalizer. +const tagTrimRight = "trim-right" // makeTrimRight makes a normalizer function for the trim right normalizer. func makeTrimRight(_ string) CheckFunc { diff --git a/upper.go b/upper.go index a0a101e..7bef24b 100644 --- a/upper.go +++ b/upper.go @@ -10,8 +10,8 @@ import ( "strings" ) -// NormalizerUpper is the name of the normalizer. -const NormalizerUpper = "upper" +// tagUpper is the tag of the normalizer. +const tagUpper = "upper" // makeUpper makes a normalizer function for the upper normalizer. func makeUpper(_ string) CheckFunc { diff --git a/url.go b/url.go index b43efb3..a27b85a 100644 --- a/url.go +++ b/url.go @@ -11,8 +11,8 @@ import ( "reflect" ) -// CheckerURL is the name of the checker. -const CheckerURL = "url" +// tagURL is the tag of the checker. +const tagURL = "url" // ErrNotURL indicates that the given value is not a valid URL. var ErrNotURL = errors.New("please enter a valid URL") diff --git a/url_escape.go b/url_escape.go index f78abe2..d7ab2d7 100644 --- a/url_escape.go +++ b/url_escape.go @@ -10,8 +10,8 @@ import ( "reflect" ) -// NormalizerURLEscape is the name of the normalizer. -const NormalizerURLEscape = "url-escape" +// tagURLEscape is the tag of the normalizer. +const tagURLEscape = "url-escape" // makeURLEscape makes a normalizer function for the URL escape normalizer. func makeURLEscape(_ string) CheckFunc { diff --git a/url_unescape.go b/url_unescape.go index d10e602..dc6a807 100644 --- a/url_unescape.go +++ b/url_unescape.go @@ -10,8 +10,8 @@ import ( "reflect" ) -// NormalizerURLUnescape is the name of the normalizer. -const NormalizerURLUnescape = "url-unescape" +// tagURLUnescape is the tag of the normalizer. +const tagURLUnescape = "url-unescape" // makeURLUnescape makes a normalizer function for the URL unscape normalizer. func makeURLUnescape(_ string) CheckFunc {