isvalid

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2021 License: MIT Imports: 16 Imported by: 0

README

godoc pkg.go.dev

WORK IN PROGRESS

isvalid

The package isvalid defines a number of validation functions and other objects that can be used, together with the cmd/isvalid tool, to generate static input validation.


Some (most) validators, including their tests & comments, were ported from https://github.com/validatorjs/validator.js

TODO:
  • Currency
  • Decimal
  • EIN
  • IC
  • PassportNumber
  • Phone
  • URL
  • VAT
  • ZIP

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CurrencyOptsDefault = CurrencyOpts{
	NeedSym: false,
}
View Source
var StrongPasswordOptsDefault = StrongPasswordOpts{
	MinLen:     8,
	MinLower:   1,
	MinUpper:   1,
	MinNumbers: 1,
	MinSymbols: 1,
}

Functions

func ASCII

func ASCII(v string) bool

ASCII reports whether or not v is an ASCII string.

isvalid:rule
{
	"name": "ascii",
	"err": { "text": "must contain only ASCII characters" }
}

func Alnum

func Alnum(v string, lang string) bool

Alnum reports whether or not v is a valid alphanumeric string.

isvalid:rule
{
	"name": "alnum",
	"opts": [[ { "key": null, "value": "en" } ]],
	"err": { "text": "must be an alphanumeric string" }
}

func Alpha

func Alpha(v string, lang string) bool

Alpha reports whether or not v is a valid alphabetic string.

isvalid:rule
{
	"name": "alpha",
	"opts": [[ { "key": null, "value": "en" } ]],
	"err": { "text": "must be an alphabetic string" }
}

func BIC

func BIC(v string) bool

BIC reports whether or not v represents a valid Bank Identification Code or SWIFT code.

isvalid:rule
{
	"name": "bic",
	"err": { "text": "must be a valid BIC or SWIFT code" }
}

func BTC

func BTC(v string) bool

BTC reports whether or not v represents a valid BTC address.

isvalid:rule
{
	"name": "btc",
	"err": { "text": "must be a valid BTC address" }
}

func Base32

func Base32(v string) bool

Base32 reports whether or not v is a valid base32 string.

isvalid:rule
{
	"name": "base32",
	"err": { "text": "must be a valid base32 string" }
}

func Base58

func Base58(v string) bool

Base58 reports whether or not v is a valid base58 string.

isvalid:rule
{
	"name": "base58",
	"err": { "text": "must be a valid base58 string" }
}

func Base64

func Base64(v string, urlsafe bool) bool

Base64 reports whether or not v is a valid base64 string. NOTE: The standard "encoding/base64" package is used for validation. With urlsafe=false StdEncoding is used and with urlsafe=true RawURLEncoding is used.

isvalid:rule
{
	"name": "base64",
	"opts": [[
		{ "key": null, "value": "false"	},
		{ "key": "url", "value": "true" }
	]],
	"err": { "text": "must be a valid base64 string" }
}

func Binary

func Binary(v string) bool

Binary reports whether or not v represents a valid binary integer.

isvalid:rule
{
	"name": "binary",
	"err": { "text": "string content must match a binary number" }
}

func Bool

func Bool(v string) bool

Bool reports whether or not v represents a valid boolean. The following are considered valid boolean values: "true", "false", "TRUE", and "FALSE".

isvalid:rule
{
	"name": "bool",
	"err": { "text": "string content must match a boolean value" }
}

func CIDR

func CIDR(v string) bool

CIDR reports whether or not v is a valid Classless Inter-Domain Routing notation. NOTE: CIDR uses "net".ParseCIDR to determine the validity of v.

isvalid:rule
{
	"name": "cidr",
	"err": { "text": "must be a valid CIDR notation" }
}

func CVV

func CVV(v string) bool

CVV reports whether or not v is a valid Card Verification Value.

isvalid:rule
{
	"name": "cvv",
	"err": { "text": "must be a valid CVV" }
}

func Currency

func Currency(v string, code string, opts *CurrencyOpts) bool

Currency reports whether or not v represents a valid Currency amount.

isvalid:rule
{
	"name": "ccy",
	"opts": [[
		{ "key": null, "value": "usd" }
	], [
		{ "key": null, "value": "nil" }
	]],
	"err": { "text": "must be a valid currency amount" }
}

func DataURI

func DataURI(v string) bool

DataURI reports whether or not v is a valid data URI.

isvalid:rule
{
	"name": "datauri",
	"err": { "text": "must be a valid data URI" }
}

func Decimal

func Decimal(v string, locale string) bool

Decimal reports whether or not v represents a valid decimal number.

isvalid:rule
{
	"name": "decimal",
	"opts": [[
		{ "key": null, "value": "en" }
	]],
	"err": { "text": "string content must match a decimal number" }
}

func Digits

func Digits(v string) bool

Digits reports whether or not v is a string of digits.

isvalid:rule
{
	"name": "digits",
	"err": { "text": "must contain only digits" }
}

func EAN

func EAN(v string) bool

EAN reports whether or not v is a valid European Article Number.

isvalid:rule
{
	"name": "ean",
	"err": { "text": "must be a valid EAN" }
}

func EIN

func EIN(v string) bool

EIN reports whether or not v is a valid Employer Identification Number.

isvalid:rule
{
	"name": "ein",
	"err": { "text": "must be a valid EIN" }
}

func ETH

func ETH(v string) bool

ETH reports whether or not v is a valid ethereum address.

isvalid:rule
{
	"name": "eth",
	"err": { "text": "must be a valid ethereum address" }
}

func Email

func Email(v string) bool

Email reports whether or not v is a valid email address. NOTE: Email uses "net/mail".ParseAddress to determine the validity of v.

isvalid:rule
{
	"name": "email",
	"err": { "text": "must be a valid email address" }
}

func FQDN

func FQDN(v string) bool

FQDN reports whether or not v is a valid Fully Qualified Domain Name. NOTE: FQDN TLD is required, numeric TLDs or trailing dots are disallowed, and underscores are forbidden.

isvalid:rule
{
	"name": "fqdn",
	"err": { "text": "must be a valid FQDN" }
}

func Float

func Float(v string) bool

Float reports whether or not v represents a valid float.

isvalid:rule
{
	"name": "float",
	"err": { "text": "string content must match a floating point number" }
}

func HSL

func HSL(v string) bool

HSL reports whether or not v represents an HSL color value.

isvalid:rule
{
	"name": "hsl",
	"err": { "text": "must be a valid HSL color" }
}

func Hash

func Hash(v string, algo string) bool

Hash reports whether or not v is a hash of the specified algorithm.

isvalid:rule
{
	"name": "hash",
	"err": { "text": "must be a valid hash" }
}

func Hex

func Hex(v string) bool

Hex reports whether or not v is a valid hexadecimal string.

isvalid:rule
{
	"name": "hex",
	"err": { "text": "must be a valid hexadecimal string" }
}

func HexColor

func HexColor(v string) bool

HexColor reports whether or not v is a valid hexadecimal color code.

isvalid:rule
{
	"name": "hexcolor",
	"err": { "text": "must represent a valid hexadecimal color code" }
}

func IBAN

func IBAN(v string) bool

IBAN reports whether or not v is an International Bank Account Number.

isvalid:rule
{
	"name": "iban",
	"err": { "text": "must be a valid IBAN" }
}

func IC

func IC(v string) bool

IC reports whether or not v is an Identity Card number.

isvalid:rule
{
	"name": "ic",
	"err": { "text": "must be a valid identity card number" }
}

func IMEI

func IMEI(v string) bool

IMEI reports whether or not v is an IMEI number.

isvalid:rule
{
	"name": "imei",
	"err": { "text": "must be a valid IMEI number" }
}

func IP

func IP(v string, ver int) bool

IP reports whether or not v is a valid IP address. The ver argument specifies the IP's expected version. The ver argument can be one of the following three values:

0 accepts both IPv4 and IPv6
4 accepts IPv4 only
6 accepts IPv6 only

---

isvalid:rule
{
	"name": "ip",
	"opts": [[
		{ "key": null, "value": "0" },
		{ "key": "v4", "value": "4" },
		{ "key": "v6", "value": "6" }
	]],
	"err": { "text": "must be a valid IP" }
}

func IPRange

func IPRange(v string) bool

IPRange reports whether or not v is a valid IP range (IPv4 only).

isvalid:rule
{
	"name": "iprange",
	"err": { "text": "must be a valid IP range" }
}

func ISBN

func ISBN(v string, ver int) bool

ISBN reports whether or not v is a valid International Standard Book Number. The ver argument specifies the ISBN's expected version. The ver argument can be one of the following three values:

0 accepts both 10 and 13
10 accepts version 10 only
13 accepts version 13 only

---

isvalid:rule
{
	"name": "isbn",
	"opts": [[ { "key": null, "value": "0" } ]],
	"err": { "text": "must be a valid ISBN" }
}

func ISIN

func ISIN(v string) bool

ISIN reports whether or not v is a valid International Securities Identification Number.

isvalid:rule
{
	"name": "isin",
	"err": { "text": "must be a valid ISIN" }
}

func ISO31661A

func ISO31661A(v string, num int) bool

ISO31661A reports whether or not v is a valid country code as defined by the ISO 3166-1 Alpha standard. The num argument specifies which of the two alpha sets of the standard should be tested. The num argument can be one of the following three values:

0 tests against both Alpha-2 and Alpha-3
2 tests against Alpha-2 only
3 tests against Alpha-3 only

---

isvalid:rule
{
	"name": "iso31661a",
	"opts": [[ { "key": null, "value": "0" } ]],
	"err": { "text": "must be a valid ISO 3166-1 Alpha value" }
}

func ISO4217

func ISO4217(v string) bool

ISO4217 reports whether or not v is a valid currency code as defined by the ISO 4217 standard.

isvalid:rule
{
	"name": "iso4217",
	"err": { "text": "must be a valid ISO 4217 value" }
}

func ISO639

func ISO639(v string, num int) bool

ISO639 reports whether or not v is a valid language code as defined by the ISO 639 set of standards. Currently only standards 639-1 & 639-2 are supported. The num argument specifies which of the supported standards should be tested. The num argument can be one of the following three values:

0 tests against both 639-1 & 639-2
1 tests against 639-1 only
2 tests against 639-2 only

---

isvalid:rule
{
	"name": "iso369",
	"opts": [[ { "key": null, "value": "0" } ]],
	"err": { "text": "must be a valid ISO 639 value" }
}

func ISRC

func ISRC(v string) bool

ISRC reports whether or not v is a valid International Standard Recording Code.

isvalid:rule
{
	"name": "isrc",
	"err": { "text": "must be a valid ISRC" }
}

func ISSN

func ISSN(v string, requireHyphen, caseSensitive bool) bool

ISSN reports whether or not v is a valid International Standard Serial Number.

isvalid:rule
{
	"name": "issn",
	"err": { "text": "must be a valid ISSN" }
}

func In

func In(v interface{}, list ...interface{}) bool

In reports whether or not v is in the provided list.

isvalid:rule
{
	"name": "in",
	"err": { "text": "must be in the list" }
}

func Int

func Int(v string) bool

Int reports whether or not v represents a valid integer.

isvalid:rule
{
	"name": "int",
	"err": { "text": "string content must match an integer" }
}

func JSON

func JSON(v []byte) bool

JSON reports whether or not v is a valid JSON value. NOTE: the input is validated using json.Unmarshal which accepts primitive values as long as they are JSON values.

isvalid:rule
{
	"name": "json",
	"err": { "text": "must be a valid JSON" }
}

func JWT

func JWT(v string) bool

JWT reports whether or not v is a valid JSON Web Token.

isvalid:rule
{
	"name": "jwt",
	"err": { "text": "must be a valid JWT" }
}

func LatLong

func LatLong(v string, dms bool) bool

LatLong reports whether or not v is a valid latitude-longitude coordinate string.

isvalid:rule
{
	"name": "latlong",
	"opts": [[
		{ "key": null, "value": "false" },
		{ "key": "dms", "value": "true" }
	]],
	"err": { "text": "must be a valid latitude-longitude coordinate" }
}

func Locale

func Locale(v string) bool

Locale reports whether or not v is a valid locale.

isvalid:rule
{
	"name": "locale",
	"err": { "text": "must be a valid locale" }
}

func LowerCase

func LowerCase(v string) bool

LowerCase reports whether or not v is an all lower-case string.

isvalid:rule
{
	"name": "lower",
	"err": { "text": "must contain only lower-case characters" }
}

func MAC

func MAC(v string, space int) bool

MAC reports whether or not v is a valid MAC address. The space argument specifies the identifier's expected address space (in bytes). The space argument can be one of the following three values:

0 accepts both EUI-48 and EUI-64
6 accepts EUI-48 format only
8 accepts EUI-64 format only

The allowed formatting of the identifiers is as follows:

// EUI-48 format
"08:00:2b:01:02:03"
"08-00-2b-01-02-03"
"08002b010203"

// EUI-64 format
"08:00:2b:01:02:03:04:05"
"08-00-2b-01-02-03-04-05"
"08002b0102030405"

---

isvalid:rule
{
	"name": "mac",
	"opts": [[ { "key": null, "value": "0" } ]],
	"err": { "text": "must be a valid MAC" }
}

func MD5

func MD5(v string) bool

MD5 reports whether or not v is a valid MD5 hash.

isvalid:rule
{
	"name": "md5",
	"err": { "text": "must be a valid MD5 hash" }
}

func MIME

func MIME(v string) bool

MIME reports whether or not v is of a valid MIME type format.

