{"id":13514150,"url":"https://github.com/go-validator/validator","last_synced_at":"2026-01-10T04:58:08.094Z","repository":{"id":16523587,"uuid":"19276758","full_name":"go-validator/validator","owner":"go-validator","description":"Package validator implements struct field validations","archived":false,"fork":false,"pushed_at":"2022-04-05T14:57:29.000Z","size":130,"stargazers_count":1328,"open_issues_count":10,"forks_count":127,"subscribers_count":31,"default_branch":"v2","last_synced_at":"2024-10-21T12:08:40.488Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-validator.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-29T13:09:24.000Z","updated_at":"2024-10-09T16:38:42.000Z","dependencies_parsed_at":"2022-08-07T08:15:26.542Z","dependency_job_id":null,"html_url":"https://github.com/go-validator/validator","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-validator%2Fvalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-validator%2Fvalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-validator%2Fvalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-validator%2Fvalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-validator","download_url":"https://codeload.github.com/go-validator/validator/tar.gz/refs/heads/v2","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246407402,"owners_count":20772127,"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":[],"created_at":"2024-08-01T05:00:47.760Z","updated_at":"2026-01-10T04:58:08.032Z","avatar_url":"https://github.com/go-validator.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Package validator\n\nPackage validator implements variable validations\n\n# Installation\n\nJust use go get.\n\n```bash\ngo get gopkg.in/validator.v2\n```\n\nAnd then just import the package into your own code.\n\n```go\nimport (\n\t\"gopkg.in/validator.v2\"\n)\n```\n\n# Usage\n\nPlease see http://godoc.org/gopkg.in/validator.v2 for detailed usage docs.\nA simple example would be.\n\n```go\ntype NewUserRequest struct {\n\tUsername string `validate:\"min=3,max=40,regexp=^[a-zA-Z]*$\"`\n\tName string     `validate:\"nonzero\"`\n\tAge int         `validate:\"min=21\"`\n\tPassword string `validate:\"min=8\"`\n}\n\nnur := NewUserRequest{Username: \"something\", Age: 20}\nif errs := validator.Validate(nur); errs != nil {\n\t// values not valid, deal with errors here\n}\n```\n\nBuiltin validators\n\nHere is the list of validators buildin in the package. Validators buildin\nwill check the element pointed to if the value to check is a pointer.\nThe `nil` pointer is treated as a valid value by validators buildin other\nthan `nonzero`, so you should to use `nonzero` if you don't want to\naccept a `nil` pointer.\n\n```\nlen\n\tFor numeric numbers, len will simply make sure that the\n\tvalue is equal to the parameter given. For strings, it\n\tchecks that the string length is exactly that number of\n\tcharacters. For slices,\tarrays, and maps, validates the\n\tnumber of items. (Usage: len=10)\n\nmax\n\tFor numeric numbers, max will simply make sure that the\n\tvalue is lesser or equal to the parameter given. For strings,\n\tit checks that the string length is at most that number of\n\tcharacters. For slices,\tarrays, and maps, validates the\n\tnumber of items. (Usage: max=10)\n\nmin\n\tFor numeric numbers, min will simply make sure that the value\n\tis greater or equal to the parameter given. For strings, it\n\tchecks that the string length is at least that number of\n\tcharacters. For slices, arrays, and maps, validates the\n\tnumber of items. (Usage: min=10)\n\nnonzero\n\tThis validates that the value is not zero. The appropriate\n\tzero value is given by the Go spec (e.g. for int it's 0, for\n\tstring it's \"\", for pointers is nil, etc.) For structs, it\n\twill not check to see if the struct itself has all zero\n\tvalues, instead use a pointer or put nonzero on the struct's\n\tkeys that you care about. For pointers, the pointer's value\n\tis used to test for nonzero in addition to the pointer itself\n\tnot being nil. To just check for not being nil, use `nonnil`.\n\t(Usage: nonzero)\n\nregexp\n\tOnly valid for string types, it will validate that the\n\tvalue matches the regular expression provided as parameter.\n\tCommas need to be escaped with 2 backslashes `\\\\`.\n\t(Usage: regexp=^a.*b$)\n\nnonnil\n\tValidates that the given value is not nil. (Usage: nonnil)\n```\n\nCustom validators\n\nIt is possible to define custom validators by using SetValidationFunc.\nFirst, one needs to create a validation function.\n\n```go\n// Very simple validator\nfunc notZZ(v interface{}, param string) error {\n\tst := reflect.ValueOf(v)\n\tif st.Kind() != reflect.String {\n\t\treturn errors.New(\"notZZ only validates strings\")\n\t}\n\tif st.String() == \"ZZ\" {\n\t\treturn errors.New(\"value cannot be ZZ\")\n\t}\n\treturn nil\n}\n```\n\nThen one needs to add it to the list of validators and give it a \"tag\"\nname.\n\n```go\nvalidator.SetValidationFunc(\"notzz\", notZZ)\n```\n\nThen it is possible to use the notzz validation tag. This will print\n\"Field A error: value cannot be ZZ\"\n\n```go\ntype T struct {\n\tA string  `validate:\"nonzero,notzz\"`\n}\nt := T{\"ZZ\"}\nif errs := validator.Validate(t); errs != nil {\n\tfmt.Printf(\"Field A error: %s\\n\", errs[\"A\"][0])\n}\n```\n\nYou can also have multiple sets of validator rules with SetTag().\n\n```go\ntype T struct {\n\tA int `foo:\"nonzero\" bar:\"min=10\"`\n}\nt := T{5}\nSetTag(\"foo\")\nvalidator.Validate(t) // valid as it's nonzero\nSetTag(\"bar\")\nvalidator.Validate(t) // invalid as it's less than 10\n```\n\nSetTag is probably better used with multiple validators.\n\n```go\nfooValidator := validator.NewValidator()\nfooValidator.SetTag(\"foo\")\nbarValidator := validator.NewValidator()\nbarValidator.SetTag(\"bar\")\nfooValidator.Validate(t)\nbarValidator.Validate(t)\n```\n\nThis keeps the default validator's tag clean. Again, please refer to\ngodocs for a lot of more examples and different uses.\n\n# Pull requests policy\n\ntl;dr. Contributions are welcome.\n\nThe repository is organized in version branches. Pull requests to, say, the\n`v2` branch that break API compatibility will not be accepted. It is okay to\nbreak the API in master, _not in the branches_.\n\nAs for validation functions, the preference is to keep the main code simple\nand add most new functions to the validator-contrib repository.\n\nhttps://github.com/go-validator/validator-contrib\n\nFor improvements and/or fixes to the builtin validation functions, please\nmake sure the behaviour will not break existing functionality in the branches.\nIf you see a case where the functionality of the builtin will change\nsignificantly, please send a pull request against `master`. We can discuss then\nwhether the changes should be incorporated in the version branches as well.\n\n# License\n\nCopyright 2014 Roberto Teixeira \u003crobteix@robteix.com\u003e\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-validator%2Fvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-validator%2Fvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-validator%2Fvalidator/lists"}