Documentation
¶
Overview ¶
Package validatorjs is a standalone port of the popular npm "validatorjs" (and the closely related "validator.js") string validation library to idiomatic Go. It depends only on the Go standard library and does not import any part of the express package, so it can be used from any program that needs to answer simple "is this string a valid X?" questions.
Reach for this package whenever you have a string of unknown provenance - form input, a query parameter, a config value, a webhook payload - and you need a cheap, allocation-light predicate that tells you whether it is a valid e-mail, URL, UUID, credit-card number, IP address, and so on. Every exported function takes a single string and returns a bool: true when the input satisfies the rule and false otherwise. There are no error returns and no panics on ordinary input, which makes the functions convenient to compose inside larger validation logic or to pass around as func(string) bool values.
Under the hood most checks are implemented with precompiled package-level regular expressions (for example hexColorRe, slugRe, semVerRe, and the alpha/alphanumeric/numeric/int/float matchers), which keeps them fast and free of per-call compilation cost. A few rules use hand-written logic where a regexp would be awkward or wrong: IsEmail splits on the last "@" and separately bounds and validates the local part and the domain labels; IsURL parses with net/url and then checks the scheme and host; IsIPv4/IsIPv6 defer to net.ParseIP; IsJSON round-trips through encoding/json; and IsCreditCard strips separators, matches a known card-prefix pattern, and then verifies the Luhn checksum. Rune-aware rules such as IsLength and IsStrongPassword count runes with unicode/utf8 rather than bytes.
The individual rules carry deliberate semantics and edge cases worth knowing. IsEmail enforces RFC-like length limits (254 overall, 64 for the local part, 253 for the domain) and rejects leading, trailing, or doubled dots in the local part; IsURL accepts only http, https, ftp, and ftps schemes and requires a non-empty host (an IP, "localhost", or a dotted domain); IsUUID accepts any version in the canonical 8-4-4-4-12 hex layout; IsBase64 requires a length that is a multiple of four; IsInt forbids superfluous leading zeros while IsNumeric does not; IsStrongPassword requires at least eight characters with a lowercase letter, an uppercase letter, a digit, and a symbol; and IsLength treats a negative max as "no upper bound". Unlike the npm library, these functions are the whole surface: this port does not expose validatorjs rule strings like "required|email|min:6" or pipe-delimited rule sets, nor the Validator object, its messages, or its option objects.
Parity with the Node originals is close but not bit-for-bit. The goal is to match the practical acceptance and rejection behavior of validator.js's most commonly used validators for typical inputs, using the same algorithms (Luhn, semver.org's recommended regexp, the standard card prefixes) where it matters. It differs in that it does not implement locale/option arguments (for example IsMobilePhone is a generic "+ and 7-15 digits" check rather than a per-locale matcher, and IsEmail has no display-name or allow-IP options), and it omits the large catalog of niche validators and all of the sanitizers that validator.js ships. For express request/body/query validation with chainable per-field rules, see the sibling validator package instead.
Index ¶
- func Contains(s, substr string) bool
- func IsAlpha(s string) bool
- func IsAlphanumeric(s string) bool
- func IsBase64(s string) bool
- func IsCreditCard(s string) bool
- func IsEmail(s string) bool
- func IsFloat(s string) bool
- func IsHexColor(s string) bool
- func IsIP(s string) bool
- func IsIPv4(s string) bool
- func IsIPv6(s string) bool
- func IsInt(s string) bool
- func IsJSON(s string) bool
- func IsJWT(s string) bool
- func IsLength(s string, min, max int) bool
- func IsMobilePhone(s string) bool
- func IsMongoId(s string) bool
- func IsNumeric(s string) bool
- func IsSemVer(s string) bool
- func IsSlug(s string) bool
- func IsStrongPassword(s string) bool
- func IsURL(s string) bool
- func IsUUID(s string) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAlphanumeric ¶
IsAlphanumeric reports whether s is non-empty and contains only ASCII letters and digits.
func IsCreditCard ¶
IsCreditCard reports whether s is a valid credit card number. It passes the Luhn checksum and matches a known card prefix (Visa, MasterCard, American Express, Diners Club, Discover, or JCB).
Example ¶
ExampleIsCreditCard exercises the Luhn-plus-prefix credit-card check. The function strips spaces and hyphens, confirms the digits match a known card prefix (Visa, MasterCard, Amex, Diners, Discover, or JCB), and then verifies the Luhn checksum. The first number is a classic Visa test number that both matches the prefix and passes the checksum. The second alters a digit so the checksum fails. Both boolean results are shown below.
package main
import (
"fmt"
"github.com/malcolmston/express/validatorjs"
)
func main() {
fmt.Println(validatorjs.IsCreditCard("4111 1111 1111 1111"))
fmt.Println(validatorjs.IsCreditCard("4111 1111 1111 1112"))
}
Output: true false
func IsEmail ¶
IsEmail reports whether s is a valid e-mail address. It accepts a local part followed by "@" and a domain that contains at least one dot and a top-level domain of two or more letters.
Example ¶
ExampleIsEmail shows the e-mail predicate accepting a well-formed address and rejecting a malformed one. IsEmail requires a local part, a single "@", and a domain with at least one dot and an alphabetic top-level domain of two or more letters. The first input satisfies all of those requirements and returns true. The second input has no domain dot and no "@" structure at all, so it returns false. The Output block records both boolean results.
package main
import (
"fmt"
"github.com/malcolmston/express/validatorjs"
)
func main() {
fmt.Println(validatorjs.IsEmail("ada@example.com"))
fmt.Println(validatorjs.IsEmail("not-an-email"))
}
Output: true false
func IsFloat ¶
IsFloat reports whether s is a valid floating-point number, allowing an optional sign, a decimal point, and scientific notation.
func IsHexColor ¶
IsHexColor reports whether s is a valid hexadecimal color, requiring a leading "#" and either three or six hexadecimal digits.
func IsInt ¶
IsInt reports whether s is a valid integer with an optional leading sign and no superfluous leading zeros.
Example ¶
ExampleIsInt highlights the strict integer rule and its treatment of leading zeros. IsInt allows an optional leading sign but forbids superfluous leading zeros, so "42" and "-7" are valid while "007" is not. This is deliberately stricter than IsNumeric, which would accept the zero-padded form. The example checks three inputs to make the distinction visible. The Output block lists the results in order.
package main
import (
"fmt"
"github.com/malcolmston/express/validatorjs"
)
func main() {
fmt.Println(validatorjs.IsInt("42"))
fmt.Println(validatorjs.IsInt("-7"))
fmt.Println(validatorjs.IsInt("007"))
}
Output: true true false
func IsJWT ¶
IsJWT reports whether s is a well-formed JSON Web Token: three non-empty base64url-encoded segments separated by dots.
func IsLength ¶
IsLength reports whether the number of runes in s is between min and max inclusive. A negative max means there is no upper bound.
Example ¶
ExampleIsLength measures string length in runes rather than bytes and honors an inclusive minimum and maximum. Passing a negative max removes the upper bound entirely. Here a five-rune string is checked against a 1-to-10 window and against a lower bound of 6 with no upper bound. The first check passes and the second fails because five is below the minimum. The example prints both boolean answers.
package main
import (
"fmt"
"github.com/malcolmston/express/validatorjs"
)
func main() {
fmt.Println(validatorjs.IsLength("hello", 1, 10))
fmt.Println(validatorjs.IsLength("hello", 6, -1))
}
Output: true false
func IsMobilePhone ¶
IsMobilePhone reports whether s looks like a mobile phone number: an optional leading "+" followed by 7 to 15 digits.
func IsMongoId ¶
IsMongoId reports whether s is a valid MongoDB ObjectId: exactly 24 hexadecimal characters.
func IsSemVer ¶
IsSemVer reports whether s is a valid Semantic Versioning 2.0.0 version string as defined at https://semver.org.
func IsSlug ¶
IsSlug reports whether s is a valid slug: lowercase alphanumeric words separated by single hyphens, with no leading or trailing hyphen.
func IsStrongPassword ¶
IsStrongPassword reports whether s is a strong password: at least eight characters including at least one lowercase letter, one uppercase letter, one digit, and one symbol.
Example ¶
ExampleIsStrongPassword checks the composite strength rule. IsStrongPassword requires at least eight characters and demands the presence of a lowercase letter, an uppercase letter, a digit, and a symbol. The first password meets every requirement and returns true. The second is all lowercase letters and therefore fails several of the character-class checks. Both results are printed so the passing and failing cases are clear.
package main
import (
"fmt"
"github.com/malcolmston/express/validatorjs"
)
func main() {
fmt.Println(validatorjs.IsStrongPassword("Passw0rd!"))
fmt.Println(validatorjs.IsStrongPassword("password"))
}
Output: true false
func IsURL ¶
IsURL reports whether s is a valid URL with an http, https, ftp, or ftps scheme and a host component.
Example ¶
ExampleIsURL demonstrates URL validation across an accepted scheme and a rejected one. IsURL parses the string with net/url and requires one of the http, https, ftp, or ftps schemes together with a non-empty host. The HTTPS address with a dotted domain and a path is accepted. A "javascript:" pseudo URL has an unsupported scheme and is rejected. The two results are printed in order.
package main
import (
"fmt"
"github.com/malcolmston/express/validatorjs"
)
func main() {
fmt.Println(validatorjs.IsURL("https://example.com/path?q=1"))
fmt.Println(validatorjs.IsURL("javascript:alert(1)"))
}
Output: true false
func IsUUID ¶
IsUUID reports whether s is a valid UUID of any version (1-5), matching the canonical 8-4-4-4-12 hexadecimal format.
Example ¶
ExampleIsUUID validates a canonical UUID against a truncated string. IsUUID accepts any version in the standard 8-4-4-4-12 hexadecimal layout without checking the version or variant nibbles. The first value is a properly shaped UUID and returns true. The second value is too short to match the pattern and returns false. The example prints both outcomes so the behavior is explicit.
package main
import (
"fmt"
"github.com/malcolmston/express/validatorjs"
)
func main() {
fmt.Println(validatorjs.IsUUID("123e4567-e89b-12d3-a456-426614174000"))
fmt.Println(validatorjs.IsUUID("123e4567-e89b"))
}
Output: true false
Types ¶
This section is empty.