{"id":13645452,"url":"https://github.com/RussellLuo/validating","last_synced_at":"2025-04-21T14:31:07.413Z","repository":{"id":23600435,"uuid":"99423068","full_name":"RussellLuo/validating","owner":"RussellLuo","description":"A Go library for validating structs, maps and slices.","archived":false,"fork":false,"pushed_at":"2024-06-27T08:34:40.000Z","size":111,"stargazers_count":223,"open_issues_count":3,"forks_count":14,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-11-04T08:50:04.151Z","etag":null,"topics":["go","validation"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/RussellLuo/validating/v3","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/RussellLuo.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-05T13:01:39.000Z","updated_at":"2024-11-01T03:38:27.000Z","dependencies_parsed_at":"2024-08-02T01:38:27.692Z","dependency_job_id":null,"html_url":"https://github.com/RussellLuo/validating","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fvalidating","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fvalidating/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fvalidating/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fvalidating/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RussellLuo","download_url":"https://codeload.github.com/RussellLuo/validating/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223868233,"owners_count":17217052,"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","validation"],"created_at":"2024-08-02T01:02:35.400Z","updated_at":"2024-11-09T18:31:25.186Z","avatar_url":"https://github.com/RussellLuo.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# validating\n\nA Go library for validating structs, maps and slices.\n\n\n## Features\n\n1. Simple\n\n    Simple and stupid, no magic involved.\n\n2. Type-safe\n\n    Schema is defined in Go, which is type-safer (and more powerful) than traditional struct tags.\n\n3. Flexible\n\n    - Validators are composable.\n    - Nested struct validation is well supported.\n    - Schema can be defined inside or outside struct.\n    - Validator customizations are made easy.\n\n4. No reflection\n\n\n## Installation\n\n\n```bash\n$ go get github.com/RussellLuo/validating/v3@latest\n```\n\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tv \"github.com/RussellLuo/validating/v3\"\n)\n\ntype Address struct {\n\tCountry string\n\tCity    string\n}\n\nfunc (a Address) Schema() v.Schema {\n\treturn v.Schema{\n\t\tv.F(\"country\", a.Country): v.Nonzero[string]().Msg(\"empty country\"),\n\t\tv.F(\"city\", a.City):       v.In(\"A\", \"B\", \"C\").Msg(\"must be A or B or C\"),\n\t}\n}\n\ntype Person struct {\n\tName    string\n\tAge     int\n\tHobbies []string\n\tAddress Address\n}\n\nfunc (p Person) Schema() v.Schema {\n\treturn v.Schema{\n\t\tv.F(\"name\", p.Name): v.All(\n\t\t\tv.LenString(5, 10).Msg(\"bad name length\"),\n\t\t\tv.Match(`\\w+`).Msg(\"bad name pattern\"),\n\t\t),\n\t\tv.F(\"age\", p.Age):         v.Gte(10).Msg(\"must be at least 10 years old\"),\n\t\tv.F(\"hobbies\", p.Hobbies): v.EachSlice[[]string](v.In(\"Music\", \"Sports\").Msg(\"unknown hobby\")),\n\t\tv.F(\"address\", p.Address): p.Address.Schema(),\n\t}\n}\n\nfunc main() {\n\tp := Person{\n\t\tName:    \"Foo\",\n\t\tAge:     5,\n\t\tHobbies: []string{\"Nothing\"},\n\t\tAddress: Address{City: \"D\"},\n\t}\n\terrs := v.Validate(p.Schema())\n\tfor _, err := range errs {\n\t\tfmt.Println(err)\n\t}\n}\n```\n\n```bash\n$ go run main.go\nname: INVALID(bad name length)\nage: INVALID(must be at least 10 years old)\nhobbies[0]: INVALID(unknown hobby)\naddress.country: INVALID(empty country)\naddress.city: INVALID(must be A or B or C)\n```\n\n\n## Validator factories and validators\n\nTo be strict, this library has a conceptual distinction between `validator factory` and `validator`.\n\nA validator factory is a function used to create a validator, which will do the actual validation.\n\n### Built-in validator factories\n\n- [Func](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Func)\n- [Schema](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Schema)\n- [Value](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Value)\n- [Nested](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Nested)\n- [EachMap](https://pkg.go.dev/github.com/RussellLuo/validating/v3#EachMap)\n- [EachSlice](https://pkg.go.dev/github.com/RussellLuo/validating/v3#EachSlice)\n- [Map](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Map)\n- [Slice/Array](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Slice)\n- [All/And](https://pkg.go.dev/github.com/RussellLuo/validating/v3#All)\n- [Any/Or](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Any)\n- [Not](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Not)\n- [Is](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Is)\n- [Nonzero](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Nonzero)\n- [Zero](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Zero)\n- [ZeroOr](https://pkg.go.dev/github.com/RussellLuo/validating/v3#ZeroOr)\n- [LenString](https://pkg.go.dev/github.com/RussellLuo/validating/v3#LenString)\n- [LenSlice](https://pkg.go.dev/github.com/RussellLuo/validating/v3#LenSlice)\n- [RuneCount](https://pkg.go.dev/github.com/RussellLuo/validating/v3#RuneCount)\n- [Eq](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Eq)\n- [Ne](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Ne)\n- [Gt](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Gt)\n- [Gte](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Gte)\n- [Lt](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Lt)\n- [Lte](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Lte)\n- [Range](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Range)\n- [In](https://pkg.go.dev/github.com/RussellLuo/validating/v3#In)\n- [Nin](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Nin)\n- [Match](https://pkg.go.dev/github.com/RussellLuo/validating/v3#Match)\n\n### Extension validator factories\n\n- [vext](https://github.com/RussellLuo/vext)\n\n### Validator customizations\n\n- [From a boolean function](example_simple_string_isip_test.go#L16)\n- [From a normal function](example_customizations_test.go#L30-L32)\n- [From a struct](example_customizations_test.go#L35-L37)\n\n\n## Examples\n\n- [Simple value](example_simple_value_test.go)\n- [Simple pointer](example_simple_pointer_test.go)\n- [Simple string (is IP?)](example_simple_string_isip_test.go)\n- [Simple struct](example_simple_struct_test.go)\n- [Simple slice](example_simple_slice_test.go)\n- [Simple map](example_simple_map_test.go)\n- [Nested struct](example_nested_struct_test.go)\n- [Nested struct (schema inside)](example_nested_struct_schema_inside_test.go)\n- [Nested struct pointer](example_nested_struct_pointer_test.go)\n- [Nested struct slice](example_nested_struct_slice_test.go)\n- [Nested struct map](example_nested_struct_map_test.go)\n\n\n## Documentation\n\nCheck out the [Godoc][1].\n\n\n## Thanks\n\nThis library borrows some ideas from the following libraries:\n\n- [mholt/binding][2]\n\n    Prefer no reflection.\n\n- [alecthomas/voluptuous][3]\n\n    Support composite validator factories `All`/`And`, `Any`/`Or`.\n\n- [go-validator/validator][4]\n\n    Use the term `nonzero` instead of `required`/`optional`.\n\n\n## License\n\n[MIT][5]\n\n\n[1]: https://pkg.go.dev/github.com/RussellLuo/validating/v3\n[2]: https://github.com/mholt/binding\n[3]: https://github.com/alecthomas/voluptuous\n[4]: https://github.com/go-validator/validator\n[5]: http://opensource.org/licenses/MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRussellLuo%2Fvalidating","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRussellLuo%2Fvalidating","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRussellLuo%2Fvalidating/lists"}