{"id":22201844,"url":"https://github.com/jimmycuadra/validations","last_synced_at":"2025-07-27T04:31:12.785Z","repository":{"id":57671419,"uuid":"59427724","full_name":"jimmycuadra/validations","owner":"jimmycuadra","description":"Rust library to provide validations for arbitrary types.","archived":false,"fork":false,"pushed_at":"2016-05-22T18:38:38.000Z","size":570,"stargazers_count":20,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-16T20:08:46.688Z","etag":null,"topics":["library","rust","validation"],"latest_commit_sha":null,"homepage":"https://jimmycuadra.github.io/validations/validations/","language":"Rust","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/jimmycuadra.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}},"created_at":"2016-05-22T18:26:27.000Z","updated_at":"2023-04-09T15:25:39.000Z","dependencies_parsed_at":"2022-09-07T07:51:12.697Z","dependency_job_id":null,"html_url":"https://github.com/jimmycuadra/validations","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmycuadra%2Fvalidations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmycuadra%2Fvalidations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmycuadra%2Fvalidations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmycuadra%2Fvalidations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jimmycuadra","download_url":"https://codeload.github.com/jimmycuadra/validations/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227760018,"owners_count":17815626,"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":["library","rust","validation"],"created_at":"2024-12-02T16:11:03.954Z","updated_at":"2024-12-02T16:11:05.040Z","avatar_url":"https://github.com/jimmycuadra.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# validations\n\nCrate `validations` provides an interface to check the validity of arbitrary types.\n\n* [validations](https://crates.io/crates/validations/) on crates.io\n* [Documentation](https://jimmycuadra.github.io/validations/validations/) for the latest crates.io release\n\n## Overview\n\nThe `Validate` trait provides the `validate` method, which runs arbitrary validation logic and\nreturns a result indicating whether or not the value is valid. A return value of `Ok(())`\nindicates a valid value. A return value of `Err(Errors)` indicates an invalid value,\nand includes details of why the value failed validation.\n\n`Errors` is a container that can hold both general and field-specific validation\nerrors for an invalid value. An individual validation error is represented by\n`Error`, which contains a human-readable error message, and an optional type of the\nprogrammer's choice that includes additional contextual information about the error.\n\nTypes that implement `Validate` should handle validation logic for each of their fields, as\nnecessary. If the type of a field implements `Validate` itself, it's also possible to delegate\nto the field to validate itself and assign any resulting errors back to the parent type's\nerrors.\n\nInstead of implementing `Validate`, another approach is to implement validation logic inside the\nconstructor function of a type `T`, and return `Result\u003cT, Errors\u003e`, preventing an invalid value\nfrom being created in the first place. This may not always be possible, as the value may be\ncreated through other means. For example, the value may be deserialized from a format like JSON\nfrom an external source. In this case, the `Validate` trait allows deserialization logic to be\ndecoupled from domain-level validation logic.\n\n## Examples\n\nValidating a value:\n\n``` rust\nlet entry = AddressBookEntry {\n    cell_number: None,\n    email: Some(Email(\"rcohle@dps.la.gov\")),\n    home_number: Some(PhoneNumber {\n        area_code: \"555\",\n        number: \"555-5555\",\n    }),\n    name: \"Rust Cohle\",\n};\n\nassert!(entry.validate().is_ok());\n```\n\nValidating a value with a non-field-specific error:\n\n``` rust\nlet entry = AddressBookEntry {\n    cell_number: None,\n    email: Some(Email(\"rcohle@dps.la.gov\")),\n    home_number: None,\n    name: \"Rust Cohle\",\n};\n\nlet errors = entry.validate().err().unwrap();\n\nassert_eq!(\n    errors.base().unwrap()[0].message(),\n    \"at least one phone number is required\".to_string()\n);\n```\n\nValidating a value with a field error:\n\n``` rust\nlet entry = AddressBookEntry {\n    cell_number: None,\n    email: Some(Email(\"rcohle@dps.la.gov\")),\n    home_number: Some(PhoneNumber {\n        area_code: \"555\",\n        number: \"555-5555\",\n    }),\n    name: \"\",\n};\n\nlet errors = entry.validate().err().unwrap();\n\nassert_eq!(\n    errors.field(\"name\").unwrap().base().unwrap()[0].message(),\n    \"can't be blank\".to_string()\n);\n```\n\n## License\n\n[MIT](http://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimmycuadra%2Fvalidations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjimmycuadra%2Fvalidations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimmycuadra%2Fvalidations/lists"}