8 releases (breaking)

0.8.0 Jan 24, 2025
0.7.0 Jan 24, 2025
0.6.0 Jan 21, 2025
0.5.0 Jan 16, 2025
0.1.0 Jan 10, 2025

#232 in Debugging

Download history 320/week @ 2025-01-07 191/week @ 2025-01-14 347/week @ 2025-01-21 8/week @ 2025-01-28 12/week @ 2025-02-04

608 downloads per month
Used in valust-axum

MIT/Apache

19KB
291 lines

Valust - Validator for Rust

Crate VersionGitHub top language Crates.io Downloads (recent)


Valust aims to provide a user-friendly, auto-generated data validation tool.

By leveraging Rust's powerful procedural macro system, valust can automatically generate everything you need for data validation with just a few simple attributes added to your data structures.

Example

use valust::{Validate, Raw};
use valust_derive::Valust;

#[derive(Debug, Valust, PartialEq)]
#[forward_derive(Debug)]
#[rename(UncheckedUsername)]
pub struct Username(
    #[trans(expr(String => _0.trim().to_owned()))]
    #[valid(expr(!_0.is_empty(), "username must not be empty"))]
    pub String,
);

#[derive(Debug, Valust, PartialEq)]
#[forward_derive(Debug)]
#[post(user_id + (username.0.len() as u32) == *magic_number)]
pub struct UserProfile {
    pub user_id: u32,
    #[forward]
    pub username: Username,
    pub magic_number: u32,
}

let raw_profile = Raw::<UserProfile> {
    user_id: 10,
    username: UncheckedUsername("  Foo  ".into()),
    magic_number: 13,
};

let profile = UserProfile::validate(raw_profile).expect("Check failed");

assert_eq!(profile, UserProfile {
    user_id: 10,
    username: Username("Foo".into()),
    magic_number: 13
});

Project Structure

Core

  • valust: Main crate, exposing fundamental traits (trait Validate) and error types.
  • valust-derive: Derive macro for creating a validate-able struct.

External Tools & Utilities

Minimum Supported Rust Version (MSRV)

The MSRV of this project is 1.78.0 (With lockfile version 4), but is possibly able to compile unlocked with Rust 1.74.1 (With lockfile version 3).

And for development, the project requires Rust 1.83.0 to work on the source code.

Dependencies

~0.2–1.2MB
~24K SLoC