{"id":13493284,"url":"https://github.com/rodkranz/govalidator","last_synced_at":"2026-01-05T08:30:18.817Z","repository":{"id":82449005,"uuid":"105057773","full_name":"rodkranz/govalidator","owner":"rodkranz","description":null,"archived":false,"fork":false,"pushed_at":"2019-05-31T15:00:53.000Z","size":9,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-29T20:19:06.138Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rodkranz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-09-27T19:01:31.000Z","updated_at":"2019-05-31T15:00:55.000Z","dependencies_parsed_at":"2023-03-09T12:30:43.272Z","dependency_job_id":null,"html_url":"https://github.com/rodkranz/govalidator","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/rodkranz%2Fgovalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodkranz%2Fgovalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodkranz%2Fgovalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodkranz%2Fgovalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rodkranz","download_url":"https://codeload.github.com/rodkranz/govalidator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239735262,"owners_count":19688262,"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":"2024-07-31T19:01:13.834Z","updated_at":"2026-01-05T08:30:18.752Z","avatar_url":"https://github.com/rodkranz.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Go - Validator\n\nThis package provides a validations for Go applications.\n\n### Validation Rules\n\nTo use them, the tag format is `validate:\"\u003cName\u003e\"`.\n\n| Name | Note\n|---|---\n| OmitEmpty | Omit rest of validations if value is empty\n| Required | Must be non-zero value\n| AlphaDash | Must be alpha characters or numerics or -_\n| AlphaDashDot | Must be alpha characters or numerics, -_ or .\n| Size(int) | Fixed length\n| MinSize(int) | Minimum length\n| MaxSize(int) | Maximum length\n| Email | Must be E-mail address\n| Url | Must be HTTP/HTTPS URL address\n| Include(string) | Must contain\n| Exclude(string) | Must not contain\n\nTo combine multiple rules: `govalidator:\"Required;MinSize(10)\".`\n\n### Example\n\nThis is an exemple of how to use **govalidator**:\n\n    package main\n    \n    import (\n        \"fmt\"\n    \n        \"github.com/rodkranz/govalidator\"\n    )\n    \n    type User struct {\n        FirstName  *string `validate:\"Required\" alias:\"Name\"`\n        SecondName string  `validate:\"Required\" alias:\"Second\"`\n    }\n    \n    func main() {\n        Validate(\u0026User{FirstName: String(\"Rodrigo\"), SecondName: \"Lopes\"})\n        Validate(\u0026User{})\n    }\n    \n    func Validate(u *User) {\n        isOk, errs := govalidator.Validate(u)\n    \n        if isOk {\n            fmt.Printf(\"The struct %T is valid!\\n\", u)\n        }\n    \n        if errs != nil {\n            for i := range errs {\n                fmt.Printf(\"The field %s is wrong: %v \\n\", errs[i].Field(), errs[i].Message)\n            }\n        }\n    }\n    \n    func String(s string) *string {\n        return \u0026s\n    }\n\n\nResult:\n\n    $ go run main.go\n    The struct *main.User is valid!\n    The field Name is wrong: Required\n    The field Second is wrong: Required\n\n\n### Custom Validation Rules\n\nIf you need to use the specific rule for you case, you can use the `govalidator.Rule` or `govalidator.ParamRule`.\n\nSuppose you want to check if advert id is blocked:\n\n    govalidator.AddParamRule(\u0026govalidator.ParamRule{\n        IsMatch: func(rule string) bool {\n            return strings.HasPrefix(rule, \"AdBlockIDs(\")\n        },\n        IsValid: func(errs govalidator.Errors, rule, name, alias string, v interface{}) (bool, govalidator.Errors) {\n            idAdvert, ok := v.(int)\n            if !ok {\n                return false, errs\n            }\n\n            ids := rule[11: len(rule)-1]\n            for _, idString := range strings.Split(ids, \",\") {\n                id, _ := strconv.Atoi(idString)\n                if id == idAdvert {\n                    errs.Add(name, alias, \"AdIdAllowed\", \"This Advert id is blocked\")\n                    return false, errs\n                }\n            }\n\n            return true, errs\n        },\n    })\n\nIf your rule is simple, you can also use `govalidator.AddRule`, it accepts type `govalidator.Rule`:\n\n\n    govalidator.AddRule(\u0026govalidator.Rule{\n        IsMatch: func(rule string) bool {\n            return rule == \"AdExists\"\n        },\n        IsValid: func(errs govalidator.Errors, name, alias string, v interface{}) (bool, govalidator.Errors) {\n            idAdvert, ok := v.(int)\n            if !ok {\n                return false, errs\n            }\n\n            if idAdvert \u003c 10 {\n                errs.Add(name, alias, \"AdExists\", \"This advert not exist in our database.\")\n            }\n\n            return true, errs\n        },\n    })\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodkranz%2Fgovalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frodkranz%2Fgovalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodkranz%2Fgovalidator/lists"}