{"id":26720961,"url":"https://github.com/cospectrum/validol","last_synced_at":"2025-03-27T19:35:12.702Z","repository":{"id":249984284,"uuid":"833126341","full_name":"cospectrum/validol","owner":"cospectrum","description":"Validation library for golang","archived":false,"fork":false,"pushed_at":"2024-07-29T20:40:10.000Z","size":61,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-30T02:27:15.742Z","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":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cospectrum.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":"2024-07-24T12:05:16.000Z","updated_at":"2024-07-29T20:39:35.000Z","dependencies_parsed_at":"2024-07-30T01:55:37.217Z","dependency_job_id":null,"html_url":"https://github.com/cospectrum/validol","commit_stats":null,"previous_names":["cospectrum/validol"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cospectrum%2Fvalidol","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cospectrum%2Fvalidol/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cospectrum%2Fvalidol/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cospectrum%2Fvalidol/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cospectrum","download_url":"https://codeload.github.com/cospectrum/validol/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245911601,"owners_count":20692619,"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":"2025-03-27T19:35:12.070Z","updated_at":"2025-03-27T19:35:12.685Z","avatar_url":"https://github.com/cospectrum.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# validol\n[![github]](https://github.com/cospectrum/validol)\n[![goref]](https://pkg.go.dev/github.com/cospectrum/validol)\n\n[github]: https://img.shields.io/badge/github-cospectrum/validol-8da0cb?logo=github\n[goref]: https://pkg.go.dev/badge/github.com/cospectrum/validol\n\nValidation library for golang.\n\n## Content\n- [Install](#install)\n- [Usage](#usage)\n- [Validators](#validators)\n- [Combinators](#combinators)\n\n## Install\n```sh\ngo get -u github.com/cospectrum/validol\n```\nRequires Go version `1.22.0` or greater.\n\n## Usage\n\n```go\nimport (\n\t\"errors\"\n\tvd \"github.com/cospectrum/validol\"\n)\n\ntype Sex string\n\nfunc (s Sex) Validate() error {\n\treturn vd.OneOf[Sex](\"male\", \"female\", \"other\")(s)\n}\n\ntype Email string\n\nfunc (e Email) Validate() error {\n\treturn vd.All(\n\t\tvd.Len[string](vd.Gt(5)),\n\t\tvd.Len[string](vd.Lte(100)),\n\t\tvd.Email,\n\t)(string(e))\n}\n\ntype User struct {\n\tEmail Email\n\tSex   Sex\n\tage   int\n}\n\nfunc (u User) Validate() error {\n\treturn errors.Join(\n\t\tvd.Gte(18)(u.age),\n\t\tvd.Walk(u), // to continue the `Walk` using the descendants' `Validate` methods\n\t)\n}\n\nfunc main() {\n\tusers := []User{\n\t\t{Email: \"first_user@mail.com\", age: 22},\n\t\t{Email: \"second_user@mail.com\", Sex: \"male\"},\n\t}\n\tif err := vd.Validate(users); err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\n## Validators\nType `Validator[T]` is equivalent to `func(T) error`. \\\n`Validatable` interface requires `Validate() error` method.\n\n| Name | Input | Description |\n| - | - | - |\n| `Validate` | T | If `T` is `Validatable`, then it will call `Validate` method, otherwise will call `Walk` |\n| `Walk` | T | Recursively calls `Validate` method for `descendants` of `T`. The descendants of the `Validatable` descendant will not be checked automatically, instead the type must continue `Walk` manually (inside its own `Validate`). The `descendants` are public struct fields, embedded types, slice/array elements, map keys/values. |\n| `Required` | T | Checks that the value is different from `default` |\n| `Empty` | T | Checks that the value is initialized as `default` |\n| `NotNil` | T | Checks that the value is different from `nil` |\n| `Nil` | T | Checks that the value is `nil` |\n| `True` | bool | Checks that the value is `true` |\n| `False` | bool | Checks that the value is `false` |\n| `Email` | string | Email string |\n| `UUID4` | string | Universally Unique Identifier UUID v4 |\n\n## Combinators\nFunctions that create a `Validator[T]`.\n\n| Name | Input | Output | Description |\n| - | - | - | - |\n| `All` | ...Validator[T] | Validator[T] | Checks whether `all` validations have been completed successfully |\n| `Any` | ...Validator[T] | Validator[T] | Checks that `at least one` validation has been completed successfully |\n| `Not` | Validator[T] | Validator[T] | Logical not |\n| `And` | Validator[T], Validator[T] | Validator[T] | Checks that both validations have been completed successfully |\n| `Or` | Validator[T], Validator[T] | Validator[T] | Checks the success of one of the two validations |\n| `OneOf` | ...T | Validator[T] | Checks that the value is equal to one of the specified | \n| `Eq` | T comparable | Validator[T] | == |\n| `Ne` | T comparable | Validator[T] | != |\n| `Gt` | T cmp.Ordered | Validator[T] | \u003e |\n| `Gte` | T cmp.Ordered | Validator[T] | \u003e= |\n| `Lt` | T cmp.Ordered | Validator[T] | \u003c |\n| `Lte` | T cmp.Ordered | Validator[T] | \u003c= |\n| `Len` | Validator[int] | Validator[T] | Checks whether the `len` of the object passes the specified `Validator[int]` |\n| `StartsWith` | string | Validator[string] | Checks if the string starts with the specified prefix |\n| `EndsWith` | string | Validator[string] | Checks whether the string ends with the specified suffix |\n| `Contains` | string | Validator[string] | Checks whether the specified substr is within string |\n| `ContainsRune` | rune | Validator[string] | Checks whether the specified Unicode code point is within string |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcospectrum%2Fvalidol","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcospectrum%2Fvalidol","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcospectrum%2Fvalidol/lists"}