Skip to main content

Crate koruma

Crate koruma 

Source
Expand description

§koruma

Build Status Docs Crates.io

koruma is a per-field validation framework focused on:

  1. Type Safety: Strongly typed validation error structs generated at compile time.
  2. Ergonomics: Derive macros and validator attributes that minimize boilerplate.
  3. Developer Experience: Optional constructors, nested/newtype validation, and fluent/i18n.

§koruma-collection

Docs Crates.io Crowdin

A curated set of validators built on top of koruma, organized by domain: string, format, numeric, collection, and general-purpose validators.

§Quick Start

use koruma::{Koruma, Validate, validator};

#[validator]
#[derive(Clone, Debug)]
pub struct RangeValidation<T> {
    pub min: T,
    pub max: T,
    #[koruma(value)]
    pub actual: T,
}

impl<T: PartialOrd + Clone> Validate<T> for RangeValidation<T> {
    fn validate(&self, value: &T) -> bool {
        *value >= self.min && *value <= self.max
    }
}

#[derive(Koruma)]
pub struct User {
    #[koruma(RangeValidation::<_>(min = 0, max = 150))]
    pub age: i32,
}

let user = User { age: 200 };
let err = user.validate().unwrap_err();
if let Some(range_err) = err.age().range_validation() {
    println!("age out of range: {}", range_err.actual);
}

§Fluent/i18n

Enable the fluent feature and derive EsFluent on validators for localized messages. Use KorumaAllFluent to format all() results, and koruma-collection with full-fluent to get built-in validators plus translations.

§Examples

Traits§

BuilderWithValue
Trait for validator builders that can receive the value being validated.
NewtypeValidation
Marker trait for newtype structs (single-field wrappers) that derive Koruma.
Validate
Trait for types that can validate a value of type T.
ValidateExt
Trait for structs that derive Koruma and have a validate() method.
ValidationError
Trait for validation error structs that have no errors.

Attribute Macros§

validator
Attribute macro for validator structs.

Derive Macros§

Koruma
Derive macro for generating validation error structs and validate methods.
KorumaAllDisplay
Derive macro for implementing Display on the all() validator enums.
KorumaAllFluent
Derive macro for implementing ToFluentString on the all() validator enums.