{"id":19176085,"url":"https://github.com/davaddi/validation","last_synced_at":"2025-05-07T19:21:52.834Z","repository":{"id":57484290,"uuid":"83381362","full_name":"DavadDi/validation","owner":"DavadDi","description":"a lib for struct field validation in go ","archived":false,"fork":false,"pushed_at":"2018-02-08T08:19:17.000Z","size":28,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-20T01:33:08.011Z","etag":null,"topics":["go","nested-objects","reflection","validation"],"latest_commit_sha":null,"homepage":"https://www.godoc.org/github.com/DavadDi/validation","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/DavadDi.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":"2017-02-28T02:44:10.000Z","updated_at":"2020-02-14T17:02:12.000Z","dependencies_parsed_at":"2022-08-26T14:21:03.192Z","dependency_job_id":null,"html_url":"https://github.com/DavadDi/validation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavadDi%2Fvalidation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavadDi%2Fvalidation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavadDi%2Fvalidation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavadDi%2Fvalidation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DavadDi","download_url":"https://codeload.github.com/DavadDi/validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252941411,"owners_count":21828872,"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","nested-objects","reflection","validation"],"created_at":"2024-11-09T10:27:09.716Z","updated_at":"2025-05-07T19:21:52.803Z","avatar_url":"https://github.com/DavadDi.png","language":"Go","readme":"validation\n=====================\n\nThe Simpler Validation in go. \n\n* Use **func(v interface{}) error** for Validater\n* Support User define Validater\n* Support Struct define **Validater() error** interface\n* Support slice/array/pointer and netestd struct validate. Not for map now!\n\n[![Build Status](http://img.shields.io/travis/DavadDi/validation.svg?style=flat-square)](https://travis-ci.org/DavadDi/validation)  [![Coverage Status](http://img.shields.io/coveralls/DavadDi/validation.svg?style=flat-square)](https://coveralls.io/r/DavadDi/validation)  [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/DavadDi/validation)  [![Go Report Card](https://goreportcard.com/badge/github.com/DavadDi/validation)](https://goreportcard.com/report/github.com/DavadDi/validation)   [![License MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://img.shields.io/badge/License-MIT-brightgreen.svg)\n\n\n## Install and tests\n\nInstall:\n\n```\n$go get github.com/DavadDi/validation\n```\n\nTest:\n\n```\n$go test github.com/DavadDi/validation\n```\n\n\n## Simple Usage\n\n```go\n\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/DavadDi/validation\"\n)\n\n// ex01 simple use\ntype Person struct {\n\tName     string   `valid:\"required\"`\n\tEmail    string   `valid:\"required;email\"`\n\tAge      int      `valid:\"-\"`\n\tSex      int      ``\n\tWebSites []string `valid:\"url\"`\n}\n\nfunc main() {\n\tweb1 := \"http://www.do1618.com\"\n\tweb2 := \"www.baidu.com\"\n\n\tperson1 := \u0026Person{\n\t\tName:     \"dave\",\n\t\tEmail:    \"dwh0403@163.com\",\n\t\tWebSites: []string{web1, web2},\n\t}\n\n\tvalidater := validation.NewValidation()\n\tres := validater.Validate(person1)\n\n\tif res {\n\t\tfmt.Println(\"Person1 validate succeed!\")\n\t} else {\n\t\tfmt.Printf(\"Person1 validate failed. %s\", validater.ErrMsg())\n\t}\n\n\tvalidater.Reset()\n\tperson2 := \u0026Person{\n\t\tEmail:    \"dwh0403@163.com\",\n\t\tWebSites: []string{web1, web2},\n\t}\n\n\tres = validater.Validate(person2)\n\n\tif !res {\n\t\tfmt.Printf(\"Person2 validate failed. %s\\n\", validater.ErrMsg())\n\t} else {\n\t\tfmt.Println(\"Person2 validate succeed!\")\n\t}\n}\n```\n\n#### Struct Tag Functions:\n\trequired\n\temail\n\turl\n\n### Output:\n\tPerson1 validate succeed!\n\tPerson2 validate failed. [Name] check failed [field can't be empty or zero] [\"\"]\n\n## Add Use Define Validater\n\nUse define validater **func(v interface{}) error** and add it to validation.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/DavadDi/validation\"\n)\n\ntype Person struct {\n\tName     string   `valid:\"required\"`\n\tEmail    string   `valid:\"required;email\"`\n\tAge      int      `valid:\"required;age\"`\n\tSex      int      ``\n\tWebSites []string `valid:\"url\"`\n}\n\nfunc ageChecker(v interface{}) error {\n\tage, ok := v.(int)\n\tif !ok {\n\t\treturn validation.NewErrWrongType(\"int\", v)\n\t}\n\n\tif age \u003c= 0 || age \u003e 140 {\n\t\treturn fmt.Errorf(\"age checke failed. should between [1-140], now %d\", age)\n\t}\n\n\treturn nil\n}\n\nfunc main() {\n\tvalidation.AddValidater(\"age\", ageChecker)\n\n\tperson1 := \u0026Person{\n\t\tName:  \"dave\",\n\t\tEmail: \"dwh0403@163.com\",\n\t}\n\n\tvalidater := validation.NewValidation()\n\tres := validater.Validate(person1)\n\n\tif res {\n\t\tfmt.Println(\"Person1 validate succeed!\")\n\t} else {\n\t\tfmt.Printf(\"Person1 validate failed. %s\\n\", validater.ErrMsg())\n\t}\n}\n\n```\n\n### Output:\n\n\tPerson1 validate failed. [Age] check failed [field can't be empty or zero] [0]\n\n## Collaborate with Struct Interface\nUse struct define validater need impl the interface **Validater() error**.\n\n```go\n\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\n\t\"github.com/DavadDi/validation\"\n)\n\ntype Person struct {\n\tName     string `valid:\"required\"`\n\tEmail    string `valid:\"required;email\"`\n\tAge      int\n\tSex      int\n\tWebSites []string `valid:\"url\"`\n}\n\nfunc (p *Person) Validater() error {\n\tlog.Println(\"In our struct validater now\")\n\tif p.Age \u003c= 0 || p.Age \u003e 140 {\n\t\treturn fmt.Errorf(\"age checke failed. should between [1-140], now %d\", p.Age)\n\t}\n\n\treturn nil\n}\n\nfunc main() {\n\t// Turn on debug\n\t// validation.EnableDebug(true)\n\tperson1 := \u0026Person{\n\t\tName:  \"dave\",\n\t\tEmail: \"dwh0403@163.com\",\n\t}\n\n\tvalidater := validation.NewValidation()\n\tres := validater.Validate(person1)\n\n\tif res {\n\t\tfmt.Println(\"Person1 validate succeed!\")\n\t} else {\n\t\tfmt.Printf(\"Person1 validate failed. %s\\n\", validater.ErrMsg())\n\t}\n}\n\n\n```\n\n### Output\n\n\t2017/02/28 10:29:34 In our struct validater now\n\tPerson1 validate failed. [Object] check failed [age checke failed. should between [1-140], now 0] [\u0026main.Person{Name:\"dave\", Email:\"dwh0403@163.com\", Age:0, Sex:0, WebSites:[]string(nil)}]\n\n## Check Ptr Field for Requried\n```go\n\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/DavadDi/validation\"\n)\n\nfunc Bool(a bool) *bool {\n\treturn \u0026a\n}\n\ntype Person struct {\n\tName     string `valid:\"required\"`\n\tEmail    string `valid:\"required;email\"`\n\tAge      int\n\tSex      int\n\tIsAdmin  *bool    `valid:\"required\"`  // check IsAmdin has or not\n\tWebSites []string `valid:\"url\"`\n}\n\nfunc main() {\n\t// Turn on debug\n\tvalidation.EnableDebug(true)\n\tperson1 := \u0026Person{\n\t\tName:  \"dave\",\n\t\tEmail: \"dwh0403@163.com\",\n\n\t\tIsAdmin: Bool(false),\n\t}\n\n\tvalidater := validation.NewValidation()\n\tres := validater.Validate(person1)\n\n\tif res {\n\t\tfmt.Println(\"Person1 validate succeed!\")\n\t} else {\n\t\tfmt.Printf(\"Person1 validate failed. %s\\n\", validater.ErrMsg())\n\t}\n}\n\n```\n\n Why used **IsAdmin  *bool**, because sometime, we recv data from RESTFUL interface, for example:\n\n web pass json struct:\n ```json\n {\n    \"name\": \"dave\"\n }\n ```\n\n In go program, we define the struct below, need the both  value **name** \u0026\u0026 **isAdmin** \n\n ```go\ntype Person struct {\n\tName     *string `valid:\"required\" json:\"name\"`\n\tIsAdmin  *bool    `valid:\"required\" json:\"isAdmin\"` \n}\n ```\n\nAfter call **json.Marshal**, valid **required** on field ptr can meet the situation.\n\n\n\n## Debug\n\nTurn on Debug\n\n```go\nvalidation.EnableDebug(true)\n```\n\nTurn off Debug\n\n```go\nvalidation.EnableDebug(false)\n```\n\n## LICENSE\n\nMIT License https://choosealicense.com/licenses/mit/  More See https://choosealicense.com/licenses/\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavaddi%2Fvalidation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavaddi%2Fvalidation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavaddi%2Fvalidation/lists"}