NOTE: This function only checks is the string format follows the etablished rules by the according RFC specifications. This function supports 'charset' in textual media types (https://tools.ietf.org/html/rfc6657).

This function does not check against all the media types listed by the IANA (https://www.iana.org/assignments/media-types/media-types.xhtml)

More informations in the RFC specifications:

---

isvalid:rule
{
	"name": "mime",
	"err": { "text": "must be a valid media type" }
}

func MagnetURI

func MagnetURI(v string) bool

MagnetURI reports whether or not v is a valid magned URI.

isvalid:rule
{
	"name": "magneturi",
	"err": { "text": "must be a valid magnet URI" }
}

func Match

func Match(v string, re string) bool

Match reports whether or not v contains any match of the *registered* regular expression re. NOTE: re MUST be registered upfront with RegisterRegexp otherwise Match will return false even if v matches the regular expression.

isvalid:rule
{
	"name": "re",
	"err": {
		"text": "must match the regular expression",
		"with_opts": true
	}
}

func MongoId

func MongoId(v string) bool

MongoId reports whether or not v is a valid hex-encoded representation of a MongoDB ObjectId.

isvalid:rule
{
	"name": "mongoid",
	"err": { "text": "must be a valid Mongo Object Id" }
}

func Numeric

func Numeric(v string) bool

Numeric reports whether or not v is a valid numeric string.

isvalid:rule
{
	"name": "numeric",
	"err": { "text": "string content must match a numeric value" }
}

func Octal

func Octal(v string) bool

Octal reports whether or not v represents a valid octal integer.

isvalid:rule
{
	"name": "octal",
	"err": { "text": "string content must match an octal number" }
}

func PAN

func PAN(v string) bool

PAN reports whether or not v is a valid Primary Account Number or Credit Card number.

isvalid:rule
{
	"name": "pan",
	"err": { "text": "must be a valid PAN" }
}

func PassportNumber

func PassportNumber(v string) bool

PassportNumber reports whether or not v is a valid passport number.

isvalid:rule
{
	"name": "passport",
	"err": { "text": "must be a valid passport number" }
}

func Phone

func Phone(v string, cc string) bool

Phone reports whether or not v is a valid phone number in the country identified by the given country code cc.

isvalid:rule
{
	"name": "phone",
	"opts": [[ { "key": null, "value": "us" } ]],
	"err": { "text": "must be a valid phone number" }
}

func Port

func Port(v string) bool

Port reports whether or not v is a valid port number.

isvalid:rule
{
	"name": "port",
	"err": { "text": "must be a valid port number" }
}

func RGB

func RGB(v string) bool

RGB reports whether or not v is a valid RGB color.

isvalid:rule
{
	"name": "rgb",
	"err": { "text": "must be a valid RGB color" }
}

func RegisterRegexp

func RegisterRegexp(expr string)

RegisterRegexp compiles the given expression and caches the result. The given expr is assumed to be a valid regular expression, if it's not then RegisterRegexp will panic.

func SSN

func SSN(v string) bool

SSN reports whether or not v has a valid Social Security Number format.

isvalid:rule
{
	"name": "ssn",
	"err": { "text": "must be a valid SSN" }
}

func SemVer

func SemVer(v string) bool

SemVer reports whether or not v is a valid Semantic Versioning number. For reference: https://semver.org/

isvalid:rule
{
	"name": "semver",
	"err": { "text": "must be a valid semver number" }
}

func Slug

func Slug(v string) bool

Slug reports whether or not v is a valid slug.

isvalid:rule
{
	"name": "slug",
	"err": { "text": "must be a valid slug" }
}

func StrongPassword

func StrongPassword(v string, opts *StrongPasswordOpts) bool

StrongPassword reports whether or not v is a strong password.

isvalid:rule
{
	"name": "strongpass",
	"opts": [[ { "key": null, "value": "nil" } ]],
	"err": { "text": "must be a strong password" }
}

func URL

func URL(v string) bool

URL reports whether or not v is a valid Uniform Resource Locator.

isvalid:rule
{
	"name": "url",
	"err": { "text": "must be a valid URL" }
}

func UUID

func UUID(v string, ver int) bool

UUID reports whether or not v is a valid Universally Unique IDentifier. NOTE: only versions 3, 4, and 5 are currently supported.

isvalid:rule
{
	"name": "uuid",
	"opts": [[ { "key": null, "value": "4" } ]],
	"err": { "text": "must be a valid UUID" }
}

func Uint

func Uint(v string) bool

Uint reports whether or not v represents a valid unsigned integer.

isvalid:rule
{
	"name": "uint",
	"err": { "text": "string content must match an unsigned integer" }
}

func UpperCase

func UpperCase(v string) bool

UpperCase reports whether or not v is an all upper-case string.

isvalid:rule
{
	"name": "upper",
	"err": { "text": "must contain only upper-case characters" }
}

func VAT

func VAT(v string, cc string) bool

VAT reports whether or not v is a valid Value Added Tax number.

isvalid:rule
{
	"name": "vat",
	"err": { "text": "must be a valid VAT number" }
}

func Zip

func Zip(v string, cc string) bool

Zip reports whether or not v is a valid zip / postal code for the country identified by the given country code cc.

isvalid:rule
{
	"name": "zip",
	"opts": [[ { "key": null, "value": "us" } ]],
	"err": { "text": "must be a valid zip code" }
}

Types

type CurrencyOpts

type CurrencyOpts struct {
	NeedSym bool
}

type StrongPasswordOpts

type StrongPasswordOpts struct {
	MinLen     int
	MinLower   int
	MinUpper   int
	MinNumbers int
	MinSymbols int
}

Directories

Path Synopsis
cmd
isvalid command
isvalid is a tool for generating struct field validation.
isvalid is a tool for generating struct field validation.
internal
cldr/gen command
search
package search is used to find targets for the generator.
package search is used to find targets for the generator.
l10n

Jump to

Keyboard shortcuts

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