Skip to main content

Module rules

Module rules 

Source
Expand description

Built-in validation rules.

These are ready-to-use Validator implementations for the most common invariants, grouped by what they constrain:

  • lengthNonEmpty, MinLen, MaxLen, LenRange
  • numericPositive, NonNegative, Negative, NonPositive, InRange
  • stringAscii, Alphanumeric, Trimmed

Every built-in rule reports failures as ValidationError, so they share one error type and compose freely with the combinators.

§Examples

use type_lib::combinator::And;
use type_lib::rules::{LenRange, Trimmed};
use type_lib::Refined;

// A display name: 1–32 characters, no surrounding whitespace.
type DisplayName<'a> = Refined<&'a str, And<Trimmed, LenRange<1, 32>>>;

assert!(DisplayName::new("Ada Lovelace").is_ok());
assert!(DisplayName::new("  spaced  ").is_err());

Re-exports§

pub use length::HasLength;
pub use length::LenRange;
pub use length::MaxLen;
pub use length::MinLen;
pub use length::NonEmpty;
pub use numeric::InRange;
pub use numeric::Negative;
pub use numeric::NonNegative;
pub use numeric::NonPositive;
pub use numeric::Positive;
pub use string::Alphanumeric;
pub use string::Ascii;
pub use string::Trimmed;

Modules§

length
Length-based rules for strings, slices, and other measurable values.
numeric
Numeric rules: sign checks and inclusive integer ranges.
string
String-content rules.