validate

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2022 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package validate contains standalone functions that can be used for custom validation process.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrOnlyZeros        = errors.New("contains only zeros")
	ErrInvalidChecksum  = errors.New("invalid checksum")
	ErrUnexpectedLength = errors.New("unexpected length")
	ErrContainsNonDigit = errors.New("contains non-digit")
)
View Source
var (
	ErrInvalid    = errors.New("invalid")
	ErrProhibited = errors.New("prohibited")
)
View Source
var (
	ErrRestrictedSchema = errors.New("restricted schema")
	ErrRestrictedHost   = errors.New("restricted host")
)

Functions

func DenyPrivateIP

func DenyPrivateIP() func(ip net.IP) error

DenyPrivateIP denies using of private IPs according to RFC 1918 (IPv4 addresses) and RFC 4193 (IPv6 addresses).

func EAN8 added in v0.8.0

func EAN8(value string) error

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 EAN13 added in v0.8.0

func EAN13(value string) error

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 IP

func IP(value string, restrictions ...func(ip net.IP) error) 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 ...func(ip net.IP) error) 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 ...func(ip net.IP) error) 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 RestrictURLHostByPattern added in v0.14.0

func RestrictURLHostByPattern(pattern *regexp.Regexp) func(u *url.URL) error

RestrictURLHostByPattern make URL validation accepts only a value with a host matching by pattern.

func RestrictURLHosts added in v0.14.0

func RestrictURLHosts(hosts ...string) func(u *url.URL) error

RestrictURLHosts make URL validation accepts only the listed hosts.

func RestrictURLSchemas added in v0.14.0

func RestrictURLSchemas(schemas ...string) func(u *url.URL) error

RestrictURLSchemas make URL validation accepts only the listed schemas.

func UPCA added in v0.8.0

func UPCA(value string) error

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

func UPCE(value string) error

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

func URL(value string, restrictions ...func(u *url.URL) error) error

URL is used to validate that value is a valid URL string. You can use a list of restrictions to additionally check for a restricted set of URLs. By default, if no restrictions are passed, the function checks for the http:// and https:// schemas.

Use the RestrictURLSchemas option 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").

Use the RestrictURLHosts or RestrictURLHostByPattern option to configure the list of allowed hosts.

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;
  • ErrRestrictedSchema if schema is not matching one of the listed schemas;
  • ErrRestrictedHost if host is not matching one of the listed hosts;
  • ErrInvalid if value is not matching the regular expression.
Example
package main

import (
	"fmt"
	"regexp"

	"github.com/muonsoft/validation/validate"
)

func main() {
	fmt.Println(validate.URL("https://example.com"))                                                    // valid absolute URL
	fmt.Println(validate.URL("ftp://example.com", validate.RestrictURLSchemas("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", validate.RestrictURLSchemas("")))                         // valid relative URL
	fmt.Println(validate.URL("http://example.com", validate.RestrictURLHosts("sample.com")))            // not matching host
	fmt.Println(                                                                                        // matching by regexp
		validate.URL(
			"http://sub.example.com",
			validate.RestrictURLHostByPattern(regexp.MustCompile(`^.*\.example\.com$`)),
		),
	)
}
Output:
<nil>
<nil>
restricted schema
parse "http:// example.com/": invalid character " " in host name
<nil>
restricted host
<nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL