{"id":29117209,"url":"https://github.com/tiendc/go-validator","last_synced_at":"2025-12-15T01:58:33.542Z","repository":{"id":204291039,"uuid":"707679439","full_name":"tiendc/go-validator","owner":"tiendc","description":"Intuitive validation library for Golang","archived":false,"fork":false,"pushed_at":"2024-11-27T14:09:21.000Z","size":122,"stargazers_count":30,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-06T07:37:42.846Z","etag":null,"topics":["generic","go","golang","validation","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/tiendc.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":"2023-10-20T12:25:28.000Z","updated_at":"2025-03-23T07:30:38.000Z","dependencies_parsed_at":"2024-07-06T21:21:09.020Z","dependency_job_id":"23677815-f617-4b83-bbaa-3994478939b8","html_url":"https://github.com/tiendc/go-validator","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"7eb8af6eee7aa2f6cc0dd437ffb8987b32fbf5f6"},"previous_names":["tiendc/go-validator"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/tiendc/go-validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tiendc","download_url":"https://codeload.github.com/tiendc/go-validator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-validator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262581514,"owners_count":23331925,"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":["generic","go","golang","validation","validator"],"created_at":"2025-06-29T11:14:15.566Z","updated_at":"2025-12-15T01:58:33.492Z","avatar_url":"https://github.com/tiendc.png","language":"Go","funding_links":[],"categories":["Validation","验证"],"sub_categories":["Utility/Miscellaneous","实用程序/Miscellaneous"],"readme":"[![Go Version][gover-img]][gover] [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![GoReport][rpt-img]][rpt]\n\n# Fast and intuitive validation library for Go\n\nThis lib uses the `Is...` validation functions from the [govalidator](https://github.com/asaskevich/govalidator) project.\n\n## Installation\n\n```shell\ngo get github.com/tiendc/go-validator\n```\n\n## Usage\n\n#### General usage\n```go\n    import (\n        vld \"github.com/tiendc/go-validator\"\n    )\n\n    type Person struct {\n        FirstName string\n        LastName  string\n        Birthdate time.Time\n\n        Unemployed bool\n        Salary     uint\n        Rank       string\n        WorkEmail  string\n        Projects   []string\n        TaskMap    map[string]Task\n    }\n    var p Person\n\n    errs := vld.Validate(\n        // Validate first and last names separately\n        vld.StrLen(\u0026p.FirstName, 3, 30).OnError(\n            vld.SetField(\"first_name\", nil),\n            vld.SetCustomKey(\"ERR_VLD_PERSON_FIRST_NAME_INVALID\"),\n        ),\n        vld.StrLen(\u0026p.FirstName, 3, 30).OnError(\n            vld.SetField(\"last_name\", nil),\n            vld.SetCustomKey(\"ERR_VLD_PERSON_LAST_NAME_INVALID\"),\n        ),\n\n        // OR use this to produce only one error when one of them fails\n        vld.Group(\n            vld.StrLen(\u0026p.FirstName, 3, 30),\n            vld.StrLen(\u0026p.LastName, 3, 30),\n        ).OnError(\n            vld.SetField(\"name\", nil),\n            vld.SetCustomKey(\"ERR_VLD_PERSON_NAME_INVALID\"),\n        ),\n\n        // Birthdate is optional, but when it's present, it must be within 1950 and now\n        vld.When(!p.Birthdate.IsZero()).Then(\n            vld.TimeRange(p.Birthdate, \u003c1950-01-01\u003e, time.Now()).OnError(...),\n        )\n\n        vld.When(!p.Unemployed).Then(\n            vld.Required(\u0026p.Salary),\n            // Work email must be valid\n            vld.StrIsEmail(\u0026p.WorkEmail),\n\n            // Rank must be one of the constants\n            vld.StrIn(\u0026p.Rank, \"Employee\", \"Manager\", \"Director\"),\n            vld.Case(\n                vld.When(p.Rank == \"Manager\").Then(vld.NumGT(\u0026p.Salary, 10000)),\n                vld.When(p.Rank == \"Director\").Then(vld.NumGT(\u0026p.Salary, 30000)),\n            ).Default(\n                vld.NumLT(\u0026p.Salary, 10000),\n            ),\n\n            // Projects are optional, but when they are present, they must be unique and sorted\n            vld.When(len(p.Projects) \u003e 0).Then(\n                vld.SliceUnique(p.Projects).OnError(...),\n                vld.SliceSorted(p.Projects).OnError(...),\n            )\n        ).Else(\n            // When person is unemployed\n            vld.NumEQ(\u0026p.Salary, 0),\n            vld.StrEQ(\u0026p.WorkEmail, \"\"),\n        ),\n\n        // Validate slice elements\n        vld.Slice(p.Projects).ForEach(func(elem int, index int, validator ItemValidator) {\n            validator.Validate(\n                vld.StrLen(\u0026elem, 10, 30).OnError(\n                    vld.SetField(fmt.Sprintf(\"projects[%d]\", index), nil),\n                    vld.SetCustomKey(\"ERR_VLD_PROJECT_NAME_INVALID\"),\n                ),\n            )\n        }),\n\n        // Validate map entries\n        vld.Map(p.TaskMap).ForEach(func(k string, v Task, validator ItemValidator) {\n            validator.Validate(\n                vld.StrLen(\u0026v.Name, 10, 30).OnError(\n                    vld.SetField(fmt.Sprintf(\"taskMap[%s].name\", k), nil),\n                    vld.SetCustomKey(\"ERR_VLD_TASK_NAME_INVALID\"),\n                ),\n            )\n        }),\n\n        // OTHER FUNCTIONS\n        // Pass if at least one of the validations passes\n        vld.OneOf(\n            // List of validations\n        ),\n\n        // Pass if exact one of the validations passes\n        vld.ExactOneOf(\n            // List of validations\n        ),\n\n        // Pass if none of the validations passes\n        vld.NotOf(\n            // List of validations\n        ),\n    )\n\n    for _, e := range errs {\n        detail, warnErr := e.BuildDetail()\n        fmt.Printf(\"%+v\\n\", detail)\n    }\n```\n\n#### Error message localization\n\n- Method 1: inline localization (not recommended)\n```go\n    errs := Validate(\n        NumLTE(\u0026p.Age, 40).OnError(\n            // Override the default template in english\n            SetTemplate(\"Tuổi nhân viên phải nhỏ hơn hoặc bằng {{.Max}}\"),\n        ),\n    )\n\n    for _, e := range errs {\n        detail, warnErr := e.BuildDetail()\n        fmt.Printf(\"%+v\\n\", detail)\n    }\n```\n\n- Method 2: using another localization lib (recommended)\n```go\n    // Supposed you have 2 files defining error messages\n    // In `error_messages.en`:\n    // ERR_VLD_EMPLOYEE_AGE_TOO_BIG = \"Employee {{.EmployeeName}} has age bigger than {{.Max}}\"\n    // In `error_messages.vi`:\n    // ERR_VLD_EMPLOYEE_AGE_TOO_BIG = \"Nhân viên {{.EmployeeName}} có tuổi lớn hơn {{.Max}}\"\n\n    errs := Validate(\n        NumLTE(\u0026p.Age, 40).OnError(\n            // Custom param (the default template doesn't have this one)\n            SetParam(\"EmployeeName\", p.Name),\n            // Custom key to define custom template to use\n            SetCustomKey(\"ERR_VLD_EMPLOYEE_AGE_TOO_BIG\"),\n        ),\n    )\n\n    for _, e := range errs {\n        errKey := e.CustomKey()\n        errParams : = e.Params() // or e.ParamsWithFormatter()\n        errorMsg := translationFunction(errKey, errParams) // You need to provide this function\n        fmt.Printf(\"%+v\\n\", errorMsg)\n    }\n```\n\n#### Custom error param formatter\n\n```go\n    errs := Validate(\n        NumLT(\u0026budget, 1000000).OnError(\n            SetField(\"Budget\", nil),\n        ),\n    )\n\n    // e.BuildDetail() may produce message `Budget must be less than 1000000`,\n    // but you may want a message like: `Budget must be less than 1,000,000`.\n    // Let's use a custom formatter\n\n    errs := Validate(\n        NumLT(\u0026budget, 1000000).OnError(\n            SetField(\"Budget\", nil),\n            SetNumParamFormatter(NewDecimalFormatFunc('.', ',', \"%f\")),\n        ),\n    )\n```\n\n## Contributing\n\n- You are welcome to make pull requests for new functions and bug fixes.\n\n## License\n\n- [MIT License](LICENSE)\n\n[doc-img]: https://pkg.go.dev/badge/github.com/tiendc/go-validator\n[doc]: https://pkg.go.dev/github.com/tiendc/go-validator\n[gover-img]: https://img.shields.io/badge/Go-%3E%3D%201.20-blue\n[gover]: https://img.shields.io/badge/Go-%3E%3D%201.20-blue\n[ci-img]: https://github.com/tiendc/go-validator/actions/workflows/go.yml/badge.svg\n[ci]: https://github.com/tiendc/go-validator/actions/workflows/go.yml\n[cov-img]: https://codecov.io/gh/tiendc/go-validator/branch/main/graph/badge.svg\n[cov]: https://codecov.io/gh/tiendc/go-validator\n[rpt-img]: https://goreportcard.com/badge/github.com/tiendc/go-validator\n[rpt]: https://goreportcard.com/report/github.com/tiendc/go-validator","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiendc%2Fgo-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiendc%2Fgo-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiendc%2Fgo-validator/lists"}