-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IP, IPv4, IPv6 validation checkers and tests to v2. (#136)
# Describe Request Add IP, IPv4, IPv6 validation checkers and tests to v2. # Change Type New code.
- Loading branch information
Showing
7 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 | ||
|
||
package v2 | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
) | ||
|
||
const ( | ||
// nameIP is the name of the IP check. | ||
nameIP = "ip" | ||
) | ||
|
||
var ( | ||
// ErrNotIP indicates that the given value is not a valid IP address. | ||
ErrNotIP = NewCheckError("IP") | ||
) | ||
|
||
// IsIP checks if the value is a valid IP address. | ||
func IsIP(value string) (string, error) { | ||
if net.ParseIP(value) == nil { | ||
return value, ErrNotIP | ||
} | ||
return value, nil | ||
} | ||
|
||
// checkIP checks if the value is a valid IP address. | ||
func checkIP(value reflect.Value) (reflect.Value, error) { | ||
_, err := IsIP(value.Interface().(string)) | ||
return value, err | ||
} | ||
|
||
// makeIP makes a checker function for the IP checker. | ||
func makeIP(_ string) CheckFunc[reflect.Value] { | ||
return checkIP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 | ||
|
||
package v2_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
v2 "github.com/cinar/checker/v2" | ||
) | ||
|
||
func ExampleIsIP() { | ||
_, err := v2.IsIP("192.168.1.1") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func TestIsIPInvalid(t *testing.T) { | ||
_, err := v2.IsIP("invalid-ip") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestIsIPValid(t *testing.T) { | ||
_, err := v2.IsIP("192.168.1.1") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCheckIPNonString(t *testing.T) { | ||
defer FailIfNoPanic(t, "expected panic") | ||
|
||
type Network struct { | ||
Address int `checkers:"ip"` | ||
} | ||
|
||
network := &Network{} | ||
|
||
v2.CheckStruct(network) | ||
} | ||
|
||
func TestCheckIPInvalid(t *testing.T) { | ||
type Network struct { | ||
Address string `checkers:"ip"` | ||
} | ||
|
||
network := &Network{ | ||
Address: "invalid-ip", | ||
} | ||
|
||
_, ok := v2.CheckStruct(network) | ||
if ok { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestCheckIPValid(t *testing.T) { | ||
type Network struct { | ||
Address string `checkers:"ip"` | ||
} | ||
|
||
network := &Network{ | ||
Address: "192.168.1.1", | ||
} | ||
|
||
_, ok := v2.CheckStruct(network) | ||
if !ok { | ||
t.Fatal("expected valid") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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 | ||
|
||
package v2 | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
) | ||
|
||
const ( | ||
// nameIPv4 is the name of the IPv4 check. | ||
nameIPv4 = "ipv4" | ||
) | ||
|
||
var ( | ||
// ErrNotIPv4 indicates that the given value is not a valid IPv4 address. | ||
ErrNotIPv4 = NewCheckError("IPv4") | ||
) | ||
|
||
// IsIPv4 checks if the value is a valid IPv4 address. | ||
func IsIPv4(value string) (string, error) { | ||
ip := net.ParseIP(value) | ||
if ip == nil || ip.To4() == nil { | ||
return value, ErrNotIPv4 | ||
} | ||
return value, nil | ||
} | ||
|
||
// checkIPv4 checks if the value is a valid IPv4 address. | ||
func checkIPv4(value reflect.Value) (reflect.Value, error) { | ||
_, err := IsIPv4(value.Interface().(string)) | ||
return value, err | ||
} | ||
|
||
// makeIPv4 makes a checker function for the IPv4 checker. | ||
func makeIPv4(_ string) CheckFunc[reflect.Value] { | ||
return checkIPv4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 | ||
|
||
package v2_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
v2 "github.com/cinar/checker/v2" | ||
) | ||
|
||
func ExampleIsIPv4() { | ||
_, err := v2.IsIPv4("192.168.1.1") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func TestIsIPv4Invalid(t *testing.T) { | ||
_, err := v2.IsIPv4("2001:db8::1") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestIsIPv4Valid(t *testing.T) { | ||
_, err := v2.IsIPv4("192.168.1.1") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCheckIPv4NonString(t *testing.T) { | ||
defer FailIfNoPanic(t, "expected panic") | ||
|
||
type Network struct { | ||
Address int `checkers:"ipv4"` | ||
} | ||
|
||
network := &Network{} | ||
|
||
v2.CheckStruct(network) | ||
} | ||
|
||
func TestCheckIPv4Invalid(t *testing.T) { | ||
type Network struct { | ||
Address string `checkers:"ipv4"` | ||
} | ||
|
||
network := &Network{ | ||
Address: "2001:db8::1", | ||
} | ||
|
||
_, ok := v2.CheckStruct(network) | ||
if ok { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestCheckIPv4Valid(t *testing.T) { | ||
type Network struct { | ||
Address string `checkers:"ipv4"` | ||
} | ||
|
||
network := &Network{ | ||
Address: "192.168.1.1", | ||
} | ||
|
||
_, ok := v2.CheckStruct(network) | ||
if !ok { | ||
t.Fatal("expected valid") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 | ||
|
||
package v2 | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
) | ||
|
||
const ( | ||
// nameIPv6 is the name of the IPv6 check. | ||
nameIPv6 = "ipv6" | ||
) | ||
|
||
var ( | ||
// ErrNotIPv6 indicates that the given value is not a valid IPv6 address. | ||
ErrNotIPv6 = NewCheckError("IPv6") | ||
) | ||
|
||
// IsIPv6 checks if the value is a valid IPv6 address. | ||
func IsIPv6(value string) (string, error) { | ||
if net.ParseIP(value) == nil || net.ParseIP(value).To4() != nil { | ||
return value, ErrNotIPv6 | ||
} | ||
return value, nil | ||
} | ||
|
||
// checkIPv6 checks if the value is a valid IPv6 address. | ||
func checkIPv6(value reflect.Value) (reflect.Value, error) { | ||
_, err := IsIPv6(value.Interface().(string)) | ||
return value, err | ||
} | ||
|
||
// makeIPv6 makes a checker function for the IPv6 checker. | ||
func makeIPv6(_ string) CheckFunc[reflect.Value] { | ||
return checkIPv6 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 | ||
|
||
package v2_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
v2 "github.com/cinar/checker/v2" | ||
) | ||
|
||
func ExampleIsIPv6() { | ||
_, err := v2.IsIPv6("2001:db8::1") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func TestIsIPv6Invalid(t *testing.T) { | ||
_, err := v2.IsIPv6("192.168.1.1") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestIsIPv6Valid(t *testing.T) { | ||
_, err := v2.IsIPv6("2001:db8::1") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCheckIPv6NonString(t *testing.T) { | ||
defer FailIfNoPanic(t, "expected panic") | ||
|
||
type Network struct { | ||
Address int `checkers:"ipv6"` | ||
} | ||
|
||
network := &Network{} | ||
|
||
v2.CheckStruct(network) | ||
} | ||
|
||
func TestCheckIPv6Invalid(t *testing.T) { | ||
type Network struct { | ||
Address string `checkers:"ipv6"` | ||
} | ||
|
||
network := &Network{ | ||
Address: "192.168.1.1", | ||
} | ||
|
||
_, ok := v2.CheckStruct(network) | ||
if ok { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestCheckIPv6Valid(t *testing.T) { | ||
type Network struct { | ||
Address string `checkers:"ipv6"` | ||
} | ||
|
||
network := &Network{ | ||
Address: "2001:db8::1", | ||
} | ||
|
||
_, ok := v2.CheckStruct(network) | ||
if !ok { | ||
t.Fatal("expected valid") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters