From 0cb0baf8794e4d092cfce15d2965509e025d8f22 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Tue, 29 Oct 2024 20:10:01 -0700 Subject: [PATCH] Doc automation --- .gomarkdoc.yml | 3 ++ README.md | 2 ++ doc/checkers/alphanumeric.md | 28 --------------- doc/checkers/ascii.md | 28 --------------- doc/checkers/cidr.md | 28 --------------- doc/checkers/credit_card.md | 62 -------------------------------- doc/checkers/digits.md | 28 --------------- doc/checkers/email.md | 28 --------------- doc/checkers/fqdn.md | 28 --------------- doc/checkers/ip.md | 28 --------------- doc/checkers/ipv4.md | 28 --------------- doc/checkers/ipv6.md | 28 --------------- doc/checkers/isbn.md | 52 --------------------------- doc/checkers/luhn.md | 28 --------------- doc/checkers/mac.md | 28 --------------- doc/checkers/max.md | 30 ---------------- doc/checkers/maxlength.md | 32 ----------------- doc/checkers/min.md | 30 ---------------- doc/checkers/minlength.md | 32 ----------------- doc/checkers/regexp.md | 48 ------------------------- doc/checkers/required.md | 27 -------------- doc/checkers/same.md | 20 ----------- doc/checkers/url.md | 29 --------------- doc/normalizers/html_escape.md | 19 ---------- doc/normalizers/html_unescape.md | 22 ------------ doc/normalizers/lower.md | 17 --------- doc/normalizers/title.md | 17 --------- doc/normalizers/trim.md | 17 --------- doc/normalizers/trim_left.md | 17 --------- doc/normalizers/trim_right.md | 17 --------- doc/normalizers/upper.md | 15 -------- doc/normalizers/url_escape.md | 22 ------------ doc/normalizers/url_unescape.md | 26 -------------- doc/optimization.md | 15 -------- taskfile.yml | 4 +++ 35 files changed, 9 insertions(+), 874 deletions(-) create mode 100644 .gomarkdoc.yml delete mode 100644 doc/checkers/alphanumeric.md delete mode 100644 doc/checkers/ascii.md delete mode 100644 doc/checkers/cidr.md delete mode 100644 doc/checkers/credit_card.md delete mode 100644 doc/checkers/digits.md delete mode 100644 doc/checkers/email.md delete mode 100644 doc/checkers/fqdn.md delete mode 100644 doc/checkers/ip.md delete mode 100644 doc/checkers/ipv4.md delete mode 100644 doc/checkers/ipv6.md delete mode 100644 doc/checkers/isbn.md delete mode 100644 doc/checkers/luhn.md delete mode 100644 doc/checkers/mac.md delete mode 100644 doc/checkers/max.md delete mode 100644 doc/checkers/maxlength.md delete mode 100644 doc/checkers/min.md delete mode 100644 doc/checkers/minlength.md delete mode 100644 doc/checkers/regexp.md delete mode 100644 doc/checkers/required.md delete mode 100644 doc/checkers/same.md delete mode 100644 doc/checkers/url.md delete mode 100644 doc/normalizers/html_escape.md delete mode 100644 doc/normalizers/html_unescape.md delete mode 100644 doc/normalizers/lower.md delete mode 100644 doc/normalizers/title.md delete mode 100644 doc/normalizers/trim.md delete mode 100644 doc/normalizers/trim_left.md delete mode 100644 doc/normalizers/trim_right.md delete mode 100644 doc/normalizers/upper.md delete mode 100644 doc/normalizers/url_escape.md delete mode 100644 doc/normalizers/url_unescape.md delete mode 100644 doc/optimization.md 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..582d949 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,8 @@ type User struct { } ``` + + # 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/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/taskfile.yml b/taskfile.yml index eb18359..f143a22 100644 --- a/taskfile.yml +++ b/taskfile.yml @@ -26,3 +26,7 @@ tasks: cmds: - go test -cover -coverprofile=coverage.out ./... + docs: + cmds: + - go run github.com/princjef/gomarkdoc/cmd/gomarkdoc@v1.1.0 -e ./... +