This is an automated archive made by the Lemmit Bot.
The original was posted on /r/rust by /u/Novel_Principle_3831 on 2024-11-03 11:53:17+00:00.
Description
I wanted to create a crate that would allow for type-based validation and make my programs safer at runtime, so I made a crate called refined_type
. It supports things like numeric types constrained to specific ranges, arrays that meet certain conditions, non-empty strings, and more.
I’ve also included features to easily compose validation rules together.
URLs
- GitHub:
- Docs:
- crates.io:
The appeal of refined_type
By combining it with serde
, you can validate JSON or YAML based on types.
Example
MinMax
type
MinMax
is a type that signifies the target exists between a certain number and another number.
rust type Age = MinMaxU8<18, 80>;
fn min\_max\_example() -> Result<(), Error> {
let age = Age::new(18)?;
assert\_eq!(age.into\_value(), 18);
let age = Age::new(80)?; assert_eq!(age.into_value(), 80);
let age = Age::new(17); assert!(age.is_err());
let age = Age::new(81); assert!(age.is_err()); Ok(())
}
ForAll
type
ForAll
is a rule that applies a specific rule to all elements in the Iterator.
fn for\_all\_example() -> anyhow::Result<()> {
let vec = vec!["Hello".to\_string(), "World".to\_string()];
let for\_all\_ok = ForAllVec::::new(vec.clone())?;
assert\_eq!(vec, for\_all\_ok.into\_value());
let vec = vec![“Hello”.to_string(), “”.to_string()]; let for_all_err = ForAllVec::::new(vec.clone()); assert!(for_all_err.is_err()); Ok(())
}
Define complex type
We can create types with complex conditions using And
, Or
, and Not
.
type Target = Refined<
Or![
And![
MinMaxRuleU8<0, 50>,
EvenRuleU8,
Not, EqualRuleU8<20>]>
],
And![
MinMaxRuleU8<60, 100>,
OddRuleU8,
Not, EqualRuleU8<85>]>
]
]
>
> ;
> rust
> fn complex\_rule\_example() -> Result<(), Error> {
> let target = Target::new(0)?;
> assert\_eq!(target.into\_value(), 0);
>
>
>
let target = Target::new(2)?; assert_eq!(target.into_value(), 2);
let target = Target::new(10); assert!(target.is_err());
let target = Target::new(20); assert!(target.is_err());
let target = Target::new(50)?; assert_eq!(target.into_value(), 50);
let target = Target::new(60); assert!(target.is_err());
let target = Target::new(61)?; assert_eq!(target.into_value(), 61);
let target = Target::new(75); assert!(target.is_err());
let target = Target::new(85); assert!(target.is_err());
let target = Target::new(100); assert!(target.is_err());
let target = Target::new(101); assert!(target.is_err());
Ok(())
}