#specification #validation #rust

specler

A simple way to write specifications on values

17 breaking releases

0.18.0 Dec 16, 2024
0.16.0 Dec 16, 2024
0.2.0 Nov 29, 2024

#893 in Rust patterns

Download history 431/week @ 2024-11-27 857/week @ 2024-12-04 767/week @ 2024-12-11 100/week @ 2024-12-18

1,104 downloads per month

CC0 license

31KB
634 lines

Specler

A simple and lightweight spec definition and validation library for Rust.


lib.rs:

Specler

A simple library for defining and validating specifications on your types. This solves an issue with other validation approaches where it is possible to first create a type and then verify its validity. In disagreeing with this approach, a solution was needed to verify specifications in factory methods.

Concepts

A specification defines a list of requirements that a type must meet. These requirements are expressed as a list of validators, which are simple functions returning a ValidatorResult. These then get compiled into a single SpecValidationResult.

Examples

Validating using an empty spec

use crate::specler::core::require::Require;
use crate::specler::core::spec_error::SpecError;

let spec = Require::<String>::to();
let result = spec.validate("");

assert!(result.is_ok());

Validating a string to not be empty

use specler::{assert_spec_error_msg};
use specler::core::require::Require;
use specler::specs::string::not_empty;
use crate::specler::core::spec_error::SpecError;

let spec = Require::<String>::to().be(not_empty());
let result = spec.validate("");

assert!(result.is_err());
assert_spec_error_msg!(result, "cannot be empty");

Validating the length of a string

use specler::core::require::Require;
use specler::specs::string::{no_longer_than, no_shorter_than};
let spec = Require::<String>::to()
    .be(no_longer_than(4))
    .be(no_shorter_than(2));
let result = spec.validate("abc");;

assert!(result.is_ok());

Validating a string to match a pattern

#
let spec = Require::<String>::to()
   .be(matching(r"^[a-zA-Z]+$"));
let result = spec.validate("oops123");

assert!(!result.is_ok());
assert_spec_error_msg!(result, "does not match the pattern '^[a-zA-Z]+$'");

Dependencies

~2.5–4MB
~73K SLoC