{"id":13412876,"url":"https://github.com/cinar/checker","last_synced_at":"2026-01-12T00:42:24.418Z","repository":{"id":176353575,"uuid":"653430265","full_name":"cinar/checker","owner":"cinar","description":"Effortless input validation in Go with the power of struct tags. No dependencies, just pure simplicity. ✨  See how! 👀","archived":false,"fork":false,"pushed_at":"2025-01-03T18:35:35.000Z","size":250,"stargazers_count":38,"open_issues_count":7,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-07T17:45:37.268Z","etag":null,"topics":["checker","customizable","data-integrity","data-validation","form-validation","go","golang","input-validation","library","lightweight","localization","no-dependencies","normalization","security","struct-tags","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/cinar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-14T03:27:57.000Z","updated_at":"2025-03-03T12:40:02.000Z","dependencies_parsed_at":"2025-01-03T19:27:49.335Z","dependency_job_id":"d942344e-df6c-474c-b3db-20348398eab0","html_url":"https://github.com/cinar/checker","commit_stats":null,"previous_names":["cinar/checker"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cinar%2Fchecker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cinar%2Fchecker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cinar%2Fchecker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cinar%2Fchecker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cinar","download_url":"https://codeload.github.com/cinar/checker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243625195,"owners_count":20321253,"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":["checker","customizable","data-integrity","data-validation","form-validation","go","golang","input-validation","library","lightweight","localization","no-dependencies","normalization","security","struct-tags","validation","validator"],"created_at":"2024-07-30T20:01:30.484Z","updated_at":"2026-01-12T00:42:24.412Z","avatar_url":"https://github.com/cinar.png","language":"Go","readme":"[![GoDoc](https://godoc.org/github.com/cinar/checker?status.svg)](https://godoc.org/github.com/cinar/checker)\n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n[![Go Report Card](https://goreportcard.com/badge/github.com/cinar/checker)](https://goreportcard.com/report/github.com/cinar/checker)\n![Go CI](https://github.com/cinar/checker/actions/workflows/ci.yml/badge.svg)\n[![codecov](https://codecov.io/gh/cinar/checker/branch/main/graph/badge.svg?token=VO9BYBHJHE)](https://codecov.io/gh/cinar/checker)\n\n# Checker\n\nChecker is a lightweight Go library designed to validate user input efficiently. It supports validation of both struct fields and individual input values.\n\nWhile there are numerous validation libraries available, Checker stands out due to its simplicity and lack of external dependencies. This makes it an ideal choice for developers who prefer to minimize dependencies and maintain control over their tools. Checker is straightforward to use and effectively meets your validation needs.\n\n## Usage\n\nTo begin using the Checker library, install it with the following command:\n\n```bash\ngo get github.com/cinar/checker/v2\n```\n\nThen, import the library into your source file as shown below:\n\n```golang\nimport (\n\tchecker \"github.com/cinar/checker/v2\"\n)\n```\n\n### Validating User Input Stored in a Struct\n\nChecker can validate user input stored in a struct by listing the checkers in the struct tags for each field. Here is an example:\n\n```golang\ntype Person struct {\n\tName string `checkers:\"trim required\"`\n}\n\nperson := \u0026Person{\n\tName: \" Onur Cinar \",\n}\n\nerrors, valid := checker.CheckStruct(person)\nif !valid {\n\t// Handle validation errors\n}\n```\n\n### Validating Individual User Input with Multiple Checkers\n\nYou can also validate individual user input by calling checker functions directly. Here is an example:\n\n```golang\nname := \" Onur Cinar \"\n\nname, err := checker.Check(name, checker.Trim, checker.Required)\nif err != nil {\n\t// Handle validation error\n}\n```\n\nThe checkers and normalizers can also be provided through a config string. Here is an example:\n\n```golang\nname := \" Onur Cinar \"\n\nname, err := checker.CheckWithConfig(name, \"trim requied\")\nif err != nil {\n\t// Handle validation error\n}\n\n```\n\n### Validating Individual User Input\n\nFor simpler validation, you can call individual checker functions. Here is an example:\n\n```golang\nname := \"Onur Cinar\"\n\nerr := checker.IsRequired(name)\nif err != nil {\n\t// Handle validation error\n}\n```\n\n## Normalizers and Checkers\n\nCheckers validate user input, while normalizers transform it into a preferred format. For example, a normalizer can trim spaces from a string or convert it to title case.\n\nAlthough combining checkers and normalizers into a single library might seem unconventional, using them together can be beneficial. They can be mixed in any order when defining validation steps. For instance, you can use the `trim` normalizer with the `required` checker to first trim the input and then ensure it is provided. Here is an example:\n\n```golang\ntype Person struct {\n\tName string `checkers:\"trim required\"`\n}\n```\n\n# Checkers Provided\n\n- [`ascii`](DOC.md#IsASCII): Ensures the string contains only ASCII characters.\n- [`alphanumeric`](DOC.md#IsAlphanumeric): Ensures the string contains only letters and numbers.\n- [`credit-card`](DOC.md#IsAnyCreditCard): Ensures the string is a valid credit card number.\n- [`cidr`](DOC.md#IsCIDR): Ensures the string is a valid CIDR notation.\n- [`digits`](DOC.md#IsDigits): Ensures the string contains only digits.\n- [`email`](DOC.md#IsEmail): Ensures the string is a valid email address.\n- [`fqdn`](DOC.md#IsFQDN): Ensures the string is a valid fully qualified domain name.\n- [`gte`](DOC.md#IsGte): Ensures the value is greater than or equal to the specified number.\n- [`hex`](DOC.md#IsHex): Ensures the string contains only hexadecimal digits.\n- [`ip`](DOC.md#IsIP): Ensures the string is a valid IP address.\n- [`ipv4`](DOC.md#IsIPv4): Ensures the string is a valid IPv4 address.\n- [`ipv6`](DOC.md#IsIPv6): Ensures the string is a valid IPv6 address.\n- [`isbn`](DOC.md#IsISBN): Ensures the string is a valid ISBN.\n- [`lte`](DOC.md#ISLte): Ensures the value is less than or equal to the specified number.\n- [`luhn`](DOC.md#IsLUHN): Ensures the string is a valid LUHN number.\n- [`mac`](DOC.md#IsMAC): Ensures the string is a valid MAC address.\n- [`max-len`](DOC.md#func-maxlen): Ensures the length of the given value (string, slice, or map) is at most n.\n- [`min-len`](DOC.md#func-minlen): Ensures the length of the given value (string, slice, or map) is at least n.\n- [`required`](DOC.md#func-required) Ensures the value is provided.\n- [`regexp`](DOC.md#func-makeregexpchecker) Ensured the string matches the pattern.\n- [`time`](DOC.md#func-istime) Ensured the string matches the provided time layout.\n- [`url`](DOC.md#IsURL): Ensures the string is a valid URL.\n\n# Normalizers Provided\n\n- [`lower`](DOC.md#Lower): Converts the string to lowercase.\n- [`title`](DOC.md#Title): Converts the string to title case.\n- [`trim-left`](DOC.md#TrimLeft): Trims whitespace from the left side of the string.\n- [`trim-right`](DOC.md#TrimRight): Trims whitespace from the right side of the string.\n- [`trim`](DOC.md#TrimSpace): Trims whitespace from both sides of the string.\n- [`upper`](DOC.md#Upper): Converts the string to uppercase.\n- [`html-escape`](DOC.md#HTMLEscape): Escapes special characters in the string for HTML.\n- [`html-unescape`](DOC.md#HTMLUnescape): Unescapes special characters in the string for HTML.\n- [`url-escape`](DOC.md#URLEscape): Escapes special characters in the string for URLs.\n- [`url-unescape`](DOC.md#URLUnescape): Unescapes special characters in the string for URLs.\n\n# Custom Checkers and Normalizers\n\nYou can define custom checkers or normalizers and register them for use in your validation logic. Here is an example of how to create and register a custom checker:\n\n```golang\nchecker.RegisterMaker(\"is-fruit\", func(params string) v2.CheckFunc[reflect.Value] {\n\treturn func(value reflect.Value) (reflect.Value, error) {\n\t\tstringValue := value.Interface().(string)\n\n\t\tif stringValue == \"apple\" || stringValue == \"banana\" {\n\t\t\treturn value, nil\n\t\t}\n\n\t\treturn value, v2.NewCheckError(\"NOT_FRUIT\")\n\t}\n})\n```\n\nIn this example, the custom checker `is-fruit` checks if the input value is either \"apple\" or \"banana\". If the value is not one of these, it returns an error.\n\nOnce registered, you can use your custom checker in struct tags just like the built-in checkers:\n\n```golang\ntype Item struct {\n\tName string `checkers:\"is-fruit\"`\n}\n\nitem := \u0026Item{\n\tName: \"banana\",\n}\n\nerrors, valid := v2.CheckStruct(item)\nif !valid {\n\tfmt.Println(errors)\n}\n```\n\nIn this example, the `is-fruit` checker is used to validate that the `Name` field of the `Item` struct is either \"apple\" or \"banana\".\n\n# Slice and Item Level Checkers\n\nWhen adding checker struct tags to a slice, you can use the `@` prefix to indicate that the checker should be applied to the slice itself. Checkers without the `@` prefix will be applied to the individual items within the slice. Here is an example:\n\n```golang\ntype Person struct {\n\tName   string   `checkers:\"required\"`\n\tEmails []string `checkers:\"@max-len:2 max-len:64\"`\n}\n```\n\nIn this example:\n- `@max-len:2` ensures that the `Emails` slice itself has at most two items.\n- `max-len:64` ensures that each email string within the `Emails` slice has a maximum length of 64 characters.\n\n# Localized Error Messages\n\nWhen validation fails, Checker returns an error. By default, the [Error()](DOC.md#CheckError.Error) function provides a human-readable error message in `en-US` locale.\n\n```golang\n_, err := checker.IsEmail(\"abcd\")\nif err != nil {\n\tfmt.Println(err)\n\t// Output: Not a valid email address.\n}\n```\n\nTo get error messages in other languages, use the [ErrorWithLocale()](DOC.md#CheckError.ErrorWithLocale) function. By default, only `en-US` is registered. You can register additional languages by calling [RegisterLocale](DOC.md#RegisterLocale).\n\n```golang\n// Register de-DE localized error messages.\nchecker.RegisterLocale(locales.DeDE, locales.DeDEMessages)\n\n_, err := checker.IsEmail(\"abcd\")\nif err != nil {\n\tfmt.Println(err.ErrorWithLocale(locales.DeDE))\n\t// Output: Keine gültige E-Mail-Adresse.\n}\n```\n\nYou can also customize existing error messages or add new ones to `locales.EnUSMessages` and other locale maps.\n\n```golang\n// Register the en-US localized error message for the custom NOT_FRUIT error code.\nlocales.EnUSMessages[\"NOT_FRUIT\"] = \"Not a fruit name.\"\n\nerrors, valid := v2.CheckStruct(item)\nif !valid {\n\tfmt.Println(errors)\n\t// Output: map[Name:Not a fruit name.]\n}\n```\n\nError messages are generated using Golang template functions, allowing them to include variables.\n\n```golang\n// Custrom checker error containing the item name.\nerr := checker.NewCheckErrorWithData(\n\t\"NOT_FRUIT\",\n\tmap[string]interface{}{\n\t\t\"name\": \"abcd\",\n\t},\n)\n\n// Register the en-US localized error message for the custom NOT_FRUIT error code.\nlocales.EnUSMessages[\"NOT_FRUIT\"] = \"Name {{ .name }} is not a fruit name.\"\n\nerrors, valid := v2.CheckStruct(item)\nif !valid {\n\tfmt.Println(errors)\n\t// Output: map[Name:Name abcd is not a fruit name.]\n}\n```\n\n# Contributing to the Project\n\nAnyone can contribute to Checkers library. Please make sure to read our [Contributor Covenant Code of Conduct](./CODE_OF_CONDUCT.md) guide first. Follow the [How to Contribute to Checker](./CONTRIBUTING.md) to contribute.\n\n# License\n\nThis library is free to use, modify, and distribute under the terms of the MIT license. The full license text can be found in the [LICENSE](./LICENSE) file.\n\nThe MIT license is a permissive license that allows you to do almost anything with the library, as long as you retain the copyright notice and the license text. This means that you can use the library in commercial products, modify it, and redistribute it without having to ask for permission from the authors.\n\nThe [LICENSE](./LICENSE) file is located in the root directory of the library. You can open it in a text editor to read the full license text.\n","funding_links":[],"categories":["Forms","表单"],"sub_categories":["Search and Analytic Databases","检索及分析资料库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcinar%2Fchecker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcinar%2Fchecker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcinar%2Fchecker/lists"}