is

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EAN8 added in v0.8.0

func EAN8(value string) bool

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

func EAN13(value string) bool

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 Email

func Email(value string) bool

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

func HTML5Email(value string) bool

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

func Hostname(value string) bool

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

func IP(value string, restrictions ...validate.IPRestriction) bool

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. See validate.IPRestriction for details.

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

func IPv4(value string, restrictions ...validate.IPRestriction) bool

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. See validate.IPRestriction for details.

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

func IPv6(value string, restrictions ...validate.IPRestriction) bool

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. See validate.IPRestriction for details.

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

func Integer(s string) bool

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

func JSON(value string) bool

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

func Number(s string) bool

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

func StrictHostname(value string) bool

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

func UPCA(value string) bool

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

func UPCE(value string) bool

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

func URL(value string, schemas ...string) bool

URL is used to check 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").

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(is.URL("https://example.com"))                       // valid absolute URL
	fmt.Println(is.URL("ftp://example.com", "http", "https", "ftp")) // valid URL with custom schema
	fmt.Println(is.URL("example.com"))                               // invalid URL
	fmt.Println(is.URL("//example.com", ""))                         // valid relative URL
}
Output:
true
true
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.

Jump to

Keyboard shortcuts

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