{"id":13786685,"url":"https://github.com/gobuffalo/validate","last_synced_at":"2025-04-05T09:07:01.623Z","repository":{"id":31041881,"uuid":"121042058","full_name":"gobuffalo/validate","owner":"gobuffalo","description":"This package provides a framework for writing validations for Go applications.","archived":false,"fork":false,"pushed_at":"2022-09-26T04:47:33.000Z","size":87,"stargazers_count":93,"open_issues_count":0,"forks_count":22,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-03-29T08:06:42.834Z","etag":null,"topics":["go","gobuffalo","golang","models","pop","validation"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"markbates/validate","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gobuffalo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"markbates","patreon":"buffalo"}},"created_at":"2018-02-10T18:25:55.000Z","updated_at":"2025-02-28T03:02:10.000Z","dependencies_parsed_at":"2022-08-19T08:01:10.898Z","dependency_job_id":null,"html_url":"https://github.com/gobuffalo/validate","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobuffalo%2Fvalidate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobuffalo%2Fvalidate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobuffalo%2Fvalidate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobuffalo%2Fvalidate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gobuffalo","download_url":"https://codeload.github.com/gobuffalo/validate/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247312077,"owners_count":20918344,"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","gobuffalo","golang","models","pop","validation"],"created_at":"2024-08-03T19:01:28.458Z","updated_at":"2025-04-05T09:07:01.601Z","avatar_url":"https://github.com/gobuffalo.png","language":"Go","readme":"# github.com/gobuffalo/validate\n[![Build Status](https://travis-ci.org/gobuffalo/validate.svg?branch=master)](https://travis-ci.org/gobuffalo/validate) [![Actions Status](https://github.com/gobuffalo/validate/workflows/Tests/badge.svg)](https://github.com/gobuffalo/validate/actions) [![GoDoc](https://godoc.org/github.com/gobuffalo/validate?status.svg)](https://godoc.org/github.com/gobuffalo/validate)\n\nThis package provides a framework for writing validations for Go applications. It does provide you with few validators, but if you need others you can easly build them.\n\n## Installation\n\n```bash\n$ go get github.com/gobuffalo/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/gobuffalo/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/gobuffalo/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\n## Built-in Validators\n\nTo make it even simpler, this package has a children package with some nice built-in validators.\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\n\t\"github.com/gobuffalo/validate\"\n\t\"github.com/gobuffalo/validate/validators\"\n)\n\ntype User struct {\n\tName  string\n\tEmail string\n}\n\n\nfunc main() {\n\tu := User{Name: \"\", Email: \"\"}\n\terrors := validate.Validate(\n\t\t\u0026validators.EmailIsPresent{Name: \"Email\", Field: u.Email, Message: \"Mail is not in the right format.\"},\n\t\t\u0026validators.StringIsPresent{Field: u.Name, Name: \"Name\"},\n\t)\n\tlog.Println(errors.Errors)\n\t// map[name:[Name can not be blank.] email:[Mail is not in the right format.]]\n}\n```\n\nAll fields are required for each validators, except Message (every validator has a default error message).\n\n### Available Validators\n\nA full list of available validators can be found at [https://pkg.go.dev/github.com/gobuffalo/validate/v3/validators](https://pkg.go.dev/github.com/gobuffalo/validate/v3/validators).\n","funding_links":["https://github.com/sponsors/markbates","https://patreon.com/buffalo"],"categories":["Validation","Utility","验证","校验库`用于校验的库`","校验库"],"sub_categories":["Utility/Miscellaneous","实用程序/Miscellaneous","HTTP Clients","查询语","Fail injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgobuffalo%2Fvalidate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgobuffalo%2Fvalidate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgobuffalo%2Fvalidate/lists"}