https://github.com/cakekindel/engsure-rs
Model validation that humans can read - pr. "ensure"
https://github.com/cakekindel/engsure-rs
composable data-oriented declarative fluent functional library model readable requirements rust validation validation-library well-documented
Last synced: about 1 year ago
JSON representation
Model validation that humans can read - pr. "ensure"
- Host: GitHub
- URL: https://github.com/cakekindel/engsure-rs
- Owner: cakekindel
- License: mit
- Created: 2020-05-24T19:18:44.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-26T15:33:41.000Z (about 6 years ago)
- Last Synced: 2025-02-10T01:37:00.085Z (over 1 year ago)
- Topics: composable, data-oriented, declarative, fluent, functional, library, model, readable, requirements, rust, validation, validation-library, well-documented
- Language: Rust
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# engsure
## Model validation that humans can read
_pronounced like "ensure"_
## Table of Contents
- [How to Use](#how-to-use)
- [API Docs](#api-docs)
- [Best Practices](#best-practices)
- [Simple Example](#simple-example)
- [How to Contribute](#how-to-contribute)
## How to Use
### API Docs
TODO
### Best Practices
How to get the most out of `engsure`
#### provide descriptive labels with `aka` as much as possible
This makes your error messages significantly more valuable, by having a human-readable name for the item being validated.
#### limit `use`s from `engsure` as much as possible
the API was designed with concise names, that way you can use fully-qualified names rather than bringing them directly into scope.
#### implement the `engsure::Validate` trait
This has 2 benefits:
1. it lets you separately define `engsure` functionality from the rest of your struct's functionality
2. by returning the `engsure::Ctx`, you have more flexibility on what you can do _after_ validating.
- _e.g._ chain validation of other instances with `and_then` (see the `main` fn in [Simple Example](#simple-example))
### Simple Example
```rust
struct Person {
pub first_name: String,
pub last_name: String,
pub phone_number: (i8, i8, i8),
pub email: Option
}
impl Person {
pub fn name(&self) -> String {
format!("{} {}", self.first_name, self.last_name)
}
}
impl engsure::Validate for Person {
use engsure::opt::{is_some};
use engsure::tup::{is_phone_number};
use engsure::txt::{is_email_address};
pub fn validate(self) -> engsure::Ctx {
engsure::that(self)
.aka(self.name())
.has(|p| p.phone_number)
.that(is_phone_number)
.and()
.has(|p| p.email)
.when(is_some)
.that(is_email_address)
.done()
}
}
pub fn main() -> engsure::Result {
let bob = Person {
first_name: "Bob",
last_name: "Builder",
phone_number: (505, 555, 1234),
email: Some("bob.t.builder@yeswecan.org")
}
let sally = Person {
first_name: "Sally",
last_name: "Jackson",
phone_number: (602, 111, 2222),
email: Some("forgot-dotcom@gmail")
};
// The following errors with:
// engsure::TextError { msg: "invalid email of 'forgot-dotcom@gmail' in 'Sally Jackson'", .. }
bob.validate()
.and_then(|_| sally.validate())
.done()
}
```
## How to Contribute
TODO