Documentation
¶
Overview ¶
Package is contains standalone functions that can be used for custom validation process.
Index ¶
- func DivisibleBy(divisible, divisor float64) bool
- func EAN13(value string) bool
- func EAN8(value string) bool
- func Email(value string) bool
- func HTML5Email(value string) bool
- func Hostname(value string) bool
- func IP(value string, restrictions ...func(ip net.IP) error) bool
- func IPv4(value string, restrictions ...func(ip net.IP) error) bool
- func IPv6(value string, restrictions ...func(ip net.IP) error) bool
- func InList[T comparable](value T, list []T) bool
- func Integer(s string) bool
- func JSON(value string) bool
- func Number(s string) bool
- func StrictHostname(value string) bool
- func ULID(value string) bool
- func UPCA(value string) bool
- func UPCE(value string) bool
- func URL(value string, restrictions ...func(u *url.URL) error) bool
- func UUID(value string, options ...func(o *validate.UUIDOptions)) bool
- func Unique[T comparable](values []T) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DivisibleBy ¶ added in v0.17.0
DivisibleBy checks that a value is divisible by another value (divisor). It checks that the remainder is zero or almost zero with an error of 1e-12.
func EAN13 ¶ added in v0.8.0
EAN13 checks that string contains valid EAN-13 code.
See https://en.wikipedia.org/wiki/International_Article_Number.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.EAN13("4719512002889"))
fmt.Println(is.EAN13("9782868890061"))
fmt.Println(is.EAN13("0000000000000")) // zeros only are prohibited
fmt.Println(is.EAN13("4006381333932")) // invalid checksum
fmt.Println(is.EAN13("A782868890061")) // contains non-digit
}
Output: true true false false false
func EAN8 ¶ added in v0.8.0
EAN8 checks that string contains valid EAN-8 code.
See https://en.wikipedia.org/wiki/EAN-8.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.EAN8("42345671"))
fmt.Println(is.EAN8("47195127"))
fmt.Println(is.EAN8("00000000")) // zeros only are prohibited
fmt.Println(is.EAN8("42345670")) // invalid checksum
fmt.Println(is.EAN8("A4234671")) // contains non-digit
}
Output: true true false false false
func Email ¶
Email is used for simplified validation of an email address. It allows all values with an "@" symbol in, and a "." in the second host part of the email address.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.Email("user@example.com")) // valid
fmt.Println(is.Email("{}~!@example.com")) // valid
fmt.Println(is.Email("пользователь@example.com")) // valid
fmt.Println(is.Email("user example.com")) // invalid
}
Output: true true true false
func HTML5Email ¶
HTML5Email is used for validation of an email address based on pattern for HTML5 (see https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address).
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.HTML5Email("user@example.com")) // valid
fmt.Println(is.HTML5Email("{}~!@example.com")) // valid
fmt.Println(is.HTML5Email("пользователь@example.com")) // invalid
fmt.Println(is.HTML5Email("user example.com")) // invalid
}
Output: true true false false
func Hostname ¶
Hostname checks that a value is a valid hostname. It checks that each label within a valid hostname may be no more than 63 octets long. Also, it checks that the total length of the hostname must not exceed 255 characters.
See StrictHostname for additional checks.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.Hostname("example.com")) // valid
fmt.Println(is.Hostname("example.localhost")) // valid
fmt.Println(is.Hostname("com")) // valid
fmt.Println(is.Hostname("example-.com")) // invalid
}
Output: true true true false
func IP ¶
IP checks 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.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
"github.com/muonsoft/validation/validate"
)
func main() {
fmt.Println(is.IP("123.123.123.123")) // valid IPv4
fmt.Println(is.IP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) // valid IPv6
fmt.Println(is.IP("123.123.123.345")) // invalid
fmt.Println(is.IP("192.168.1.0")) // non-restricted private IP
fmt.Println(is.IP("192.168.1.0", validate.DenyPrivateIP())) // restricted private IP
}
Output: true true false true false
func IPv4 ¶
IPv4 checks that a value is a valid IPv4 address. You can use a list of restrictions to additionally check for a restricted range of IPs.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
"github.com/muonsoft/validation/validate"
)
func main() {
fmt.Println(is.IPv4("123.123.123.123")) // valid IPv4
fmt.Println(is.IPv4("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) // invalid (IPv6)
fmt.Println(is.IPv4("123.123.123.345")) // invalid
fmt.Println(is.IPv4("192.168.1.0")) // non-restricted private IP
fmt.Println(is.IPv4("192.168.1.0", validate.DenyPrivateIP())) // restricted private IP
}
Output: true false false true false
func IPv6 ¶
IPv6 checks that a value is a valid IPv6 address. You can use a list of restrictions to additionally check for a restricted range of IPs.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
"github.com/muonsoft/validation/validate"
)
func main() {
fmt.Println(is.IPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) // valid (IPv6)
fmt.Println(is.IPv6("123.123.123.123")) // invalid IPv4
fmt.Println(is.IPv6("z001:0db8:85a3:0000:0000:8a2e:0370:7334")) // invalid
fmt.Println(is.IPv6("fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c")) // non-restricted private IP
fmt.Println(is.IPv6("fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c", validate.DenyPrivateIP())) // restricted private IP
}
Output: true false false true false
func InList ¶ added in v0.9.0
func InList[T comparable](value T, list []T) bool
InList returns true if one of the elements of the list is equal to the value.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.InList("foo", nil))
fmt.Println(is.InList("foo", []string{"bar", "baz"}))
fmt.Println(is.InList("foo", []string{"bar", "baz", "foo"}))
fmt.Println(is.InList(2, []int{1, 2, 3}))
}
Output: false false true true
func Integer ¶ added in v0.5.1
Integer checks that string is an integer.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.Integer("123"))
fmt.Println(is.Integer("123.123"))
fmt.Println(is.Integer("-123"))
fmt.Println(is.Integer("123foo"))
}
Output: true false true false
func JSON ¶
JSON checks that value is a valid JSON string.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.JSON(`{"valid": true}`)) // valid
fmt.Println(is.JSON(`"invalid": true`)) // invalid
}
Output: true false
func Number ¶ added in v0.5.1
Number checks that string is a number (a float or an integer).
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.Number("123"))
fmt.Println(is.Number("123.123"))
fmt.Println(is.Number("123e123"))
fmt.Println(is.Number("-123"))
fmt.Println(is.Number("123foo"))
}
Output: true true true true false
func StrictHostname ¶
StrictHostname checks that a value is a valid hostname. Beside checks from Hostname function it checks that hostname is fully qualified and include its top-level domain name (TLD). For instance, example.com is valid but example is not.
Additionally it checks for reserved top-level domains according to RFC 2606 and that's why hostnames containing them are not considered valid: .example, .invalid, .localhost, and .test.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.StrictHostname("example.com")) // valid
fmt.Println(is.StrictHostname("example.localhost")) // reserved
fmt.Println(is.StrictHostname("com")) // invalid
fmt.Println(is.StrictHostname("example-.com")) // invalid
}
Output: true false false false
func ULID ¶ added in v0.17.0
ULID validates whether the value is a valid ULID (Universally Unique Lexicographically Sortable Identifier). See https://github.com/ulid/spec for ULID specifications.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.ULID("01ARZ3NDEKTSV4RRFFQ69G5FAV"))
fmt.Println(is.ULID("01ARZ3NDEKTSV4RRFFQ69G5FA")) // too short
fmt.Println(is.ULID("01ARZ3NDEKTSV4RRFFQ69G5FAVA")) // too long
fmt.Println(is.ULID("01ARZ3NDEKTSV4RRFFQ69G5FAO")) // invalid characters
fmt.Println(is.ULID("81ARZ3NDEKTSV4RRFFQ69G5FAV")) // too large
}
Output: true false false false false
func UPCA ¶ added in v0.8.0
UPCA checks that string contains valid UPC-A code.
See https://en.wikipedia.org/wiki/Universal_Product_Code.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.UPCA("614141000036"))
fmt.Println(is.UPCA("123456789999"))
fmt.Println(is.UPCA("000000000000")) // zeros only are prohibited
fmt.Println(is.UPCA("614141000037")) // invalid checksum
fmt.Println(is.UPCA("A14141000036")) // contains non-digit
}
Output: true true false false false
func UPCE ¶ added in v0.8.0
UPCE checks that string contains valid UPC-E code.
See https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.UPCE("123456")) // 6-digit is always valid
fmt.Println(is.UPCE("1234505")) // 7-digit with last check digit
fmt.Println(is.UPCE("01234505")) // 8-digit with first zero and last check digit
fmt.Println(is.UPCE("00000000")) // zeros only are prohibited
fmt.Println(is.UPCE("11234505")) // non-zero number system is prohibited
fmt.Println(is.UPCE("01234501")) // invalid checksum
fmt.Println(is.UPCE("A2345673")) // contains non-digit
fmt.Println(is.UPCE("12345")) // invalid length
}
Output: true true true false false false false false
func URL ¶
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 github.com/muonsoft/validation/validate.RestrictURLHosts or github.com/muonsoft/validation/validate.RestrictURLHostByPattern option to configure the list of allowed hosts.
Example ¶
package main
import (
"fmt"
"regexp"
"github.com/muonsoft/validation/is"
"github.com/muonsoft/validation/validate"
)
func main() {
fmt.Println(is.URL("https://example.com")) // valid absolute URL
fmt.Println(is.URL("ftp://example.com", validate.RestrictURLSchemas("http", "https", "ftp"))) // valid URL with custom schema
fmt.Println(is.URL("example.com")) // invalid URL
fmt.Println(is.URL("//example.com", validate.RestrictURLSchemas(""))) // valid relative URL
fmt.Println(is.URL("http://example.com", validate.RestrictURLHosts("sample.com"))) // not matching host
fmt.Println( // matching by regexp
is.URL(
"http://sub.example.com",
validate.RestrictURLHostByPattern(regexp.MustCompile(`^.*\.example\.com$`)),
),
)
}
Output: true true false true false true
func UUID ¶ added in v0.17.0
func UUID(value string, options ...func(o *validate.UUIDOptions)) bool
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 github.com/muonsoft/validation/validate.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 github.com/muonsoft/validation/validate.AllowUUIDVersions to validate for a specific set of versions.
Nil UUID ("00000000-0000-0000-0000-000000000000") values are considered as valid. Use github.com/muonsoft/validation/validate.DenyNilUUID to disallow nil value.
See http://tools.ietf.org/html/rfc4122.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
"github.com/muonsoft/validation/validate"
)
func main() {
fmt.Println(is.UUID("83eab6fd-230b-44fe-b52f-463387bd8788")) // v4
fmt.Println(is.UUID("83eab6fd-230b-44fe-b52f-463387bd8788", validate.AllowUUIDVersions(4))) // v4
fmt.Println(is.UUID("00000000-0000-0000-0000-000000000000")) // nil UUID
fmt.Println(is.UUID("00000000-0000-0000-0000-000000000000", validate.DenyNilUUID())) // deny nil UUID
fmt.Println(is.UUID("x3eab6fd-230b-44fe-b52f-463387bd8788")) // invalid
fmt.Println(is.UUID("216fff40-98d9-f1e3-a5e2-0800200c9a66")) // invalid version
fmt.Println(is.UUID("216fff4098d911e3a5e20800200c9a66")) // non-canonical
fmt.Println(is.UUID("216fff4098d911e3a5e20800200c9a66", validate.AllowNonCanonicalUUIDFormats())) // non-canonical
}
Output: true true true false false false false true
func Unique ¶ added in v0.9.0
func Unique[T comparable](values []T) bool
Unique checks that the slice of comparable values are unique.
Example ¶
package main
import (
"fmt"
"github.com/muonsoft/validation/is"
)
func main() {
fmt.Println(is.Unique([]string{}))
fmt.Println(is.Unique([]string{"one", "two", "three"}))
fmt.Println(is.Unique([]string{"one", "two", "one"}))
fmt.Println(is.Unique([]int{1, 2, 1}))
}
Output: true true false false
Types ¶
This section is empty.