-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipv6.go
40 lines (33 loc) · 955 Bytes
/
ipv6.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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("NOT_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
}