{"id":13711295,"url":"https://github.com/thedevsaddam/govalidator","last_synced_at":"2025-05-15T03:05:08.801Z","repository":{"id":25198389,"uuid":"103425053","full_name":"thedevsaddam/govalidator","owner":"thedevsaddam","description":"Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.","archived":false,"fork":false,"pushed_at":"2024-05-12T11:52:08.000Z","size":180,"stargazers_count":1335,"open_issues_count":43,"forks_count":123,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-05-15T03:05:05.478Z","etag":null,"topics":["go-request-validation","go-validator","govalidator","request","validation","validator"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thedevsaddam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"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":"2017-09-13T16:42:20.000Z","updated_at":"2025-05-15T00:32:39.000Z","dependencies_parsed_at":"2024-06-18T12:28:22.719Z","dependency_job_id":null,"html_url":"https://github.com/thedevsaddam/govalidator","commit_stats":{"total_commits":130,"total_committers":15,"mean_commits":8.666666666666666,"dds":"0.16923076923076918","last_synced_commit":"410bf76327c31ef88a2cc858060b4016dcb80bb3"},"previous_names":["govalidator/validator","thedevsaddam/validator"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedevsaddam%2Fgovalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedevsaddam%2Fgovalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedevsaddam%2Fgovalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedevsaddam%2Fgovalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thedevsaddam","download_url":"https://codeload.github.com/thedevsaddam/govalidator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254264765,"owners_count":22041793,"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-request-validation","go-validator","govalidator","request","validation","validator"],"created_at":"2024-08-02T23:01:06.660Z","updated_at":"2025-05-15T03:05:08.766Z","avatar_url":"https://github.com/thedevsaddam.png","language":"Go","funding_links":[],"categories":["Validation","Go","Developer Tools \u0026 Libraries","验证","校验库`用于校验的库`","校验库","Utility"],"sub_categories":["Utility/Miscellaneous","🚀 How to contribute","HTTP Clients","Advanced Console UIs","实用程序/Miscellaneous","查询语","交流","Fail injection","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"![govalidator](govalidator.png)\n\n[![Build Status](https://travis-ci.org/thedevsaddam/govalidator.svg?branch=master)](https://travis-ci.org/thedevsaddam/govalidator)\n[![Project status](https://img.shields.io/badge/version-1.9-green.svg)](https://github.com/thedevsaddam/govalidator/releases)\n[![Go Report Card](https://goreportcard.com/badge/github.com/thedevsaddam/govalidator)](https://goreportcard.com/report/github.com/thedevsaddam/govalidator)\n[![Coverage Status](https://coveralls.io/repos/github/thedevsaddam/govalidator/badge.svg?branch=master)](https://coveralls.io/github/thedevsaddam/govalidator?branch=master)\n[![GoDoc](https://godoc.org/github.com/thedevsaddam/govalidator?status.svg)](https://godoc.org/github.com/thedevsaddam/govalidator)\n[![License](https://img.shields.io/dub/l/vibe-d.svg)](https://github.com/thedevsaddam/govalidator/blob/dev/LICENSE.md)\n\nValidate golang request data with simple rules. Highly inspired by Laravel's request validation.\n\n\n### Installation\n\nInstall the package using\n```go\n$ go get github.com/thedevsaddam/govalidator\n// or\n$ go get gopkg.in/thedevsaddam/govalidator.v1\n```\n\n### Usage\n\nTo use the package import it in your `*.go` code\n```go\nimport \"github.com/thedevsaddam/govalidator\"\n// or\nimport \"gopkg.in/thedevsaddam/govalidator.v1\"\n```\n\n### Example\n\n***Validate `form-data`, `x-www-form-urlencoded` and `query params`***\n\n```go\n\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/thedevsaddam/govalidator\"\n)\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n\trules := govalidator.MapData{\n\t\t\"username\": []string{\"required\", \"between:3,8\"},\n\t\t\"email\":    []string{\"required\", \"min:4\", \"max:20\", \"email\"},\n\t\t\"web\":      []string{\"url\"},\n\t\t\"phone\":    []string{\"digits:11\"},\n\t\t\"agree\":    []string{\"bool\"},\n\t\t\"dob\":      []string{\"date\"},\n\t}\n\n\tmessages := govalidator.MapData{\n\t\t\"username\": []string{\"required:আপনাকে অবশ্যই ইউজারনেম দিতে হবে\", \"between:ইউজারনেম অবশ্যই ৩-৮ অক্ষর হতে হবে\"},\n\t\t\"phone\":    []string{\"digits:ফোন নাম্বার অবশ্যই ১১ নম্বারের হতে হবে\"},\n\t}\n\n\topts := govalidator.Options{\n\t\tRequest:         r,        // request object\n\t\tRules:           rules,    // rules map\n\t\tMessages:        messages, // custom message map (Optional)\n\t\tRequiredDefault: true,     // all the field to be pass the rules\n\t}\n\tv := govalidator.New(opts)\n\te := v.Validate()\n\terr := map[string]interface{}{\"validationError\": e}\n\tw.Header().Set(\"Content-type\", \"application/json\")\n\tjson.NewEncoder(w).Encode(err)\n}\n\nfunc main() {\n\thttp.HandleFunc(\"/\", handler)\n\tfmt.Println(\"Listening on port: 9000\")\n\thttp.ListenAndServe(\":9000\", nil)\n}\n\n```\n\nSend request to the server using curl or postman: `curl GET \"http://localhost:9000?web=\u0026phone=\u0026zip=\u0026dob=\u0026agree=\"`\n\n\n***Response***\n```json\n{\n    \"validationError\": {\n        \"agree\": [\n            \"The agree may only contain boolean value, string or int 0, 1\"\n        ],\n        \"dob\": [\n            \"The dob field must be a valid date format. e.g: yyyy-mm-dd, yyyy/mm/dd etc\"\n        ],\n        \"email\": [\n            \"The email field is required\",\n            \"The email field must be a valid email address\"\n        ],\n        \"phone\": [\n            \"ফোন নাম্বার অবশ্যই ১১ নম্বারের হতে হবে\"\n        ],\n        \"username\": [\n            \"আপনাকে অবশ্যই ইউজারনেম দিতে হবে\",\n            \"ইউজারনেম অবশ্যই ৩-৮ অক্ষর হতে হবে\"\n        ],\n        \"web\": [\n            \"The web field format is invalid\"\n        ]\n    }\n}\n```\n\n### More examples\n\n***Validate file***\n\n* [Validate file](doc/FILE_VALIDATION.md)\n\n***Validate `application/json` or `text/plain` as raw body***\n\n* [Validate JSON to simple struct](doc/SIMPLE_STRUCT_VALIDATION.md)\n* [Validate JSON to map](doc/MAP_VALIDATION.md)\n* [Validate JSON to nested struct](doc/NESTED_STRUCT.md)\n* [Validate using custom rule](doc/CUSTOM_RULE.md)\n\n***Validate struct directly***\n\n* [Validate Struct](doc/STRUCT_VALIDATION.md)\n\n### Validation Rules\n* `alpha` The field under validation must be entirely alphabetic characters.\n* `alpha_dash` The field under validation may have alpha-numeric characters, as well as dashes and underscores.\n* `alpha_space` The field under validation may have alpha-numeric characters, as well as dashes, underscores and space.\n* `alpha_num` The field under validation must be entirely alpha-numeric characters.\n* `between:numeric,numeric` The field under validation check the length of characters/ length of array, slice, map/ range between two integer or float number etc.\n* `numeric` The field under validation must be entirely numeric characters.\n* `numeric_between:numeric,numeric` The field under validation must be a numeric value between the range.\n   e.g: `numeric_between:18,65` may contains numeric value like `35`, `55` . You can also pass float value to check. Moreover, both bounds can be omitted to create an unbounded minimum (e.g: `numeric_between:,65`) or an unbounded maximum (e.g: `numeric_between:-1,`).\n* `bool` The field under validation must be able to be cast as a boolean. Accepted input are `true, false, 1, 0, \"1\" and \"0\"`.\n* `credit_card` The field under validation must have a valid credit card number. Accepted cards are `Visa, MasterCard, American Express, Diners Club, Discover and JCB card`\n* `coordinate` The field under validation must have a value of valid coordinate.\n* `css_color` The field under validation must have a value of valid CSS color. Accepted colors are `hex, rgb, rgba, hsl, hsla` like `#909, #00aaff, rgb(255,122,122)`\n* `date` The field under validation must have a valid date of format yyyy-mm-dd or yyyy/mm/dd.\n* `date:dd-mm-yyyy` The field under validation must have a valid date of format dd-mm-yyyy.\n* `digits:int` The field under validation must be numeric and must have an exact length of value.\n* `digits_between:int,int` The field under validation must be numeric and must have length between the range.\n   e.g: `digits_between:3,5` may contains digits like `2323`, `12435`\n* `in:foo,bar` The field under validation must have one of the values. e.g: `in:admin,manager,user` must contain the values (admin or manager or user)\n* `not_in:foo,bar` The field under validation must have one value except foo,bar. e.g: `not_in:admin,manager,user` must not contain the values (admin or manager or user)\n* `email` The field under validation must have a valid email.\n* `float` The field under validation must have a valid float number.\n* `mac_address` The field under validation must have be a valid Mac Address.\n* `min:numeric` The field under validation must have a min length of characters for string, items length for slice/map, value for integer or float.\n   e.g: `min:3` may contains characters minimum length of 3 like `\"john\", \"jane\", \"jane321\"` but not `\"mr\", \"xy\"`\n* `max:numeric` The field under validation must have a max length of characters for string, items length for slice/map, value for integer or float.\n   e.g: `max:6` may contains characters maximum length of 6 like `\"john doe\", \"jane doe\"` but not `\"john\", \"jane\"`\n* `len:numeric` The field under validation must have an exact length of characters, exact integer or float value, exact size of map/slice.\n   e.g: `len:4` may contains characters exact length of 4 like `Food, Mood, Good`\n* `ip` The field under validation must be a valid IP address.\n* `ip_v4` The field under validation must be a valid IP V4 address.\n* `ip_v6` The field under validation must be a valid IP V6 address.\n* `json` The field under validation must be a valid JSON string.\n* `lat` The field under validation must be a valid latitude.\n* `lon` The field under validation must be a valid longitude.\n* `regex:regular expression` The field under validation validate against the regex. e.g: `regex:^[a-zA-Z]+$` validate the letters.\n* `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: 1) The value is null. 2)The value is an empty string. 3) Zero length of map, slice. 4) Zero value for integer or float\n* `size:integer` The field under validation validate a file size only in form-data ([see example](doc/FILE_VALIDATION.md))\n* `ext:jpg,png` The field under validation validate a file extension ([see example](doc/FILE_VALIDATION.md))\n* `mime:image/jpg,image/png` The field under validation validate a file mime type ([see example](doc/FILE_VALIDATION.md))\n* `url` The field under validation must be a valid URL.\n* `uuid` The field under validation must be a valid UUID.\n* `uuid_v3` The field under validation must be a valid UUID V3.\n* `uuid_v4` The field under validation must be a valid UUID V4.\n* `uuid_v5` The field under validation must be a valid UUID V5.\n\n### Add Custom Rules\n\n```go\nfunc init() {\n\t// simple example\n\tgovalidator.AddCustomRule(\"must_john\", func(field string, rule string, message string, value interface{}) error {\n\t\tval := value.(string)\n\t\tif val != \"john\" || val != \"John\" {\n\t\t\treturn fmt.Errorf(\"The %s field must be John or john\", field)\n\t\t}\n\t\treturn nil\n\t})\n\n\t// custom rules to take fixed length word.\n\t// e.g: word:5 will throw error if the field does not contain exact 5 word\n\tgovalidator.AddCustomRule(\"word\", func(field string, rule string, message string, value interface{}) error {\n\t\tvalSlice := strings.Fields(value.(string))\n\t\tl, _ := strconv.Atoi(strings.TrimPrefix(rule, \"word:\")) //handle other error\n\t\tif len(valSlice) != l {\n\t\t\treturn fmt.Errorf(\"The %s field must be %d word\", field, l)\n\t\t}\n\t\treturn nil\n\t})\n\n}\n```\nNote: Array, map, slice can be validated by adding custom rules.\n\n### Custom Message/ Localization\nIf you need to translate validation message you can pass messages as options.\n\n```go\nmessages := govalidator.MapData{\n\t\"username\": []string{\"required:You must provide username\", \"between:The username field must be between 3 to 8 chars\"},\n\t\"zip\":      []string{\"numeric:Please provide zip field as numeric\"},\n}\n\nopts := govalidator.Options{\n\tMessages:        messages,\n}\n```\n\n### Contribution\nIf you are interested to make the package better please send pull requests or create an issue so that others can fix.\n[Read the contribution guide here](CONTRIBUTING.md)\n\n### Contributors\n\n- [Jun Kimura](https://github.com/bluele)\n- [Steve HIll](https://github.com/stevehill1981)\n- [ErickSkrauch](https://github.com/erickskrauch)\n- [Sakib Sami](https://github.com/s4kibs4mi)\n- [Rip](https://github.com/ripbandit)\n- [Jose Nazario](https://github.com/paralax)\n\n### See all [contributors](https://github.com/thedevsaddam/govalidator/graphs/contributors)\n\n### See [benchmarks](doc/BENCHMARK.md)\n### Read [API documentation](https://godoc.org/github.com/thedevsaddam/govalidator)\n\n### **License**\nThe **govalidator** is an open-source software licensed under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedevsaddam%2Fgovalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthedevsaddam%2Fgovalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedevsaddam%2Fgovalidator/lists"}