Skip to content

Commit f37281f

Browse files
authored
Merge pull request #4 from eredotpkfr/new-validators
[Feat] - Luhn and Credit Card Validator
2 parents a5ae7e8 + d43e75d commit f37281f

File tree

16 files changed

+161
-31
lines changed

16 files changed

+161
-31
lines changed

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@
55
[![Release](https://img.shields.io/github/v/release/eredotpkfr/golidators)](https://github.com/eredotpkfr/golidators/releases/latest)
66
[![License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/eredotpkfr/golidators/blob/main/LICENSE)
77
[![Stars](https://img.shields.io/github/stars/eredotpkfr/golidators?style=social)](https://github.com/eredotpkfr/golidators/stargazers)
8+
89
# golidators
9-
Golidators is a golang package, it includes basic data validation functions and regexes.
10+
11+
Golidators is a golang package, it includes basic data validation functions and regexes
1012

1113
## Install
14+
1215
```bash
13-
$ go get github.com/eredotpkfr/golidators
16+
~$ go get github.com/eredotpkfr/golidators
1417
```
1518

1619
## Overview
20+
1721
Following validators available on this package:
22+
1823
- Domain
1924
- MD5, SHA1, SHA224, SHA256, SHA512
2025
- IPv4, IPv4CIDR, IPv6, IPv6CIDR
2126
- MAC
2227
- Port
2328
- URL
2429
- UUID
30+
- CreditCard/Luhn
2531

2632
## Usage
33+
2734
Just import and use it. Also see documentation at [pkg.go.dev](https://pkg.go.dev/github.com/eredotpkfr/golidators#section-documentation)
35+
2836
```go
2937
package main
3038

@@ -34,20 +42,28 @@ import (
3442
)
3543

3644
func main() {
45+
// Validate domain address
3746
fmt.Println(golidators.Domain("www.example.com"))
38-
// true
47+
// Validate IPv4 address
3948
fmt.Println(golidators.Ipv4("::1"))
40-
// false
49+
// Validate IPv6 address
4150
fmt.Println(golidators.Ipv6("::1"))
42-
// true
51+
// Validate URL
4352
fmt.Println(golidators.Url("https://www.example.com"))
44-
// true
53+
// Validate IPv4CIDR
4554
fmt.Println(golidators.Ipv4Cidr("127.0.0.1/12"))
46-
// true
55+
// Validate most common hashes
4756
fmt.Println(golidators.Md5("foo/bar"))
48-
// false
57+
// Validate with Luhn algorithm
58+
fmt.Println(golidators.Luhn(5300025108592596))
59+
// Calculate check digit with Luhn algorithm
60+
fmt.Println(golidators.LuhnCheckDigit(5146713835433))
61+
// Validate credit card number with Luhn
62+
fmt.Println(golidators.CreditCard(5184214431476070))
4963
}
5064
```
65+
5166
## Contact
67+
5268
Blog - [erdoganyoksul.com](https://www.erdoganyoksul.com)<br/>
5369

creditcard.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package golidators
2+
3+
// CreditCard validate credit card number with Luhn
4+
func CreditCard(number int) bool {
5+
return Luhn(number)
6+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/eredotpkfr/golidators
22

3-
go 1.17
3+
go 1.23
44

5-
require golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167
5+
require golang.org/x/net v0.33.0
66

7-
require golang.org/x/text v0.3.6 // indirect
7+
require golang.org/x/text v0.21.0 // indirect

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167 h1:eDd+TJqbgfXruGQ5sJRU7tEtp/58OAx4+Ayjxg4SM+4=
2-
golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
3-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
6-
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
7-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
8-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
1+
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
2+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
3+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
4+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=

internal/test/creditcard_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/eredotpkfr/golidators"
7+
)
8+
9+
var CreditCardCases = [10]IntTestRecord{
10+
{1234567890, false},
11+
{1234567897, true},
12+
{5146713835430, false},
13+
{5146713835433, true},
14+
{5371087585041475, true},
15+
{5300025108592596, true},
16+
{5302025202593516, false},
17+
{5184214431476070, true},
18+
{5371087585041475, true},
19+
{5101365588025704, true},
20+
}
21+
22+
func TestCreditCard(t *testing.T) {
23+
for _, record := range CreditCardCases {
24+
if golidators.CreditCard(record.TargetValue) != record.Expected {
25+
t.Error(record.TargetValue)
26+
}
27+
}
28+
}

internal/test/domain_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package test
22

33
import (
4-
"github.com/eredotpkfr/golidators"
54
"testing"
5+
6+
"github.com/eredotpkfr/golidators"
67
)
78

89
var DomainCases = [35]StrTestRecord{

internal/test/hashes_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package test
22

33
import (
4-
"github.com/eredotpkfr/golidators"
54
"testing"
5+
6+
"github.com/eredotpkfr/golidators"
67
)
78

89
var (

internal/test/ip_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package test
22

33
import (
4-
"github.com/eredotpkfr/golidators"
54
"testing"
5+
6+
"github.com/eredotpkfr/golidators"
67
)
78

89
var (

internal/test/luhn_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/eredotpkfr/golidators"
7+
)
8+
9+
var LuhnCases = [7]IntTestRecord{
10+
{1234567890, false},
11+
{1234567897, true},
12+
{5146713835430, false},
13+
{5146713835433, true},
14+
{5371087585041475, true},
15+
{5300025108592596, true},
16+
{5302025202593516, false},
17+
}
18+
19+
var LuhnCheckDigitCases = [7]IntTestRecordWithIntReturn{
20+
{1234567890, 3},
21+
{1234567897, 8},
22+
{5146713835430, 2},
23+
{5146713835433, 6},
24+
{5371087585041475, 1},
25+
{5300025108592596, 2},
26+
{5302025202593516, 6},
27+
}
28+
29+
func TestLuhn(t *testing.T) {
30+
for _, record := range LuhnCases {
31+
if golidators.Luhn(record.TargetValue) != record.Expected {
32+
t.Error(record.TargetValue)
33+
}
34+
}
35+
}
36+
37+
func TestLuhnCheckDigit(t *testing.T) {
38+
for _, record := range LuhnCheckDigitCases {
39+
if golidators.LuhnCheckDigit(record.TargetValue) != record.Expected {
40+
t.Error(record.TargetValue)
41+
}
42+
}
43+
}

internal/test/mac_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package test
22

33
import (
4-
"github.com/eredotpkfr/golidators"
54
"testing"
5+
6+
"github.com/eredotpkfr/golidators"
67
)
78

89
var MacCases = [7]StrTestRecord{

0 commit comments

Comments
 (0)