Validator

Derive-macro struct validation for Rust, covering email, URL, length, range, and custom rules

Library
Cargo
v0.21.0
2,504stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
59/100Fair
Development Activity56
Maintenance20
Community60
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
78/100Good
Architecture80
Code Quality82
Innovation72
Learning Curve78

Validator is a Rust crate that brings declarative, derive-macro-based validation to structs, inspired by Python’s marshmallow and Django’s validators. Instead of hand-writing imperative checks, you annotate struct fields with attributes like #[validate(email)], #[validate(length(min = 1, max = 10))], or #[validate(range(min = 18, max = 20))], and the validator_derive proc-macro generates a validate() method that returns a structured ValidationErrors map keyed by field name. It supports nested struct and vector validation, struct-level cross-field checks, custom validation functions with optional context arguments, and per-rule custom error messages and codes for i18n. The crate can also be used standalone (without the derive macro) by calling its validation functions directly.

What You Get

  • Built-in validators: email, url, length, range, must_match, contains, does_not_contain, regex, credit_card, non_control_character, and required
  • Nested validation for structs and vectors of structs, with List/Struct/Field error variants that mirror the source shape
  • Struct-level #[validate(schema(function = "..."))] for cross-field checks that run after per-field validation
  • Custom validators via #[validate(custom(function = "..."))], including an optional typed context argument for dependency injection (e.g. a database handle)
  • Per-rule message and code overrides for internationalized, application-specific error text
  • An optional card feature (via card-validate) for credit card number checks, kept out of the default build

Common Use Cases

  • Validating deserialized API request bodies (paired with serde::Deserialize) before they reach business logic
  • Enforcing signup/profile form constraints such as email format, username length, and age ranges
  • Cross-field checks like password confirmation (must_match) or category-dependent rules via struct-level validation
  • Composing validation across nested DTOs, e.g. a SignupData struct containing a Vec<Preference> that each need validating

Under The Hood

Architecture - The workspace splits into validator (the Validate/ValidateArgs traits, built-in validation functions in src/validation/*.rs, and error types in types.rs/traits.rs) and validator_derive (the proc-macro that parses #[validate(...)] attributes and generates the trait implementation), kept as separate crates so consumers can depend on just the runtime validators without the macro if desired. Tech Stack - Rust 2021 edition (MSRV 1.88), built on regex, url, and idna for format validators, serde/serde_derive/serde_json for error serialization, with card-validate and indexmap as opt-in features. Code Quality - The validator_derive_tests crate holds 20+ focused integration test files (one per validator plus nested/custom/schema/skip cases) alongside trybuild-style compile-pass/compile-fail fixtures for macro error messages; the core crates carry MIT licensing and a CI badge for cross-version testing. API Design - The attribute-based API keeps validation declarations colocated with struct fields and close to serde’s own attribute style, so most projects add one derive and a handful of attributes to get structured, field-keyed errors with no boilerplate glue code.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search