is

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 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 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 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 StringInList added in v0.4.1

func StringInList(s string, list []string) bool

StringInList returns true if one of the elements of the list is equal to the string.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(is.StringInList("foo", nil))
	fmt.Println(is.StringInList("foo", []string{"bar", "baz"}))
	fmt.Println(is.StringInList("foo", []string{"bar", "baz", "foo"}))
}
Output:
false
false
true

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 UniqueStrings added in v0.4.0

func UniqueStrings(values []string) bool

UniqueStrings checks that slice of strings has unique values.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(is.UniqueStrings([]string{}))
	fmt.Println(is.UniqueStrings([]string{"one", "two", "three"}))
	fmt.Println(is.UniqueStrings([]string{"one", "two", "one"}))
}
Output:
true
true
false

Types

This section is empty.

Jump to

Keyboard shortcuts

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