Documentation
¶
Overview ¶
Package validate contains standalone functions that can be used for custom validation process.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalid = errors.New("invalid") ErrProhibited = errors.New("prohibited") )
var (
ErrUnexpectedSchema = errors.New("unexpected schema")
)
Functions ¶
func IP ¶
func IP(value string, restrictions ...IPRestriction) error
IP validates that a value is a valid IP address (IPv4 or IPv6). You can use a list of restrictions to additionally check for a restricted range of IPs. For example, you can deny using private IP addresses using DenyPrivateIP function.
If value is not valid the function will return one of the errors:
- ErrInvalid on invalid IP address;
- ErrProhibited on restricted IP address.
Example ¶
package main import ( "fmt" "github.com/muonsoft/validation/validate" ) func main() { fmt.Println(validate.IP("123.123.123.123")) // valid IPv4 fmt.Println(validate.IP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) // valid IPv6 fmt.Println(validate.IP("123.123.123.345")) // invalid fmt.Println(validate.IP("192.168.1.0")) // non-restricted private IP fmt.Println(validate.IP("192.168.1.0", validate.DenyPrivateIP())) // restricted private IP }
Output: <nil> <nil> invalid <nil> prohibited
func IPv4 ¶
func IPv4(value string, restrictions ...IPRestriction) error
IPv4 validates that a value is a valid IPv4 address. You can use a list of restrictions to additionally check for a restricted range of IPs. For example, you can deny using private IP addresses using DenyPrivateIP function.
If value is not valid the function will return one of the errors:
- ErrInvalid on invalid IP address or when using IPv6;
- ErrProhibited on restricted IP address.
Example ¶
package main import ( "fmt" "github.com/muonsoft/validation/validate" ) func main() { fmt.Println(validate.IPv4("123.123.123.123")) // valid IPv4 fmt.Println(validate.IPv4("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) // invalid (IPv6) fmt.Println(validate.IPv4("123.123.123.345")) // invalid fmt.Println(validate.IPv4("192.168.1.0")) // non-restricted private IP fmt.Println(validate.IPv4("192.168.1.0", validate.DenyPrivateIP())) // restricted private IP }
Output: <nil> invalid invalid <nil> prohibited
func IPv6 ¶
func IPv6(value string, restrictions ...IPRestriction) error
IPv6 validates that a value is a valid IPv6 address. You can use a list of restrictions to additionally check for a restricted range of IPs. For example, you can deny using private IP addresses using DenyPrivateIP function.
If value is not valid the function will return one of the errors:
- ErrInvalid on invalid IP address or when using IPv4;
- ErrProhibited on restricted IP address.
Example ¶
package main import ( "fmt" "github.com/muonsoft/validation/validate" ) func main() { fmt.Println(validate.IPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) // valid (IPv6) fmt.Println(validate.IPv6("123.123.123.123")) // invalid IPv4 fmt.Println(validate.IPv6("z001:0db8:85a3:0000:0000:8a2e:0370:7334")) // invalid fmt.Println(validate.IPv6("fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c")) // non-restricted private IP fmt.Println(validate.IPv6("fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c", validate.DenyPrivateIP())) // restricted private IP }
Output: <nil> invalid invalid <nil> prohibited
func URL ¶
URL is used to validate that value is a valid URL string. By default (if no schemas are passed), the function checks only for the http:// and https:// schemas. Use the schemas argument to configure the list of expected schemas. If an empty string is passed as a schema, then URL value may be treated as relative (without schema, e.g. "//example.com").
If value is not a valid URL the function will return one of the errors:
- parsing error from url.Parse method if value cannot be parsed as an URL;
- ErrUnexpectedSchema if schema is not matching one of the listed schemas;
- ErrInvalid if value is not matching the regular expression.
Example ¶
package main import ( "fmt" "github.com/muonsoft/validation/validate" ) func main() { fmt.Println(validate.URL("https://example.com")) // valid absolute URL fmt.Println(validate.URL("ftp://example.com", "http", "https", "ftp")) // valid URL with custom schema fmt.Println(validate.URL("example.com")) // url without schema fmt.Println(validate.URL("http:// example.com/")) // invalid URL fmt.Println(validate.URL("//example.com", "")) // valid relative URL }
Output: <nil> <nil> unexpected schema parse "http:// example.com/": invalid character " " in host name <nil>
Types ¶
type IPRestriction ¶
IPRestriction can be used to limit valid IP address values.
func DenyPrivateIP ¶
func DenyPrivateIP() IPRestriction
DenyPrivateIP denies using of private IPs according to RFC 1918 (IPv4 addresses) and RFC 4193 (IPv6 addresses).