{"id":13764079,"url":"https://github.com/markbates/validate","last_synced_at":"2025-05-10T17:31:32.306Z","repository":{"id":57480573,"uuid":"78125650","full_name":"markbates/validate","owner":"markbates","description":"This package provides a framework for writing validations for Go applications.","archived":true,"fork":true,"pushed_at":"2018-07-28T10:27:26.000Z","size":30,"stargazers_count":34,"open_issues_count":3,"forks_count":0,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-16T23:32:33.334Z","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":"gobuffalo/validate","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/markbates.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-05T15:41:54.000Z","updated_at":"2024-06-13T21:19:16.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/markbates/validate","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/markbates%2Fvalidate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbates%2Fvalidate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbates%2Fvalidate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbates%2Fvalidate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markbates","download_url":"https://codeload.github.com/markbates/validate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253453322,"owners_count":21911077,"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-03T15:01:12.417Z","updated_at":"2025-05-10T17:31:31.979Z","avatar_url":"https://github.com/markbates.png","language":"Go","funding_links":[],"categories":["Validation","验证","驗證"],"sub_categories":["Advanced Console UIs","高級控制台界面","高级控制台界面","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"# github.com/markbates/validate\n[![Build Status](https://travis-ci.org/markbates/validate.svg?branch=master)](https://travis-ci.org/markbates/validate)\n\nThis package provides a framework for writing validations for Go applications. It does not, however, provide you with any actual validators, that part is up to you.\n\n## Installation\n\n```bash\n$ go get github.com/markbates/validate\n```\n\n## Usage\n\nUsing validate is pretty easy, just define some `Validator` objects and away you go.\n\nHere is a pretty simple example:\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\n\tv \"github.com/markbates/validate\"\n)\n\ntype User struct {\n\tName  string\n\tEmail string\n}\n\nfunc (u *User) IsValid(errors *v.Errors) {\n\tif u.Name == \"\" {\n\t\terrors.Add(\"name\", \"Name must not be blank!\")\n\t}\n\tif u.Email == \"\" {\n\t\terrors.Add(\"email\", \"Email must not be blank!\")\n\t}\n}\n\nfunc main() {\n\tu := User{Name: \"\", Email: \"\"}\n\terrors := v.Validate(\u0026u)\n\tlog.Println(errors.Errors)\n  // map[name:[Name must not be blank!] email:[Email must not be blank!]]\n}\n```\n\nIn the previous example I wrote a single `Validator` for the `User` struct. To really get the benefit of using go-validator, as well as the Go language, I would recommend creating distinct validators for each thing you want to validate, that way they can be run concurrently.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"strings\"\n\n\tv \"github.com/markbates/validate\"\n)\n\ntype User struct {\n\tName  string\n\tEmail string\n}\n\ntype PresenceValidator struct {\n\tField string\n\tValue string\n}\n\nfunc (v *PresenceValidator) IsValid(errors *v.Errors) {\n\tif v.Value == \"\" {\n\t\terrors.Add(strings.ToLower(v.Field), fmt.Sprintf(\"%s must not be blank!\", v.Field))\n\t}\n}\n\nfunc main() {\n\tu := User{Name: \"\", Email: \"\"}\n\terrors := v.Validate(\u0026PresenceValidator{\"Email\", u.Email}, \u0026PresenceValidator{\"Name\", u.Name})\n\tlog.Println(errors.Errors)\n  // map[name:[Name must not be blank!] email:[Email must not be blank!]]\n}\n```\n\nThat's really it. Pretty simple and straight-forward Just a nice clean framework for writing your own validators. Use in good health.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkbates%2Fvalidate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkbates%2Fvalidate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkbates%2Fvalidate/lists"}