Go library made for validating several form fields and common values, such as ASCII, Base64, CEP, CNPJ, CPF, credit card, email, real image bytes, MAC address, MD5, port, postal code, telephone, password, and much more.
This package is the Go version of Multiform Validator. It currently includes ASCII, Base64, CEP, CNPJ, CPF, credit card, email, image, MAC address, MD5, port, postal code, telephone, text, and higher-level validation helpers, with more validators being added over time. The CNPJ validator supports both the old LEGACY numeric CNPJ format and the new alphanumeric CNPJ format, in accordance with the official Receita Federal / SERPRO specification.
go get github.com/Multiform-Validator/goThis library is tested in CI with Go 1.20, 1.21, 1.22, 1.23, 1.24, 1.25, and 1.26.
Lint, build, race tests, and unit tests run in CI for all of those versions.
Security checks do not currently run in CI for Go 1.20, 1.21, or 1.22. The security job currently runs only for Go 1.23, 1.24, 1.25, and 1.26, so there is less security-check confidence for versions below 1.23.
Use Go versions below 1.23 only if you have no other choice. Go >= 1.23 is the recommended baseline.
When govulncheck is executed with Go 1.20, 1.21, or 1.22, it may report GO-2025-3750, a vulnerability in the Go standard library on Windows, in os / syscall, related to inconsistent handling of O_CREATE|O_EXCL. At a minimum, this can cause incorrect file-creation exclusivity behavior. In the observed CI output, this appeared as os@go1.22.12, with a fix noted in os@go1.23.10.
package main
import (
"fmt"
"os"
mv "github.com/Multiform-Validator/go"
"github.com/Multiform-Validator/go/validate"
)
func main() {
if err := mv.IsAscii("Hello 123!"); err != nil {
fmt.Println(err)
}
if err := mv.IsBase64("SGVsbG8gV29ybGQ="); err != nil {
fmt.Println(err)
}
if err := mv.IsCEP("12345-678"); err != nil {
fmt.Println(err)
}
if err := mv.IsCNPJ("12.ABC.345/01DE-35"); err != nil {
fmt.Println(err)
}
if err := mv.IsCNPJ("04.252.011/0001-10"); err != nil {
fmt.Println(err)
}
if err := mv.IsCPF("123.456.789-09"); err != nil {
fmt.Println(err)
}
fmt.Println(mv.IdentifyFlagCard("4111 1111 1111 1111"))
if err := mv.IsCreditCard("4111 1111 1111 1111"); err != nil {
fmt.Println(err)
}
fmt.Println(mv.GetOnlyEmail("Contact team: joao@empresa.com, maria@empresa.com"))
if err := mv.IsEmail("user@example.com"); err != nil {
fmt.Println(err)
}
imageBytes, err := os.ReadFile("avatar.png")
if err != nil {
fmt.Println(err)
}
if err := mv.IsImage(imageBytes); err != nil {
fmt.Println(err)
}
if err := mv.IsMACAddress("00:1A:2B:3C:4D:5E"); err != nil {
fmt.Println(err)
}
if err := mv.IsPort("8080"); err != nil {
fmt.Println(err)
}
if err := mv.IsPortNumber(8080); err != nil {
fmt.Println(err)
}
if err := mv.IsPostalCode("10045-123", "BR"); err != nil {
fmt.Println(err)
}
if err := mv.IsTelephone("+55 11 91234-5678", "BR"); err != nil {
fmt.Println(err)
}
if err := mv.IsBlank(" "); err != nil {
fmt.Println(err)
}
if err := mv.IsEmpty(""); err != nil {
fmt.Println(err)
}
if err := validate.Email("user@gmail.com", validate.EmailOptions{ValidDomains: true}); err != nil {
fmt.Println(err)
}
if err := validate.Password("MyP@ssw0rd", validate.PasswordOptions{
MinLength: 8,
MaxLength: 20,
RequireUppercase: true,
RequireSpecialChar: true,
RequireNumber: true,
RequireLetter: true,
}); err != nil {
fmt.Println(err)
}
}You can also import each validator package directly:
import (
"github.com/Multiform-Validator/go/ascii"
"github.com/Multiform-Validator/go/base64"
"github.com/Multiform-Validator/go/cep"
"github.com/Multiform-Validator/go/cnpj"
"github.com/Multiform-Validator/go/cpf"
"github.com/Multiform-Validator/go/creditcard"
"github.com/Multiform-Validator/go/email"
"github.com/Multiform-Validator/go/image"
"github.com/Multiform-Validator/go/macaddress"
"github.com/Multiform-Validator/go/md5"
"github.com/Multiform-Validator/go/port"
"github.com/Multiform-Validator/go/postalcode"
"github.com/Multiform-Validator/go/telephone"
"github.com/Multiform-Validator/go/text"
"github.com/Multiform-Validator/go/validate"
)Most validators follow the direct Is* style, such as IsEmail, IsCPF, and IsPort. These are simple checks for one specific value.
The validate package is intentionally separate and works a little differently. It is for higher-level validators with options and composed rules, using the validate.* style, such as validate.Email and validate.Password.
IsAsciiIsAsciiBytesIsBase64IsCEPCalculateCNPJCheckDigitsIsCNPJIsCPFIdentifyFlagCardIsCreditCardGetOnlyEmailGetOnlyEmailsIsEmailIsImageIsMACAddressIsMD5IsPortIsPortNumberIsPostalCodeIsTelephoneIsBlankIsBlankBytesIsEmptyIsEmptyBytesvalidate.Emailvalidate.Password
IsTelephone accepts an optional country argument. Current country-specific validation supports Brazil, United States, China, Japan, Germany, India, United Kingdom, France, Italy, Canada, and South Korea.
IsPostalCode accepts an optional country argument. Current country-specific validation supports Brazil, United States, Canada, United Kingdom, France, Netherlands, Japan, Spain, South Africa, Germany, Switzerland, and Italy.
IsImage validates real image bytes. It currently accepts PNG, JPEG, GIF, and ICO files. Other image-like formats, such as SVG, AVIF, PSD, and ICNS, are not considered valid by this validator.
IdentifyFlagCard is a special helper from the credit card package, similar to how GetOnlyEmail belongs to the email package. It identifies the card flag by prefix and returns Unknown when no known flag matches.
validate.Email provides higher-level email validation options inspired by the TypeScript package: max length, country suffix, default allowed domains, and custom allowed domains.
validate.Password validates password length and optional uppercase, special character, number, and letter requirements.
make checkCoverage is enforced in CI and currently must remain at 100.0%.
Basic contribution guidelines are available in CONTRIBUTING.md.