validate

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: MIT Imports: 8 Imported by: 2

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 (
	ErrTooShort               = errors.New("too short")
	ErrTooLong                = errors.New("too long")
	ErrTooLarge               = errors.New("too large")
	ErrInvalidCharacters      = errors.New("invalid characters")
	ErrInvalidHyphenPlacement = errors.New("invalid hyphen placement")
	ErrInvalidVersion         = errors.New("invalid version")
	ErrIsNil                  = errors.New("is nil")
)
View Source
var (
	ErrRestrictedSchema = errors.New("restricted schema")
	ErrRestrictedHost   = errors.New("restricted host")
)

Functions

func AllowNonCanonicalUUIDFormats added in v0.17.0

func AllowNonCanonicalUUIDFormats() func(o *UUIDOptions)

AllowNonCanonicalUUIDFormats used to enable parsing UUID value from non-canonical formats.

Following formats are supported:

  • "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
  • "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
  • "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  • "6ba7b8109dad11d180b400c04fd430c8"
  • "{6ba7b8109dad11d180b400c04fd430c8}",
  • "urn:uuid:6ba7b8109dad11d180b400c04fd430c8".

func AllowUUIDVersions added in v0.17.0

func AllowUUIDVersions(versions ...byte) func(o *UUIDOptions)

AllowUUIDVersions used to set valid versions of the UUID value. If the versions are empty, the UUID will be checked for compliance with the default registered versions: 1, 2, 3, 4, 5, 6 or 7.

func DenyNilUUID added in v0.17.0

func DenyNilUUID() func(o *UUIDOptions)

DenyNilUUID used to treat nil UUID ("00000000-0000-0000-0000-000000000000") value as invalid.

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 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:

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

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:

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 ...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:

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:

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:

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 ULID added in v0.17.0

func ULID(value string) error

ULID validates whether the value is a valid ULID (Universally Unique Lexicographically Sortable Identifier). See https://github.com/ulid/spec for ULID specifications.

Possible errors:

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(validate.ULID("01ARZ3NDEKTSV4RRFFQ69G5FAV"))
	fmt.Println(validate.ULID("01ARZ3NDEKTSV4RRFFQ69G5FA"))
	fmt.Println(validate.ULID("01ARZ3NDEKTSV4RRFFQ69G5FAVA"))
	fmt.Println(validate.ULID("01ARZ3NDEKTSV4RRFFQ69G5FAO"))
	fmt.Println(validate.ULID("81ARZ3NDEKTSV4RRFFQ69G5FAV"))
}
Output:

<nil>
too short
too long
invalid characters
too large

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:

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:

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 callable 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:

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>

func UUID added in v0.17.0

func UUID(value string, options ...func(o *UUIDOptions)) error

UUID validates whether a string value is a valid UUID (also known as GUID).

By default, it uses strict mode and checks the UUID as specified in RFC 4122. To parse additional formats, use the AllowNonCanonicalUUIDFormats option.

In addition, it checks if the UUID version matches one of the registered versions: 1, 2, 3, 4, 5, 6 or 7. Use AllowUUIDVersions to validate for a specific set of versions.

Nil UUID ("00000000-0000-0000-0000-000000000000") values are considered as valid. Use DenyNilUUID to disallow nil value.

Possible errors:

See http://tools.ietf.org/html/rfc4122.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(validate.UUID("83eab6fd-230b-44fe-b52f-463387bd8788"))                                      // v4
	fmt.Println(validate.UUID("83eab6fd-230b-44fe-b52f-463387bd8788", validate.AllowUUIDVersions(4)))       // v4
	fmt.Println(validate.UUID("00000000-0000-0000-0000-000000000000"))                                      // nil UUID
	fmt.Println(validate.UUID("00000000-0000-0000-0000-000000000000", validate.DenyNilUUID()))              // deny nil UUID
	fmt.Println(validate.UUID("x3eab6fd-230b-44fe-b52f-463387bd8788"))                                      // invalid
	fmt.Println(validate.UUID("216fff40-98d9-f1e3-a5e2-0800200c9a66"))                                      // invalid version
	fmt.Println(validate.UUID("216fff4098d911e3a5e20800200c9a66"))                                          // non-canonical
	fmt.Println(validate.UUID("216fff4098d911e3a5e20800200c9a66", validate.AllowNonCanonicalUUIDFormats())) // non-canonical
}
Output:

<nil>
<nil>
<nil>
is nil
invalid
invalid version
too short
<nil>

Types

type UUIDOptions added in v0.17.0

type UUIDOptions struct {
	// contains filtered or unexported fields
}

UUIDOptions are used to set up validation process of the UUID function.

Jump to

Keyboard shortcuts

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