/go-validator

[Go] Package of validators and support multi languages

Primary LanguageGoMIT LicenseMIT

go-validator

Travis Status Go Report Card GoDoc

A package of validators and sanitizers for strings, structs and collections.

features:

  • Customizable Attributes.
  • Customizable error messages.
  • Support i18n messages

Installation

Make sure that Go is installed on your computer. Type the following command in your terminal:

go get github.com/syssam/go-validator

Usage and documentation

Examples:

Available Validation Rules

  • omitempty
  • required
  • requiredIf
  • requiredUnless
  • requiredWith
  • requiredWithAll
  • requiredWithout
  • requiredWithoutAll
  • between
  • digitsBetween
  • size
  • max
  • min
  • same
  • gt
  • gte
  • lt
  • lte
  • distinct
  • email
  • alpha
  • alphaNum
  • alphaDash
  • alphaUnicode
  • alphaNumUnicode
  • alphaDashUnicode
  • numeric
  • int
  • integer
  • float
  • null
  • ip
  • ipv4
  • ipv6
  • uuid3
  • uuid4
  • uuid5
  • uuid

omitempty

The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.

required

The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:

  • The value is nil.
  • The value is an empty string.
  • The value is an empty array | map

requiredIf=anotherfield|value|...

The field under validation must be present and not empty if the anotherfield field is equal to any value.

requiredUnless=anotherfield|value|...

The field under validation must be present and not empty unless the anotherfield field is equal to any value.

requiredWith=anotherfield|anotherfield|...

The field under validation must be present and not empty only if any of the other specified fields are present.

requiredWithAll=anotherfield|anotherfield|...

The field under validation must be present and not empty only if all of the other specified fields are present.

requiredWithout=anotherfield|anotherfield|...

The field under validation must be present and not empty only when any of the other specified fields are not present.

requiredWithoutAll=anotherfield|anotherfield|...

The field under validation must be present and not empty only when all of the other specified fields are not present.

between=min|max

The field under validation must have a size between the given min and max. String, Number, Array, Map are evaluated in the same fashion as the size rule.

digitsBetween=min|max

The field under validation must have a length between the given min and max.

size=value

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For an array | map | slice, size corresponds to the count of the array | map | slice.

max=value

The field under validation must be less than or equal to a maximum value. String, Number, Array, Map are evaluated in the same fashion as the size rule.

min=value

The field under validation must be greater than or equal to a minimum value. String, Number, Array, Map are evaluated in the same fashion as the size rule.

same=anotherfield

The given field must match the field under validation.

gt=anotherfield

The field under validation must be greater than the given field. The two fields must be of the same type. String, Number, Array, Map are evaluated using the same conventions as the size rule.

gte=anotherfield

The field under validation must be greater than or equal to the given field. The two fields must be of the same type. String, Number, Array, Map are evaluated using the same conventions as the size rule.

lt=anotherfield

The field under validation must be less than the given field. The two fields must be of the same type. String, Number, Array, Map are evaluated using the same conventions as the size rule.

lte=anotherfield

The field under validation must be less than or equal to the given field. The two fields must be of the same type. String, Number, Array, Map are evaluated using the same conventions as the size rule.

distinct

The field under validation must not have any duplicate values.

email

The field under validation must be formatted as an e-mail address.

alpha

The field under validation may be only contains letters. Empty string is valid.

alphaNum

The field under validation may be only contains letters and numbers. Empty string is valid.

alphaDash

The field under validation may be only contains letters, numbers, dashes and underscores. Empty string is valid.

alphaUnicode

The field under validation may be only contains letters. Empty string is valid.

alphaNumUnicode

The field under validation may be only contains letters and numbers. Empty string is valid.

alphaDashUnicode

The field under validation may be only contains letters, numbers, dashes and underscores. Empty string is valid.

numeric

The field under validation must be numbers. Empty string is valid.

int

The field under validation must be int. Empty string is valid.

float

The field under validation must be float. Empty string is valid.

ip

The field under validation must be an IP address.

ipv4

The field under validation must be an IPv4 address.

ipv6

The field under validation must be an IPv6 address.

uuid3

The field under validation must be an uuid3.

uuid4

The field under validation must be an uuid4.

uuid5

The field under validation must be an uuid5.

uuid

The field under validation must be an uuid.

Custom Validation Rules

  validator.CustomTypeTagMap.Set("customValidator", func CustomValidator(v reflect.Value, o reflect.Value, validTag *validator.ValidTag) bool {
    return false
  })
  

List of functions:

    IsNumeric(str string) bool
    IsInt(str string) bool
    IsFloat(str string) bool
    IsNull(str string) bool
    ValidateBetween(i interface{}, params []string) bool
    ValidateDigitsBetween(i interface{}, params []string) bool
    ValidateDigitsBetweenInt64(value, left, right int64) bool
    ValidateDigitsBetweenFloat64(value, left, right float64) bool
    ValidateGt(i interface{}, a interface{}) bool
    ValidateGtFloat64(v, param float64) bool
    ValidateGte(i interface{}, a interface{}) bool
    ValidateGteFloat64(v, param float64) bool
    ValidateLt(i interface{}, a interface{}) bool
    ValidateLtFloat64(v, param float64) bool
    ValidateLte(i interface{}, a interface{}) bool
    ValidateLteFloat64(v, param float64) bool
    ValidateRequired(i interface{}) bool
    ValidateMin(i interface{}, params []string) bool
    ValidateMinFloat64(v, param float64) bool
    ValidateMax(i interface{}, params []string) bool
    ValidateMaxFloat64(v, param float64) bool
    ValidateSize(i interface{}, params []string) bool
    ValidateDistinct(i interface{}) bool
    ValidateEmail(str string) bool
    ValidateAlpha(str string) bool
    ValidateAlphaNum(str string) bool
    ValidateAlphaDash(str string) bool
    ValidateAlphaUnicode(str string) bool
    ValidateAlphaNumUnicode(str string) bool
    ValidateAlphaDashUnicode(str string) bool
    ValidateIP(str string) bool
    ValidateIPv4(str string) bool
    ValidateIPv6(str string) bool
    ValidateUUID3(str string) bool
    ValidateUUID4(str string) bool
    ValidateUUID5(str string) bool
    ValidateUUID(str string) bool
    ValidateURL(str string) bool