Skip to main content

catfive/
lib.rs

1//! # Catfive
2//!
3//! **Catfive** is a category-5, fluent, zero-dependency schema and form validation library for Rust.
4//! Inspired by developer-friendly validation libraries, it lets you construct composable validation
5//! rules without procedural attribute macros (`#[derive(...)]`).
6//!
7//! ## Key Features
8//! - **Macro-free**: Clean, functional, and composable builder API.
9//! - **Zero dependencies**: Core validation operates entirely on `std`.
10//! - **Cloneable Validators**: All validators implement `Clone` for easy template reuse.
11//! - **Detailed errors & Custom Messages**: Rules support `println!`-style placeholders (e.g. `{char}`, `{min}`, `{count}`) or closures.
12//! - **Form and Schema support**: Validate untyped form payloads as well as strongly-typed Rust structs with nested schemas and refinements.
13//! - **Collections & Lists**: Validate arrays and vectors with `list(...)`.
14//! - **Optional Serde**: Enable the `serde` Cargo feature to serialize validation errors to JSON for REST APIs.
15//! - **Optional Regex**: Enable the `regex` Cargo feature for arbitrary pattern matching.
16
17pub mod error;
18pub mod form;
19pub(crate) mod format_helper;
20pub mod list;
21pub mod number;
22pub mod schema;
23pub mod spek;
24pub mod string;
25pub mod traits;
26
27// Re-exports of primary types
28pub use error::{FieldError, ValidationError, ValidationErrors};
29pub use form::{FormData, FormValidator};
30pub use list::ListValidator;
31pub use number::NumberValidator;
32pub use schema::Schema;
33pub use string::StringValidator;
34pub use traits::Validator;
35
36/// Creates a new fluent string validator.
37///
38/// # Example
39/// ```rust
40/// use catfive::string;
41///
42/// let name_val = string().min(3).max(20).block(";@#$&");
43/// ```
44pub fn string() -> StringValidator {
45    StringValidator::new()
46}
47
48/// Creates a new fluent numeric validator for any compatible number type.
49///
50/// # Example
51/// ```rust
52/// use catfive::number;
53///
54/// let age_val = number::<u8>().min(18).max(120);
55/// ```
56pub fn number<T>() -> NumberValidator<T>
57where
58    T: PartialOrd + Copy + std::fmt::Display + Send + Sync + 'static,
59{
60    NumberValidator::new()
61}
62
63/// Creates a new fluent list/array validator for collections of items.
64///
65/// # Example
66/// ```rust
67/// use catfive::{list, string, Validator};
68///
69/// let tags_val = list::<&str, _>(string().min(2)).min_items(1).max_items(5);
70/// let tags = vec!["rust", "actix"];
71/// assert!(tags_val.validate(&tags).is_ok());
72/// ```
73pub fn list<T, Val>(item_validator: Val) -> ListValidator<T, Val>
74where
75    T: Send + Sync + 'static,
76    Val: Validator<T> + Send + Sync + 'static,
77{
78    ListValidator::new(item_validator)
79}
80
81/// Creates a new form validator for key-value payloads.
82///
83/// # Example
84/// ```rust
85/// use catfive::{form, string};
86///
87/// let contact_form = form()
88///     .field("username", string().min(3).block(";@#$&"))
89///     .field("email", string().email());
90/// ```
91pub fn form() -> FormValidator {
92    FormValidator::new()
93}
94
95/// Creates a new strongly-typed schema validator for struct `T`.
96///
97/// # Example
98/// ```rust
99/// use catfive::{schema, string};
100///
101/// struct User {
102///     username: String,
103/// }
104///
105/// let user_schema = schema::<User>()
106///     .field("username", |u| &u.username, string().min(3).block(";@#$&"));
107/// ```
108pub fn schema<T>() -> Schema<T> {
109    Schema::new()
110}
111
112/// Convenient prelude module to import all essential traits and constructors.
113pub mod prelude {
114    pub use crate::error::{FieldError, ValidationError, ValidationErrors};
115    pub use crate::form::{FormData, FormValidator};
116    pub use crate::list::ListValidator;
117    pub use crate::number::NumberValidator;
118    pub use crate::schema::Schema;
119    pub use crate::string::StringValidator;
120    pub use crate::traits::Validator;
121    pub use crate::{form, list, number, schema, string};
122}