{"id":36696868,"url":"https://github.com/marksalpeter/validator","last_synced_at":"2026-01-12T11:32:55.498Z","repository":{"id":57537106,"uuid":"284363603","full_name":"marksalpeter/validator","owner":"marksalpeter","description":"A go validation package that returns human readable l18n error messages","archived":false,"fork":false,"pushed_at":"2021-12-11T17:32:06.000Z","size":38,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T00:45:47.500Z","etag":null,"topics":["api-validator","go","golang","golang-library","model","models","validate","validate-url","validater","validation","validation-library","validator"],"latest_commit_sha":null,"homepage":"","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/marksalpeter.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":"2020-08-02T00:42:13.000Z","updated_at":"2024-06-20T00:45:47.501Z","dependencies_parsed_at":"2022-09-04T12:52:54.928Z","dependency_job_id":null,"html_url":"https://github.com/marksalpeter/validator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/marksalpeter/validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksalpeter%2Fvalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksalpeter%2Fvalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksalpeter%2Fvalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksalpeter%2Fvalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marksalpeter","download_url":"https://codeload.github.com/marksalpeter/validator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksalpeter%2Fvalidator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28338971,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T10:58:46.209Z","status":"ssl_error","status_checked_at":"2026-01-12T10:58:42.742Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["api-validator","go","golang","golang-library","model","models","validate","validate-url","validater","validation","validation-library","validator"],"created_at":"2026-01-12T11:32:55.414Z","updated_at":"2026-01-12T11:32:55.486Z","avatar_url":"https://github.com/marksalpeter.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](http://godoc.org/github.com/marksalpeter/validator)\n\nThis is a validation package for golang that returns human readable error messages. Its ideal for validating input data for public facing restful api's. \n\nIt has the following features\n* Combination of validators with logical operators (e.g. `\u0026`, `|`, `()`)\n* Cross field and cross struct validation (e.g. `firstName and lastName must be set`)\n* Custom validators, e.g. `validator.AddRule(\"name\", func(ps..) error)`\n* Customizable i18n aware error messages using the `golang.org/x/text/message` package\n\n## How it works\n`Validator` uses `struct` tags to verify data passed in to apis. Use the `validate` tag to apply various `Rule`s that the field must follow (e.g. `validate:\"email\"`). You can add custom validation rules as necessary by implementing your own `validator.Rule` functions. This package also comes with [several common rules referenced below](#Validation-Rules) such as `number:min,max`, `email`, `password`, etc.\n\n### Example\n```go\npackage main\n\nimport \"github.com/marksalpeter/validator\"\n\ntype User struct {\n\tFirstName    string `json:\"firstName,omitempty\" validate:\"name\"`           // FirstName cannot contain numbers or special characters\n\tLastName     string `json:\"lastName,omitempty\" validate:\"name\"`            // LastName cannot contain numbers or special characters\n\tEmailAddress string `json:\"emailAddress,omitempty\" validate:\"email\"`       // EmailAddress must be a valid email address\n\tPhoneNumber  string `json:\"phoneNumber,omitempty\" validate:\"number:11,15\"` // PhoneNumber must be 11-15 numbers e.g 15551234567\n}\n\nfunc main() {\n\n\t// displays all validation errors\n\tvar user User\n\tv := validator.New()\n\tif err := v.Validate(\u0026user); err != nil {\n\t\tfmt.Println(err) // prints [\"'firstName' must be a valid name\",\"'lastName' must be a valid name\",\"'emailAddress' must be a valid email address\",\"'phoneNumber' must contain only numbers\"]\n\t}\n\n\t// set the properties\n\tuser.FirstName = \"First\"\n\tuser.LastName = \"Last\"\n\tuser.EmailAddress = \"email@address.com\"\n\tuser.PhoneNumber = \"15551234567\"\n\tif err := v.Validate(); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(\"user data is valid!\")\n}\n```\n\n### Advanced Example\n```go\npackage main\n\nimport \"github.com/marksalpeter/validator\"\n\n// User can either set name or firstName and lastName\ntype User struct {\n\tName      string `json:\"name,omitempty\" validate:\"name | or:FirstName,LastName\"`\n\tFirstName string `json:\"firstName,omitempty\" validate:\"empty | (name \u0026 and:LastName)\"`\n\tLastName  string `json:\"lastName,omitempty\" validate:\"empty | (name \u0026 and:FirstName)\"`\n}\n\nfunc main() {\n\n\tvar user User\n\tv := validator.New()\n\n\t// empty struct fails on `or:FirstName,LastName`\n\tif err := v.Validate(\u0026user); err != nil {\n\t\tfmt.Println(err) // prints [\"either 'name', 'firstName' and/or 'lastName' must be set\"]\n\t}\n\n\t// only first name fails on `(name \u0026 and:LastName)`\n\tuser.FirstName = \"First\"\n\tuser.LastName = \"\"\n\tif err := v.Validate(\u0026user); err != nil {\n\t\tfmt.Println(err) // prints [\"'firstName' and 'lastName' must be set\"]\n\t}\n\n\t// only last name fails on (name \u0026 and:FirstName)\n\tuser.FirstName = \"\"\n\tuser.LastName = \"Last\"\n\tif err := v.Validate(\u0026user); err != nil {\n\t\tfmt.Println(err) // prints [\"'lastName' and 'firstName' must be set\"]\n\t}\n\n\t// first and last name are set first name passes\n\tuser.FirstName = \"First\"\n\tuser.LastName = \"Last\"\n\tif err := v.Validate(\u0026user); err != nil {\n\t\tfmt.Println(err) // never reached\n\t}\n\n\t// name passes\n\tuser.Name = \"First Last\"\n\tuser.FirstName = \"\"\n\tuser.LastName = \"\"\n\tif err := v.Validate(\u0026user); err != nil {\n\t\tfmt.Println(err) // never reached\n\t}\n\n}\n```\n\n## Validation Rules\nThis package comes with several default validation rules:\n\n| Rule  | Description  |\n| :--- | :--- |\n| [required](#required-) | `required` returns an error if the filed contains the zero value of the type or nil |\n| [empty](#empty-) | `empty` returns an error if the field is not empty |\n| [name](#name-) | `name` returns an error if the field doesn't contain a valid name |\n| [email](#email-) | `email` returns an error if the field doesn't contain a valid email address |\n| [password](#password-) | `password` returns an error if the field doesn't contain a valid password |\n| [number](#number-) | `number` retuns an error if the field doesn't contain numbers only |\n| [letters](#letters-) | `letters` retuns an error if the field doesn't contain letters only |\n| [eq](#eq-) | `eq` returns an error if the field does not == one of the params passed in |\n| [xor](#xor-) | `xor` returns an error when more than one or zero of either the field that it is applied to or any of the field names passed as params are set to a non zero value |\n| [or](#or-) | `or` returns an error when neither the field that it is applied to nor any of the field names passed as params are set to a non zero value |\n| [and](#and-) | `and` returns an error when the field that it is applied to or any of the field names passed as params are set to the zero value |\n\n\n### Required [^](#Validation-Rules)\nRequired returns an error if the filed contains the zero value of the type or nil.\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"required\"` // 'field' is required\n}\n```\n\n### Empty [^](#Validation-Rules)\nEmpty returns an error if the field is not empty. It should be 'or'd together with\nother rules that require manditory input\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"empty | email\"` // 'field' must be a valid email address or not set at all\n}\n```\n\n### Name [^](#Validation-Rules)\nName returns an error if the field doesn't contain a valid name\ni.e. no numbers or most special characters, excepting characters that may be in a name like a -\nand allowing foreign language letters with accent marks as well as spaces\nThis prevents things like emails or phone numbers from being entered as a name.\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"name\"` // 'field' must be a valid name\n}\n```\n\n### Email [^](#Validation-Rules)\nEmail returns an error if the field doesn't contain a valid email address\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"email\"` // 'field' must be a valid email address\n}\n```\n\n### Password [^](#Validation-Rules)\nPassword returns an error if the field doesn't contain a valid password\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"password\"` // 'field' must be a valid password\n}\n```\n\n### Number [^](#Validation-Rules)\nNumber retuns an error if the field doesn't contain numbers only\n#### Example\n```go\ntype Struct struct {\n\tField   string `json:\"field\" validate:\"number\"`      // 'field' must contain only numbers\n\tField2  string `json:\"field2\" validate:\"number:3,5\"` // 'field2' must be 3 to 5 digits\n\tField3  uint   `json:\"field3\" validate:\"number:3,5\"` // 'field3' must be 3 to 5\n}\n```\n\n### Letters [^](#Validation-Rules)\nLetters retuns an error if the field doesn't contain letters only\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"letters\"` // 'field' can only take letters and spaces\n}\n```\n\n### EQ [^](#Validation-Rules)\nEQ returns an error if the field does not == one of the params passed in\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"eq:one,two,three\"` // 'field' must equal either \"one\", \"two\", or \"three\"\n}\n```\n\n### XOR [^](#Validation-Rules)\nXOR returns an error when more than one or zero of either the field that it is applied to or any of the field names passed as params are set to a non zero value\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\" xor:Field2\"` // either \"field\" or \"Field2\" must be set\n\tField2 string\n}\n```\n\n### OR [^](#Validation-Rules)\nOR returns an error when neither the field that it is applied to nor any of the field names passed as params are set to a non zero value\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"or:Field2\"` // either \"field\" or \"Field2\" or both must be set\n\tField2 string\n}\n```\n\n### AND [^](#Validation-Rules)\nAND returns an error when the field that it is applied to or any of the field names passed as params are set to the zero value\n#### Example\n```go\ntype Struct struct {\n\tField  string `json:\"field\" validate:\"and:Field2\"` // \"field\" and \"Field2\" must be set\n\tField2 string\n}\n```\n\n## Special Mentions\nThis package was heavily inspired by the [go-playground validator](https://github.com/go-playground/validator) and was originally envisioned as a more flexible, powerful version of the same basic concept.\n\n## How to Contribute\nOpen a pull request 😁\n\n## License\nDistributed under MIT License, please see license file within the code for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarksalpeter%2Fvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarksalpeter%2Fvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarksalpeter%2Fvalidator/lists"}