Add IsExcluded method and ExclusionChecker support to IP validators

This commit is contained in:
2026-08-12 16:27:09 +05:00
parent 797dac73a0
commit 6f395d1a3b
+24 -2
View File
@@ -20,7 +20,11 @@ type IPValidator interface {
// IsValid checks if the given IP address is valid. // IsValid checks if the given IP address is valid.
IsValid(ip string) bool IsValid(ip string) bool
// IsValidAndReturnVersion validates the given IP address and returns whether it is valid and its version (IPv4 or IPv6).
IsValidAndReturnVersion(ip string) (bool, IPVersion) IsValidAndReturnVersion(ip string) (bool, IPVersion)
// IsExcluded determines if the given IP address is excluded based on certain criteria and returns related IP addresses.
IsExcluded(ip string) (excluded bool, ips []string, err error)
} }
type IPVersion int type IPVersion int
@@ -35,7 +39,9 @@ type IPs []string
// DefaultIPValidator implements IPValidator interface. // DefaultIPValidator implements IPValidator interface.
// It validates IP addresses by parsing them using net.ParseIP and net.ParseCIDR. // It validates IP addresses by parsing them using net.ParseIP and net.ParseCIDR.
type DefaultIPValidator struct{} type DefaultIPValidator struct {
ExclusionChecker ExclusionChecker
}
// IsValid checks if the given IP address is valid. // IsValid checks if the given IP address is valid.
// It returns true if the IP address is not a loopback address. // It returns true if the IP address is not a loopback address.
@@ -103,9 +109,18 @@ func (v *DefaultIPValidator) IsValidAndReturnVersion(value string) (bool, IPVers
return false, IPVersion4 return false, IPVersion4
} }
func (v *DefaultIPValidator) IsExcluded(ip string) (excluded bool, ips []string, err error) {
if v.ExclusionChecker == nil {
return false, nil, nil
}
return v.ExclusionChecker.IsExcluded(ip)
}
// IPRangeValidator implements IPValidator interface. // IPRangeValidator implements IPValidator interface.
// It validates IP ranges by parsing them using net.ParseIP and checking if the start and end IPs are in the same network. // It validates IP ranges by parsing them using net.ParseIP and checking if the start and end IPs are in the same network.
type IPRangeValidator struct{} type IPRangeValidator struct {
ExclusionChecker ExclusionChecker
}
// IsValid checks if the given IP range is valid. // IsValid checks if the given IP range is valid.
// It returns true if the start and end IPs are in the same network. // It returns true if the start and end IPs are in the same network.
@@ -182,3 +197,10 @@ func (v *IPRangeValidator) IsValidAndReturnVersion(value string) (bool, IPVersio
return false, IPVersion4 return false, IPVersion4
} }
} }
func (v *IPRangeValidator) IsExcluded(ip string) (excluded bool, ips []string, err error) {
if v.ExclusionChecker == nil {
return false, nil, nil
}
return v.ExclusionChecker.IsExcluded(ip)
}