Documentation
¶
Overview ¶
Package validate contains standalone functions that can be used for custom validation process.
Index ¶
- Variables
- func EAN13(value string) error
- func EAN8(value string) error
- func IP(value string, restrictions ...IPRestriction) error
- func IPv4(value string, restrictions ...IPRestriction) error
- func IPv6(value string, restrictions ...IPRestriction) error
- func UPCA(value string) error
- func UPCE(value string) error
- func URL(value string, schemas ...string) error
- type IPRestriction
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrOnlyZeros = errors.New("contains only zeros") ErrInvalidChecksum = errors.New("invalid checksum") ErrUnexpectedLength = errors.New("unexpected length") ErrContainsNonDigit = errors.New("contains non-digit") )
var ( ErrInvalid = errors.New("invalid") ErrProhibited = errors.New("prohibited") )
var ErrUnexpectedSchema = errors.New("unexpected schema")
Functions ¶
func EAN13 ¶ added in v0.8.0
EAN13 checks that string contains valid EAN-13 code.
If the value is not valid then one of the errors will be returned:
- ErrOnlyZeros if code contains only zeros;
- ErrInvalidChecksum if check digit is not valid;
- ErrUnexpectedLength if value length has an unexpected size;
- ErrContainsNonDigit if string contains non-digit value.
See https://en.wikipedia.org/wiki/International_Article_Number.
Example ¶
package main import ( "fmt" "github.com/muonsoft/validation/validate" ) func main() { fmt.Println(validate.EAN13("4719512002889")) fmt.Println(validate.EAN13("0000000000000")) fmt.Println(validate.EAN13("2266111566")) fmt.Println(validate.EAN13("A782868890061")) fmt.Println(validate.EAN13("4006381333932")) }
Output: <nil> contains only zeros unexpected length contains non-digit: 'A' invalid checksum
func EAN8 ¶ added in v0.8.0
EAN8 checks that string contains valid EAN-8 code.
If the value is not valid then one of the errors will be returned:
- ErrOnlyZeros if code contains only zeros;
- ErrInvalidChecksum if check digit is not valid;
- ErrUnexpectedLength if value length has an unexpected size;
- ErrContainsNonDigit if string contains non-digit value.
See https://en.wikipedia.org/wiki/EAN-8.
Example ¶
package main import ( "fmt" "github.com/muonsoft/validation/validate" ) func main() { fmt.Println(validate.EAN8("42345671")) fmt.Println(validate.EAN8("00000000")) fmt.Println(validate.EAN8("42345670")) fmt.Println(validate.EAN8("423456712")) fmt.Println(validate.EAN8("A4234671")) }
Output: <nil> contains only zeros invalid checksum unexpected length contains non-digit: 'A'
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 UPCA ¶ added in v0.8.0
UPCA checks that string contains valid UPC-A code.
If the value is not valid then one of the errors will be returned:
- ErrOnlyZeros if code contains only zeros;
- ErrInvalidChecksum if check digit is not valid;
- ErrUnexpectedLength if value length has an unexpected size;
- ErrContainsNonDigit if string contains non-digit value.
See https://en.wikipedia.org/wiki/Universal_Product_Code.
Example ¶
package main import ( "fmt" "github.com/muonsoft/validation/validate" ) func main() { fmt.Println(validate.UPCA("614141000036")) fmt.Println(validate.UPCA("000000000000")) fmt.Println(validate.UPCA("61414100003")) fmt.Println(validate.UPCA("A14141000036")) fmt.Println(validate.UPCA("614141000037")) }
Output: <nil> contains only zeros unexpected length contains non-digit: 'A' invalid checksum
func UPCE ¶ added in v0.8.0
UPCE checks that string contains valid UPC-E code.
If the value is not valid then one of the errors will be returned:
- ErrOnlyZeros if code contains only zeros;
- ErrInvalid if 8-digits code starts with number not equal to 0;
- ErrInvalidChecksum if check digit is not valid;
- ErrUnexpectedLength if value length has an unexpected size;
- ErrContainsNonDigit if string contains non-digit value.
See https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E.
Example ¶
package main import ( "fmt" "github.com/muonsoft/validation/validate" ) func main() { fmt.Println(validate.UPCE("123456")) // 6-digit is always valid fmt.Println(validate.UPCE("1234505")) // 7-digit with last check digit fmt.Println(validate.UPCE("01234505")) // 8-digit with first zero and last check digit fmt.Println(validate.UPCE("00000000")) fmt.Println(validate.UPCE("11234505")) fmt.Println(validate.UPCE("01234501")) fmt.Println(validate.UPCE("023456731")) fmt.Println(validate.UPCE("A2345673")) fmt.Println(validate.UPCE("12345")) }
Output: <nil> <nil> <nil> contains only zeros invalid invalid checksum unexpected length contains non-digit: 'A' unexpected length
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 a 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).