Catfive 🌪️
Catfive is a category-5, fluent, zero-dependency schema and form validation library for Rust.
Inspired by developer-friendly schema libraries like Zod, Catfive gives you composable, expressive validation rules without forcing procedural attribute macros (#[derive(...)]) onto your structs.
Highlights
- ⚡ Macro-Free: Clean, functional, and composable builder API without
#[derive(Validate)]. - 📦 Zero Dependencies by Default: Core validators operate entirely on the Rust standard library (
std). - 🔮 Universal Pattern Engine (
spek): Validate documents and standards via.spek("[...]"):[CPF]: Brazilian CPF (modulo 11 check digits, repeated digits rejection).[CNPJ]: Brazilian CNPJ (native support for both classic numeric and the new 2026 alphanumeric standard IN RFB 2229/2024).[LUHN]: ISO/IEC 7812 check digit algorithm (credit cards, IMEI).[HEX],[BASE64],[BASE58],[ASCII 65..90], and custom literal charsets like[ABC234].
- 🔄 Cloneable Validators: Every validator implements
Clonefor easy template reuse across your codebase. - 💬 Custom & Template Error Messages: Format errors like
println!with dynamic placeholders ({char},{index},{min},{count},{value}) or closures. - 📋 Form & Struct Schemas: Validate untyped key-value form submissions or strongly-typed Rust structs with nested schemas and cross-field refinements.
- 📚 Collection Validation: Validate lists, vectors, and slices using
catfive::listwith uniqueness and length rules. - 🎯 Optional Features:
serde: SerializeValidationErrorsdirectly to JSON for web APIs (Axum, Actix-web, Rocket).regex: Enable regular expression pattern matching.
Installation
Add Catfive to your Cargo.toml:
[]
= "0.2.0"
With optional features:
[]
= { = "0.2.0", = ["serde", "regex"] }
Quick Start
1. Custom & Template Error Messages (println!-style)
You can let Catfive use its precise default error messages (which pinpoint the exact offending character), or provide your own message using template placeholders or closures:
use string;
// Using template placeholders:
let username_val = string
.min_msg
.block_msg;
let err = username_val.validate.unwrap_err;
println!; // "Found forbidden character '@' at position 4"
// Using a custom closure:
let strict_val = string.block_with;
2. Universal Pattern Engine: .spek("[...]")
Validate Brazilian documents, standard encodings, mathematical algorithms, and custom character sets with a single declarative method:
use string;
// 1. Brazilian CNPJ (natively supports classic numeric AND new 2026 alphanumeric IN RFB 2229/2024):
let cnpj_val = string.spek.block;
assert!; // Classic numeric
assert!; // New 2026 alphanumeric
// 2. Brazilian CPF:
let cpf_val = string.spek;
assert!;
// 3. Mathematical Checksum (Luhn algorithm for credit cards / IMEI):
let card_val = string.spek;
// 4. ASCII ranges & custom character specifications:
let uppercase = string.spek; // 'A'..='Z'
let custom_charset = string.spek.block;
// 5. Encodings:
let hex_val = string.spek;
let b64_val = string.spek;
let b58_val = string.spek;
3. Validating Structs (Optional Fields, Refinements & Nested Schemas)
Define schemas for arbitrary structs using typed field selectors, cross-field validations, and nested schemas:
use ;
4. Validating Collections & Arrays (catfive::list)
Validate vectors and slices with collection-level constraints and individual item checks:
use ;
let tags_validator =
.min_items
.max_items
.unique;
let tags = vec!;
assert!;
let duplicate_tags = vec!;
assert!; // "unique_items"
5. Validating Forms (Key-Value Payloads)
Perfect for HTTP requests, query params, and HTML forms:
use HashMap;
use ;
let signup_form = form
.field
.field
.optional_field;
let mut form_data = new;
form_data.insert;
form_data.insert;
assert!;
6. Built-in String Validators
.spek("[...]")/.spek_msg("[...]", "...").email()/.email_msg(...).url()/.url_msg(...).uuid()/.uuid_msg(...).ip()/.ip_msg(...).in_list(&["admin", "user"])/.in_list_msg(...).contains("substr")/.contains_msg(...).starts_with("prefix")/.ends_with("suffix").alphanumeric()/.alphabetic()/.numeric().block("chars")/.allow_only("chars").regex(pattern)(requiresregexfeature)
7. JSON Serialization for Web APIs (serde feature)
Enable the serde feature in Cargo.toml to serialize errors directly into JSON responses (e.g. HTTP 422 Unprocessable Entity in Axum or Actix-web):
License
This project is licensed under the MIT License.