Documentation
¶
Overview ¶
Package is contains standalone functions that can be used for custom validation process.
Index ¶
- func Email(value string) bool
- func HTML5Email(value string) bool
- func Hostname(value string) bool
- func IP(value string, restrictions ...validate.IPRestriction) bool
- func IPv4(value string, restrictions ...validate.IPRestriction) bool
- func IPv6(value string, restrictions ...validate.IPRestriction) bool
- func Integer(s string) bool
- func JSON(value string) bool
- func Number(s string) bool
- func StrictHostname(value string) bool
- func StringInList(s string, list []string) bool
- func URL(value string, schemas ...string) bool
- func UniqueStrings(values []string) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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
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 StringInList ¶ added in v0.4.1
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 ¶
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
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.