kikinteractive/govalidator

Name: govalidator

Owner: Kik Interactive

Description: [Go] Package of string validators and sanitizers for Go lang.

Created: 2015-05-31 15:07:03.0

Updated: 2017-05-21 17:29:49.0

Pushed: 2015-06-03 09:16:03.0

Homepage:

Size: 271

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

govalidator

Gitter GoDoc Coverage Status views wercker status Build Status

A package of string validators and sanitizers for Go lang. Based on validator.js.

Installation

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

go get github.com/asaskevich/govalidator

After it the package is ready to use.

Import package in your project

Add following line in your *.go file:

rt "github.com/asaskevich/govalidator"

If you unhappy to use long govalidator, you can do something like this:

rt (
valid "github.com/asaskevich/govalidator"

List of functions:
 BlackList(str, chars string) string
 CamelCaseToUnderscore(str string) string
 Contains(str, substring string) bool
 GetLine(s string, index int) (string, error)
 GetLines(s string) []string
 IsASCII(str string) bool
 IsAlpha(str string) bool
 IsAlphanumeric(str string) bool
 IsBase64(str string) bool
 IsByteLength(str string, min, max int) bool
 IsCreditCard(str string) bool
 IsDataURI(str string) bool
 IsDivisibleBy(str, num string) bool
 IsEmail(str string) bool
 IsFilePath(str string) (bool, int)
 IsFloat(str string) bool
 IsFullWidth(str string) bool
 IsHalfWidth(str string) bool
 IsHexadecimal(str string) bool
 IsHexcolor(str string) bool
 IsIP(str string) bool
 IsIPv4(str string) bool
 IsIPv6(str string) bool
 IsISBN(str string, version int) bool
 IsISBN10(str string) bool
 IsISBN13(str string) bool
 IsISO3166Alpha2(str string) bool
 IsISO3166Alpha3(str string) bool
 IsInt(str string) bool
 IsJSON(str string) bool
 IsLatitude(str string) bool
 IsLongitude(str string) bool
 IsLowerCase(str string) bool
 IsMAC(str string) bool
 IsMongoID(str string) bool
 IsMultibyte(str string) bool
 IsNull(str string) bool
 IsNumeric(str string) bool
 IsPrintableASCII(str string) bool
 IsRGBcolor(str string) bool
 IsRequestURI(rawurl string) bool
 IsRequestURL(rawurl string) bool
 IsSSN(str string) bool
 IsURL(str string) bool
 IsUTFDigit(str string) bool
 IsUTFLetter(str string) bool
 IsUTFLetterNumeric(str string) bool
 IsUTFNumeric(str string) bool
 IsUUID(str string) bool
 IsUUIDv3(str string) bool
 IsUUIDv4(str string) bool
 IsUUIDv5(str string) bool
 IsUpperCase(str string) bool
 IsVariableWidth(str string) bool
 LeftTrim(str, chars string) string
 Matches(str, pattern string) bool
 NormalizeEmail(str string) (string, error)
 RemoveTags(s string) string
 ReplacePattern(str, pattern, replace string) string
 Reverse(s string) string
 RightTrim(str, chars string) string
 SafeFileName(str string) string
 StripLow(str string, keepNewLines bool) string
 ToBoolean(str string) (bool, error)
 ToFloat(str string) (float64, error)
 ToInt(str string) (int64, error)
 ToString(obj interface{}) (string, error)
 Trim(str, chars string) string
 Truncate(str string, length int, ending string) string
 UnderscoreToCamelCase(s string) string
 ValidateStruct(s interface{}) (bool, error)
 WhiteList(str, chars string) string
 ISO3166Entry
 UnsupportedTypeError
func (e *UnsupportedTypeError) Error() string
 Validator
Examples IsURL
tln(govalidator.IsURL(`http://user@pass:domain.com/path/page`))
ToString
 User struct {
FirstName string
LastName string


 _ := govalidator.ToString(&User{"John", "Juan"})
tln(str)
ValidateStruct #2

If you want to validate structs, you can use tag valid for any field in your structure. All validators used with this field in one tag are separated by comma. If you want to skip validation, place - in your tag. If you need a validator that is not on the list below, you can add it like this:

lidator.TagMap["duck"] = govalidator.Validator(func(str string) bool {
return str == "duck"

Here is a list of available validators for struct fields (validator - used function):

il":          IsEmail,
":            IsURL,
url":         IsRequestURL,
uri":         IsRequestURI,
ha":          IsAlpha,
letter":      IsUTFLetter,
hanum":       IsAlphanumeric,
letternum":   IsUTFLetterNumeric,
eric":        IsNumeric,
numeric":     IsUTFNumeric,
digit":       IsUTFDigit,
adecimal":    IsHexadecimal,
color":       IsHexcolor,
color":       IsRGBcolor,
ercase":      IsLowerCase,
ercase":      IsUpperCase,
":            IsInt,
at":          IsFloat,
l":           IsNull,
d":           IsUUID,
dv3":         IsUUIDv3,
dv4":         IsUUIDv4,
dv5":         IsUUIDv5,
ditcard":     IsCreditCard,
n10":         IsISBN10,
n13":         IsISBN13,
n":           IsJSON,
tibyte":      IsMultibyte,
ii":          IsASCII,
ntableascii": IsPrintableASCII,
lwidth":      IsFullWidth,
fwidth":      IsHalfWidth,
iablewidth":  IsVariableWidth,
e64":         IsBase64,
auri":        IsDataURI,
:             IsIP,
4":           IsIPv4,
6":           IsIPv6,
":            IsMAC,
itude":       IsLatitude,
gitude":      IsLongitude,
":            IsSSN

And here is small example of usage:

 Post struct {
Title    string `valid:"alphanum,required"`
Message  string `valid:"duck,ascii"`
AuthorIP string `valid:"ipv4"`
Date     string `valid:"-"`

 := &Post{
Title:   "My Example Post",
Message: "duck",
AuthorIP: "123.234.54.3",


dd your own struct validation tags
lidator.TagMap["duck"] = govalidator.Validator(func(str string) bool {
return str == "duck"


lt, err := govalidator.ValidateStruct(post)
rr != nil {
println("error: " + err.Error())

tln(result)
WhiteList
emove all characters from string ignoring characters between "a" and "z"
tln(govalidator.WhiteList("a3a43a5a4a3a2a23a4a5a4a3a4", "a-z") == "aaaaaaaaaaaa")
Notes

Documentation is available here: godoc.org. Full information about code coverage is also available here: govalidator on gocover.io.

Support

If you do have a contribution for the package feel free to put up a Pull Request or open Issue.

Special thanks to contributors

Bitdeli Badge


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.