Add Examples. Fixes #111 (#115)

# Describe Request

Add Examples. Fixes #111

# Change Type

Documentation change.
This commit is contained in:
Onur Cinar 2023-06-24 18:41:42 -07:00 committed by GitHub
commit 3a53201769
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 292 additions and 86 deletions

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsAlphanumeric() {
result := checker.IsAlphanumeric("ABcd1234")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsAlphanumericInvalid(t *testing.T) {
if checker.IsAlphanumeric("-/") == checker.ResultValid {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsASCII() {
result := checker.IsASCII("Checker")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsASCIIInvalid(t *testing.T) {
if checker.IsASCII("𝄞 Music!") == checker.ResultValid {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsCidr() {
result := checker.IsCidr("2001:db8::/32")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsCidrInvalid(t *testing.T) {
if checker.IsCidr("900.800.200.100//24") == checker.ResultValid {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker
import (
@ -85,8 +84,8 @@ func IsDinersCreditCard(number string) Result {
return isCreditCard(number, dinersPattern)
}
// IsDiscoveryCreditCard checks if the given valie is a valid Discovery credit card.
func IsDiscoveryCreditCard(number string) Result {
// IsDiscoverCreditCard checks if the given valie is a valid Discover credit card.
func IsDiscoverCreditCard(number string) Result {
return isCreditCard(number, discoverPattern)
}

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -37,6 +36,14 @@ func changeToInvalidLuhn(number string) string {
return number[:len(number)-1] + strconv.Itoa(luhn)
}
func ExampleIsAnyCreditCard() {
result := checker.IsAnyCreditCard("6011111111111117")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsAnyCreditCardValid(t *testing.T) {
if checker.IsAnyCreditCard(amexCard) != checker.ResultValid {
t.Fail()
@ -55,6 +62,14 @@ func TestIsAnyCreditCardInvalidLuhn(t *testing.T) {
}
}
func ExampleIsAmexCreditCard() {
result := checker.IsAmexCreditCard("378282246310005")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsAmexCreditCardValid(t *testing.T) {
if checker.IsAmexCreditCard(amexCard) != checker.ResultValid {
t.Fail()
@ -73,6 +88,13 @@ func TestIsAmexCreditCardInvalidLuhn(t *testing.T) {
}
}
func ExampleIsDinersCreditCard() {
result := checker.IsDinersCreditCard("36227206271667")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsDinersCreditCardValid(t *testing.T) {
if checker.IsDinersCreditCard(dinersCard) != checker.ResultValid {
t.Fail()
@ -91,24 +113,39 @@ func TestIsDinersCreditCardInvalidLuhn(t *testing.T) {
}
}
func ExampleIsDiscoverCreditCard() {
result := checker.IsDiscoverCreditCard("6011111111111117")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsDiscoverCreditCardValid(t *testing.T) {
if checker.IsDiscoveryCreditCard(discoverCard) != checker.ResultValid {
if checker.IsDiscoverCreditCard(discoverCard) != checker.ResultValid {
t.Fail()
}
}
func TestIsDiscoverCreditCardInvalidPattern(t *testing.T) {
if checker.IsDiscoveryCreditCard(invalidCard) == checker.ResultValid {
if checker.IsDiscoverCreditCard(invalidCard) == checker.ResultValid {
t.Fail()
}
}
func TestIsDiscoverCreditCardInvalidLuhn(t *testing.T) {
if checker.IsDiscoveryCreditCard(changeToInvalidLuhn(discoverCard)) == checker.ResultValid {
if checker.IsDiscoverCreditCard(changeToInvalidLuhn(discoverCard)) == checker.ResultValid {
t.Fail()
}
}
func ExampleIsJcbCreditCard() {
result := checker.IsJcbCreditCard("3530111333300000")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsJcbCreditCardValid(t *testing.T) {
if checker.IsJcbCreditCard(jcbCard) != checker.ResultValid {
t.Fail()
@ -127,6 +164,14 @@ func TestIsJcbCreditCardInvalidLuhn(t *testing.T) {
}
}
func ExampleIsMasterCardCreditCard() {
result := checker.IsMasterCardCreditCard("5555555555554444")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsMasterCardCreditCardValid(t *testing.T) {
if checker.IsMasterCardCreditCard(masterCard) != checker.ResultValid {
t.Fail()
@ -145,6 +190,14 @@ func TestIsMasterCardCreditCardInvalidLuhn(t *testing.T) {
}
}
func ExampleIsUnionPayCreditCard() {
result := checker.IsUnionPayCreditCard("6200000000000005")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsUnionPayCreditCardValid(t *testing.T) {
if checker.IsUnionPayCreditCard(unionPayCard) != checker.ResultValid {
t.Fail()
@ -163,6 +216,13 @@ func TestIsUnionPayCreditCardInvalidLuhn(t *testing.T) {
}
}
func ExampleIsVisaCreditCard() {
result := checker.IsVisaCreditCard("4111111111111111")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsVisaCreditCardValid(t *testing.T) {
if checker.IsVisaCreditCard(visaCard) != checker.ResultValid {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsDigits() {
result := checker.IsDigits("1234")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsDigitsInvalid(t *testing.T) {
if checker.IsDigits("checker") == checker.ResultValid {
t.Fail()

View file

@ -18,7 +18,7 @@ if !valid {
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
var name string
result := checker.IsRequired(name)
if result != checker.ResultValid {

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsEmail() {
result := checker.IsEmail("user@zdo.com")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestCheckEmailNonString(t *testing.T) {
defer checker.FailIfNoPanic(t)

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsFqdn() {
result := checker.IsFqdn("zdo.com")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestCheckFdqnWithoutTld(t *testing.T) {
if checker.IsFqdn("abcd") != checker.ResultNotFqdn {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsIP() {
result := checker.IsIP("2001:db8::68")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsIPInvalid(t *testing.T) {
if checker.IsIP("900.800.200.100") == checker.ResultValid {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsIPV4() {
result := checker.IsIPV4("192.168.1.1")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsIPV4Invalid(t *testing.T) {
if checker.IsIPV4("900.800.200.100") == checker.ResultValid {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsIPV6() {
result := checker.IsIPV6("2001:db8::68")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsIPV6Invalid(t *testing.T) {
if checker.IsIPV6("900.800.200.100") == checker.ResultValid {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,13 @@ import (
"github.com/cinar/checker"
)
func ExampleIsISBN10() {
result := checker.IsISBN10("1430248270")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsISBN10Valid(t *testing.T) {
result := checker.IsISBN10("1430248270")
if result != checker.ResultValid {
@ -49,6 +55,13 @@ func TestIsISBN10InvalidCheck(t *testing.T) {
}
}
func ExampleIsISBN13() {
result := checker.IsISBN13("9781430248279")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsISBN13Valid(t *testing.T) {
result := checker.IsISBN13("9781430248279")
if result != checker.ResultValid {
@ -77,6 +90,13 @@ func TestIsISBN13InvalidCheck(t *testing.T) {
}
}
func ExampleIsISBN() {
result := checker.IsISBN("1430248270")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsISBNValid10(t *testing.T) {
result := checker.IsISBN("1430248270")
if result != checker.ResultValid {

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsLuhn() {
result := checker.IsLuhn("4012888888881881")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsLuhnValid(t *testing.T) {
numbers := []string{
"4012888888881881",

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)
func ExampleIsMac() {
result := checker.IsMac("00:00:5e:00:53:01")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsMacInvalid(t *testing.T) {
if checker.IsMac("1234") == checker.ResultValid {
t.Fail()

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,16 @@ import (
"github.com/cinar/checker"
)
func ExampleIsMax() {
quantity := 5
result := checker.IsMax(quantity, 10)
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsMaxValid(t *testing.T) {
n := 5

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,16 @@ import (
"github.com/cinar/checker"
)
func ExampleIsMaxLength() {
s := "1234"
result := checker.IsMaxLength(s, 4)
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsMaxLengthValid(t *testing.T) {
s := "1234"

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,16 @@ import (
"github.com/cinar/checker"
)
func ExampleIsMin() {
age := 45
result := checker.IsMin(age, 21)
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsMinValid(t *testing.T) {
n := 45

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,16 @@ import (
"github.com/cinar/checker"
)
func ExampleIsMinLength() {
s := "1234"
result := checker.IsMinLength(s, 4)
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsMinLengthValid(t *testing.T) {
s := "1234"

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,15 @@ import (
"github.com/cinar/checker"
)
func ExampleIsRequired() {
var name string
result := checker.IsRequired(name)
if result != checker.ResultValid {
// Send the result back to the user
}
}
func TestIsRequired(t *testing.T) {
s := "valid"

View file

@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test
import (
@ -14,6 +13,13 @@ import (
"github.com/cinar/checker"
)
func ExampleIsURL() {
result := checker.IsURL("https://zdo.com")
if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsURLValid(t *testing.T) {
result := checker.IsURL("https://zdo.com")
if result != checker.ResultValid {