{"id":13367367,"url":"https://github.com/asaskevich/Govalidator","last_synced_at":"2025-03-12T18:32:33.607Z","repository":{"id":18011767,"uuid":"21034555","full_name":"asaskevich/govalidator","owner":"asaskevich","description":"[Go] Package of validators and sanitizers for strings, numerics, slices and structs","archived":false,"fork":false,"pushed_at":"2024-05-22T18:30:12.000Z","size":825,"stargazers_count":5936,"open_issues_count":168,"forks_count":551,"subscribers_count":94,"default_branch":"master","last_synced_at":"2024-05-22T19:35:54.564Z","etag":null,"topics":["go","govalidator","sanitization","validation","validation-library"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"ikkisoft/bradamsa","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asaskevich.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-06-20T10:45:23.000Z","updated_at":"2024-05-27T19:05:16.054Z","dependencies_parsed_at":"2024-05-27T19:05:13.676Z","dependency_job_id":"31fd600b-b1e2-4a27-a03f-feb79149b1f4","html_url":"https://github.com/asaskevich/govalidator","commit_stats":{"total_commits":392,"total_committers":115,"mean_commits":3.408695652173913,"dds":0.8622448979591837,"last_synced_commit":"a9d515a09cc289c60d55064edec5ef189859f172"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asaskevich%2Fgovalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asaskevich%2Fgovalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asaskevich%2Fgovalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asaskevich%2Fgovalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asaskevich","download_url":"https://codeload.github.com/asaskevich/govalidator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221309977,"owners_count":16795837,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","govalidator","sanitization","validation","validation-library"],"created_at":"2024-07-30T00:01:45.937Z","updated_at":"2025-03-12T18:32:33.599Z","avatar_url":"https://github.com/asaskevich.png","language":"Go","funding_links":["https://opencollective.com/govalidator"],"categories":["验证","驗證"],"sub_categories":["高级控制台界面","高級控制台界面"],"readme":"govalidator\n===========\n[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/asaskevich/govalidator?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge) [![GoDoc](https://godoc.org/github.com/asaskevich/govalidator?status.png)](https://godoc.org/github.com/asaskevich/govalidator)\n[![Build Status](https://travis-ci.org/asaskevich/govalidator.svg?branch=master)](https://travis-ci.org/asaskevich/govalidator)\n[![Coverage](https://codecov.io/gh/asaskevich/govalidator/branch/master/graph/badge.svg)](https://codecov.io/gh/asaskevich/govalidator) [![Go Report Card](https://goreportcard.com/badge/github.com/asaskevich/govalidator)](https://goreportcard.com/report/github.com/asaskevich/govalidator) [![GoSearch](http://go-search.org/badge?id=github.com%2Fasaskevich%2Fgovalidator)](http://go-search.org/view?id=github.com%2Fasaskevich%2Fgovalidator) [![Backers on Open Collective](https://opencollective.com/govalidator/backers/badge.svg)](#backers) [![Sponsors on Open Collective](https://opencollective.com/govalidator/sponsors/badge.svg)](#sponsors) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fasaskevich%2Fgovalidator.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fasaskevich%2Fgovalidator?ref=badge_shield)\n\nA package of validators and sanitizers for strings, structs and collections. Based on [validator.js](https://github.com/chriso/validator.js).\n\n#### Installation\nMake sure that Go is installed on your computer.\nType the following command in your terminal:\n\n\tgo get github.com/asaskevich/govalidator/v11\n\nor you can get specified release of the package with `gopkg.in`:\n\n\tgo get gopkg.in/asaskevich/govalidator.v10\n\nAfter it the package is ready to use.\n\n\n#### Import package in your project\nAdd following line in your `*.go` file:\n```go\nimport \"github.com/asaskevich/govalidator/v11\"\n```\nIf you are unhappy to use long `govalidator`, you can do something like this:\n```go\nimport (\n  valid \"github.com/asaskevich/govalidator/v11\"\n)\n```\n\n#### Activate behavior to require all fields have a validation tag by default\n`SetFieldsRequiredByDefault` causes validation to fail when struct fields do not include validations or are not explicitly marked as exempt (using `valid:\"-\"` or `valid:\"email,optional\"`). A good place to activate this is a package init function or the main() function.\n\n`SetNilPtrAllowedByRequired` causes validation to pass when struct fields marked by `required` are set to nil. This is disabled by default for consistency, but some packages that need to be able to determine between `nil` and `zero value` state can use this. If disabled, both `nil` and `zero` values cause validation errors.\n\n```go\nimport \"github.com/asaskevich/govalidator/v11\"\n\nfunc init() {\n  govalidator.SetFieldsRequiredByDefault(true)\n}\n```\n\nHere's some code to explain it:\n```go\n// this struct definition will fail govalidator.ValidateStruct() (and the field values do not matter):\ntype exampleStruct struct {\n  Name  string ``\n  Email string `valid:\"email\"`\n}\n\n// this, however, will only fail when Email is empty or an invalid email address:\ntype exampleStruct2 struct {\n  Name  string `valid:\"-\"`\n  Email string `valid:\"email\"`\n}\n\n// lastly, this will only fail when Email is an invalid email address but not when it's empty:\ntype exampleStruct2 struct {\n  Name  string `valid:\"-\"`\n  Email string `valid:\"email,optional\"`\n}\n```\n\n#### Recent breaking changes (see [#123](https://github.com/asaskevich/govalidator/pull/123))\n##### Custom validator function signature\nA context was added as the second parameter, for structs this is the object being validated – this makes dependent validation possible.\n```go\nimport \"github.com/asaskevich/govalidator/v11\"\n\n// old signature\nfunc(i interface{}) bool\n\n// new signature\nfunc(i interface{}, o interface{}) bool\n```\n\n##### Adding a custom validator\nThis was changed to prevent data races when accessing custom validators.\n```go\nimport \"github.com/asaskevich/govalidator/v11\"\n\n// before\ngovalidator.CustomTypeTagMap[\"customByteArrayValidator\"] = func(i interface{}, o interface{}) bool {\n  // ...\n}\n\n// after\ngovalidator.CustomTypeTagMap.Set(\"customByteArrayValidator\", func(i interface{}, o interface{}) bool {\n  // ...\n})\n```\n\n#### List of functions:\n```go\nfunc Abs(value float64) float64\nfunc BlackList(str, chars string) string\nfunc ByteLength(str string, params ...string) bool\nfunc CamelCaseToUnderscore(str string) string\nfunc Contains(str, substring string) bool\nfunc Count(array []interface{}, iterator ConditionIterator) int\nfunc Each(array []interface{}, iterator Iterator)\nfunc ErrorByField(e error, field string) string\nfunc ErrorsByField(e error) map[string]string\nfunc Filter(array []interface{}, iterator ConditionIterator) []interface{}\nfunc Find(array []interface{}, iterator ConditionIterator) interface{}\nfunc GetLine(s string, index int) (string, error)\nfunc GetLines(s string) []string\nfunc HasLowerCase(str string) bool\nfunc HasUpperCase(str string) bool\nfunc HasWhitespace(str string) bool\nfunc HasWhitespaceOnly(str string) bool\nfunc InRange(value interface{}, left interface{}, right interface{}) bool\nfunc InRangeFloat32(value, left, right float32) bool\nfunc InRangeFloat64(value, left, right float64) bool\nfunc InRangeInt(value, left, right interface{}) bool\nfunc IsASCII(str string) bool\nfunc IsAlpha(str string) bool\nfunc IsAlphanumeric(str string) bool\nfunc IsBase64(str string) bool\nfunc IsByteLength(str string, min, max int) bool\nfunc IsCIDR(str string) bool\nfunc IsCRC32(str string) bool\nfunc IsCRC32b(str string) bool\nfunc IsCreditCard(str string) bool\nfunc IsDNSName(str string) bool\nfunc IsDataURI(str string) bool\nfunc IsDialString(str string) bool\nfunc IsDivisibleBy(str, num string) bool\nfunc IsEmail(str string) bool\nfunc IsExistingEmail(email string) bool\nfunc IsFilePath(str string) (bool, int)\nfunc IsFloat(str string) bool\nfunc IsFullWidth(str string) bool\nfunc IsHalfWidth(str string) bool\nfunc IsHash(str string, algorithm string) bool\nfunc IsHexadecimal(str string) bool\nfunc IsHexcolor(str string) bool\nfunc IsHost(str string) bool\nfunc IsIP(str string) bool\nfunc IsIPv4(str string) bool\nfunc IsIPv6(str string) bool\nfunc IsISBN(str string, version int) bool\nfunc IsISBN10(str string) bool\nfunc IsISBN13(str string) bool\nfunc IsISO3166Alpha2(str string) bool\nfunc IsISO3166Alpha3(str string) bool\nfunc IsISO4217(str string) bool\nfunc IsISO693Alpha2(str string) bool\nfunc IsISO693Alpha3b(str string) bool\nfunc IsIn(str string, params ...string) bool\nfunc IsInRaw(str string, params ...string) bool\nfunc IsInt(str string) bool\nfunc IsJSON(str string) bool\nfunc IsLatitude(str string) bool\nfunc IsLongitude(str string) bool\nfunc IsLowerCase(str string) bool\nfunc IsMAC(str string) bool\nfunc IsMD4(str string) bool\nfunc IsMD5(str string) bool\nfunc IsMagnetURI(str string) bool\nfunc IsMongoID(str string) bool\nfunc IsMultibyte(str string) bool\nfunc IsNatural(value float64) bool\nfunc IsNegative(value float64) bool\nfunc IsNonNegative(value float64) bool\nfunc IsNonPositive(value float64) bool\nfunc IsNotNull(str string) bool\nfunc IsNull(str string) bool\nfunc IsNumeric(str string) bool\nfunc IsPort(str string) bool\nfunc IsPositive(value float64) bool\nfunc IsPrintableASCII(str string) bool\nfunc IsRFC3339(str string) bool\nfunc IsRFC3339WithoutZone(str string) bool\nfunc IsRGBcolor(str string) bool\nfunc IsRegex(str string) bool\nfunc IsRequestURI(rawurl string) bool\nfunc IsRequestURL(rawurl string) bool\nfunc IsRipeMD128(str string) bool\nfunc IsRipeMD160(str string) bool\nfunc IsRsaPub(str string, params ...string) bool\nfunc IsRsaPublicKey(str string, keylen int) bool\nfunc IsSHA1(str string) bool\nfunc IsSHA256(str string) bool\nfunc IsSHA384(str string) bool\nfunc IsSHA512(str string) bool\nfunc IsSSN(str string) bool\nfunc IsSemver(str string) bool\nfunc IsTiger128(str string) bool\nfunc IsTiger160(str string) bool\nfunc IsTiger192(str string) bool\nfunc IsTime(str string, format string) bool\nfunc IsType(v interface{}, params ...string) bool\nfunc IsURL(str string) bool\nfunc IsUTFDigit(str string) bool\nfunc IsUTFLetter(str string) bool\nfunc IsUTFLetterNumeric(str string) bool\nfunc IsUTFNumeric(str string) bool\nfunc IsUUID(str string) bool\nfunc IsUUIDv3(str string) bool\nfunc IsUUIDv4(str string) bool\nfunc IsUUIDv5(str string) bool\nfunc IsULID(str string) bool\nfunc IsUnixTime(str string) bool\nfunc IsUpperCase(str string) bool\nfunc IsVariableWidth(str string) bool\nfunc IsYYYYMMDD(str string) bool\nfunc IsWhole(value float64) bool\nfunc LeftTrim(str, chars string) string\nfunc Map(array []interface{}, iterator ResultIterator) []interface{}\nfunc Matches(str, pattern string) bool\nfunc MaxStringLength(str string, params ...string) bool\nfunc MinStringLength(str string, params ...string) bool\nfunc NormalizeEmail(str string) (string, error)\nfunc PadBoth(str string, padStr string, padLen int) string\nfunc PadLeft(str string, padStr string, padLen int) string\nfunc PadRight(str string, padStr string, padLen int) string\nfunc PrependPathToErrors(err error, path string) error\nfunc Range(str string, params ...string) bool\nfunc RemoveTags(s string) string\nfunc ReplacePattern(str, pattern, replace string) string\nfunc Reverse(s string) string\nfunc RightTrim(str, chars string) string\nfunc RuneLength(str string, params ...string) bool\nfunc SafeFileName(str string) string\nfunc SetFieldsRequiredByDefault(value bool)\nfunc SetNilPtrAllowedByRequired(value bool)\nfunc Sign(value float64) float64\nfunc StringLength(str string, params ...string) bool\nfunc StringMatches(s string, params ...string) bool\nfunc StripLow(str string, keepNewLines bool) string\nfunc ToBoolean(str string) (bool, error)\nfunc ToFloat(str string) (float64, error)\nfunc ToInt(value interface{}) (res int64, err error)\nfunc ToJSON(obj interface{}) (string, error)\nfunc ToString(obj interface{}) string\nfunc Trim(str, chars string) string\nfunc Truncate(str string, length int, ending string) string\nfunc TruncatingErrorf(str string, args ...interface{}) error\nfunc UnderscoreToCamelCase(s string) string\nfunc ValidateMap(inputMap map[string]interface{}, validationMap map[string]interface{}) (bool, error)\nfunc ValidateStruct(s interface{}) (bool, error)\nfunc WhiteList(str, chars string) string\ntype ConditionIterator\ntype CustomTypeValidator\ntype Error\nfunc (e Error) Error() string\ntype Errors\nfunc (es Errors) Error() string\nfunc (es Errors) Errors() []error\ntype ISO3166Entry\ntype ISO693Entry\ntype InterfaceParamValidator\ntype Iterator\ntype ParamValidator\ntype ResultIterator\ntype UnsupportedTypeError\nfunc (e *UnsupportedTypeError) Error() string\ntype Validator\n```\n\n#### Examples\n###### IsURL\n```go\nprintln(govalidator.IsURL(`http://user@pass:domain.com/path/page`))\n```\n###### IsType\n```go\nprintln(govalidator.IsType(\"Bob\", \"string\"))\nprintln(govalidator.IsType(1, \"int\"))\ni := 1\nprintln(govalidator.IsType(\u0026i, \"*int\"))\n```\n\nIsType can be used through the tag `type` which is essential for map validation:\n```go\ntype User\tstruct {\n  Name string      `valid:\"type(string)\"`\n  Age  int         `valid:\"type(int)\"`\n  Meta interface{} `valid:\"type(string)\"`\n}\nresult, err := govalidator.ValidateStruct(User{\"Bob\", 20, \"meta\"})\nif err != nil {\n\tprintln(\"error: \" + err.Error())\n}\nprintln(result)\n```\n###### ToString\n```go\ntype User struct {\n\tFirstName string\n\tLastName string\n}\n\nstr := govalidator.ToString(\u0026User{\"John\", \"Juan\"})\nprintln(str)\n```\n###### Each, Map, Filter, Count for slices\nEach iterates over the slice/array and calls Iterator for every item\n```go\ndata := []interface{}{1, 2, 3, 4, 5}\nvar fn govalidator.Iterator = func(value interface{}, index int) {\n\tprintln(value.(int))\n}\ngovalidator.Each(data, fn)\n```\n```go\ndata := []interface{}{1, 2, 3, 4, 5}\nvar fn govalidator.ResultIterator = func(value interface{}, index int) interface{} {\n\treturn value.(int) * 3\n}\n_ = govalidator.Map(data, fn) // result = []interface{}{1, 6, 9, 12, 15}\n```\n```go\ndata := []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\nvar fn govalidator.ConditionIterator = func(value interface{}, index int) bool {\n\treturn value.(int)%2 == 0\n}\n_ = govalidator.Filter(data, fn) // result = []interface{}{2, 4, 6, 8, 10}\n_ = govalidator.Count(data, fn) // result = 5\n```\n###### ValidateStruct [#2](https://github.com/asaskevich/govalidator/pull/2)\nIf 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:\n```go\ngovalidator.TagMap[\"duck\"] = govalidator.Validator(func(str string) bool {\n\treturn str == \"duck\"\n})\n```\nFor completely custom validators (interface-based), see below.\n\nHere is a list of available validators for struct fields (validator - used function):\n```go\n\"email\":              IsEmail,\n\"url\":                IsURL,\n\"dialstring\":         IsDialString,\n\"requrl\":             IsRequestURL,\n\"requri\":             IsRequestURI,\n\"alpha\":              IsAlpha,\n\"utfletter\":          IsUTFLetter,\n\"alphanum\":           IsAlphanumeric,\n\"utfletternum\":       IsUTFLetterNumeric,\n\"numeric\":            IsNumeric,\n\"utfnumeric\":         IsUTFNumeric,\n\"utfdigit\":           IsUTFDigit,\n\"hexadecimal\":        IsHexadecimal,\n\"hexcolor\":           IsHexcolor,\n\"rgbcolor\":           IsRGBcolor,\n\"lowercase\":          IsLowerCase,\n\"uppercase\":          IsUpperCase,\n\"int\":                IsInt,\n\"float\":              IsFloat,\n\"null\":               IsNull,\n\"uuid\":               IsUUID,\n\"uuidv3\":             IsUUIDv3,\n\"uuidv4\":             IsUUIDv4,\n\"uuidv5\":             IsUUIDv5,\n\"creditcard\":         IsCreditCard,\n\"isbn10\":             IsISBN10,\n\"isbn13\":             IsISBN13,\n\"json\":               IsJSON,\n\"multibyte\":          IsMultibyte,\n\"ascii\":              IsASCII,\n\"printableascii\":     IsPrintableASCII,\n\"fullwidth\":          IsFullWidth,\n\"halfwidth\":          IsHalfWidth,\n\"variablewidth\":      IsVariableWidth,\n\"base64\":             IsBase64,\n\"datauri\":            IsDataURI,\n\"ip\":                 IsIP,\n\"port\":               IsPort,\n\"ipv4\":               IsIPv4,\n\"ipv6\":               IsIPv6,\n\"dns\":                IsDNSName,\n\"host\":               IsHost,\n\"mac\":                IsMAC,\n\"latitude\":           IsLatitude,\n\"longitude\":          IsLongitude,\n\"ssn\":                IsSSN,\n\"semver\":             IsSemver,\n\"rfc3339\":            IsRFC3339,\n\"rfc3339WithoutZone\": IsRFC3339WithoutZone,\n\"ISO3166Alpha2\":      IsISO3166Alpha2,\n\"ISO3166Alpha3\":      IsISO3166Alpha3,\n\"ulid\":               IsULID,\n\"yyyymmdd\":           IsYYYYMMDD,\n```\nValidators with parameters\n\n```go\n\"range(min|max)\": Range,\n\"length(min|max)\": ByteLength,\n\"runelength(min|max)\": RuneLength,\n\"stringlength(min|max)\": StringLength,\n\"matches(pattern)\": StringMatches,\n\"in(string1|string2|...|stringN)\": IsIn,\n\"rsapub(keylength)\" : IsRsaPub,\n\"minstringlength(int): MinStringLength,\n\"maxstringlength(int): MaxStringLength,\n```\nValidators with parameters for any type\n\n```go\n\"type(type)\": IsType,\n```\n\nAnd here is small example of usage:\n```go\ntype Post struct {\n\tTitle    string `valid:\"alphanum,required\"`\n\tMessage  string `valid:\"duck,ascii\"`\n\tMessage2 string `valid:\"animal(dog)\"`\n\tAuthorIP string `valid:\"ipv4\"`\n\tDate     string `valid:\"-\"`\n}\npost := \u0026Post{\n\tTitle:   \"My Example Post\",\n\tMessage: \"duck\",\n\tMessage2: \"dog\",\n\tAuthorIP: \"123.234.54.3\",\n}\n\n// Add your own struct validation tags\ngovalidator.TagMap[\"duck\"] = govalidator.Validator(func(str string) bool {\n\treturn str == \"duck\"\n})\n\n// Add your own struct validation tags with parameter\ngovalidator.ParamTagMap[\"animal\"] = govalidator.ParamValidator(func(str string, params ...string) bool {\n    species := params[0]\n    return str == species\n})\ngovalidator.ParamTagRegexMap[\"animal\"] = regexp.MustCompile(\"^animal\\\\((\\\\w+)\\\\)$\")\n\nresult, err := govalidator.ValidateStruct(post)\nif err != nil {\n\tprintln(\"error: \" + err.Error())\n}\nprintln(result)\n```\n###### ValidateMap [#2](https://github.com/asaskevich/govalidator/pull/338)\nIf you want to validate maps, you can use the map to be validated and a validation map that contain the same tags used in ValidateStruct, both maps have to be in the form `map[string]interface{}`\n\nSo here is small example of usage:\n```go\nvar mapTemplate = map[string]interface{}{\n\t\"name\":\"required,alpha\",\n\t\"family\":\"required,alpha\",\n\t\"email\":\"required,email\",\n\t\"cell-phone\":\"numeric\",\n\t\"address\":map[string]interface{}{\n\t\t\"line1\":\"required,alphanum\",\n\t\t\"line2\":\"alphanum\",\n\t\t\"postal-code\":\"numeric\",\n\t},\n}\n\nvar inputMap = map[string]interface{}{\n\t\"name\":\"Bob\",\n\t\"family\":\"Smith\",\n\t\"email\":\"foo@bar.baz\",\n\t\"address\":map[string]interface{}{\n\t\t\"line1\":\"\",\n\t\t\"line2\":\"\",\n\t\t\"postal-code\":\"\",\n\t},\n}\n\nresult, err := govalidator.ValidateMap(inputMap, mapTemplate)\nif err != nil {\n\tprintln(\"error: \" + err.Error())\n}\nprintln(result)\n```\n\n###### WhiteList\n```go\n// Remove all characters from string ignoring characters between \"a\" and \"z\"\nprintln(govalidator.WhiteList(\"a3a43a5a4a3a2a23a4a5a4a3a4\", \"a-z\") == \"aaaaaaaaaaaa\")\n```\n\n###### Custom validation functions\nCustom validation using your own domain specific validators is also available - here's an example of how to use it:\n```go\nimport \"github.com/asaskevich/govalidator/v11\"\n\ntype CustomByteArray [6]byte // custom types are supported and can be validated\n\ntype StructWithCustomByteArray struct {\n  ID              CustomByteArray `valid:\"customByteArrayValidator,customMinLengthValidator\"` // multiple custom validators are possible as well and will be evaluated in sequence\n  Email           string          `valid:\"email\"`\n  CustomMinLength int             `valid:\"-\"`\n}\n\ngovalidator.CustomTypeTagMap.Set(\"customByteArrayValidator\", func(i interface{}, context interface{}) bool {\n  switch v := context.(type) { // you can type switch on the context interface being validated\n  case StructWithCustomByteArray:\n    // you can check and validate against some other field in the context,\n    // return early or not validate against the context at all – your choice\n  case SomeOtherType:\n    // ...\n  default:\n    // expecting some other type? Throw/panic here or continue\n  }\n\n  switch v := i.(type) { // type switch on the struct field being validated\n  case CustomByteArray:\n    for _, e := range v { // this validator checks that the byte array is not empty, i.e. not all zeroes\n      if e != 0 {\n        return true\n      }\n    }\n  }\n  return false\n})\ngovalidator.CustomTypeTagMap.Set(\"customMinLengthValidator\", func(i interface{}, context interface{}) bool {\n  switch v := context.(type) { // this validates a field against the value in another field, i.e. dependent validation\n  case StructWithCustomByteArray:\n    return len(v.ID) \u003e= v.CustomMinLength\n  }\n  return false\n})\n```\n\n###### Loop over Error()\nBy default .Error() returns all errors in a single String. To access each error you can do this:\n```go\n  if err != nil {\n    errs := err.(govalidator.Errors).Errors()\n    for _, e := range errs {\n      fmt.Println(e.Error())\n    }\n  }\n```\n\n###### Custom error messages\nCustom error messages are supported via annotations by adding the `~` separator - here's an example of how to use it:\n```go\ntype Ticket struct {\n  Id        int64     `json:\"id\"`\n  FirstName string    `json:\"firstname\" valid:\"required~First name is blank\"`\n}\n```\n\n#### Notes\nDocumentation is available here: [godoc.org](https://godoc.org/github.com/asaskevich/govalidator).\nFull information about code coverage is also available here: [govalidator on gocover.io](http://gocover.io/github.com/asaskevich/govalidator).\n\n#### Support\nIf you do have a contribution to the package, feel free to create a Pull Request or an Issue.\n\n#### What to contribute\nIf you don't know what to do, there are some features and functions that need to be done\n\n- [ ] Refactor code\n- [ ] Edit docs and [README](https://github.com/asaskevich/govalidator/README.md): spellcheck, grammar and typo check\n- [ ] Create actual list of contributors and projects that currently using this package\n- [ ] Resolve [issues and bugs](https://github.com/asaskevich/govalidator/issues)\n- [ ] Update actual [list of functions](https://github.com/asaskevich/govalidator#list-of-functions)\n- [ ] Update [list of validators](https://github.com/asaskevich/govalidator#validatestruct-2) that available for `ValidateStruct` and add new\n- [ ] Implement new validators: `IsFQDN`, `IsIMEI`, `IsPostalCode`, `IsISIN`, `IsISRC` etc\n- [x] Implement [validation by maps](https://github.com/asaskevich/govalidator/issues/224)\n- [ ] Implement fuzzing testing\n- [ ] Implement some struct/map/array utilities\n- [ ] Implement map/array validation\n- [ ] Implement benchmarking\n- [ ] Implement batch of examples\n- [ ] Look at forks for new features and fixes\n\n#### Advice\nFeel free to create what you want, but keep in mind when you implement new features:\n- Code must be clear and readable, names of variables/constants clearly describes what they are doing\n- Public functions must be documented and described in source file and added to README.md to the list of available functions\n- There are must be unit-tests for any new functions and improvements\n\n## Credits\n### Contributors\n\nThis project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].\n\n#### Special thanks to [contributors](https://github.com/asaskevich/govalidator/graphs/contributors)\n* [Daniel Lohse](https://github.com/annismckenzie)\n* [Attila Oláh](https://github.com/attilaolah)\n* [Daniel Korner](https://github.com/Dadie)\n* [Steven Wilkin](https://github.com/stevenwilkin)\n* [Deiwin Sarjas](https://github.com/deiwin)\n* [Noah Shibley](https://github.com/slugmobile)\n* [Nathan Davies](https://github.com/nathj07)\n* [Matt Sanford](https://github.com/mzsanford)\n* [Simon ccl1115](https://github.com/ccl1115)\n\n\u003ca href=\"https://github.com/asaskevich/govalidator/graphs/contributors\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/contributors.svg?width=890\" /\u003e\u003c/a\u003e\n\n\n### Backers\n\nThank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/govalidator#backer)]\n\n\u003ca href=\"https://opencollective.com/govalidator#backers\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/backers.svg?width=890\"\u003e\u003c/a\u003e\n\n\n### Sponsors\n\nSupport this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/govalidator#sponsor)]\n\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/0/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/0/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/1/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/1/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/2/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/2/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/3/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/3/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/4/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/4/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/5/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/5/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/6/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/6/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/7/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/7/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/8/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/8/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/govalidator/sponsor/9/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/govalidator/sponsor/9/avatar.svg\"\u003e\u003c/a\u003e\n\n\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fasaskevich%2Fgovalidator.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fasaskevich%2Fgovalidator?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasaskevich%2FGovalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasaskevich%2FGovalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasaskevich%2FGovalidator/lists"}