valitron/
lib.rs

1//! Valitron is an ergonomics, functional and configurable validator.
2//!
3//! ## This is an example:
4//!
5//! ```rust
6//! # use serde::{Deserialize, Serialize};
7//! # use valitron::{
8//! # available::{Message, Required, StartWith},
9//! # custom, RuleExt, Validator
10//! # };
11//! #[derive(Serialize, Debug)]
12//! struct Person {
13//!     introduce: &'static str,
14//!     age: u8,
15//!     weight: f32,
16//! }
17//!
18//! # fn main() {
19//! let validator = Validator::new()
20//!     .rule("introduce", Required.and(StartWith("I am")))
21//!     .rule("age", custom(age_range))
22//!     .message([
23//!         ("introduce.required", "introduce is required"),
24//!         (
25//!             "introduce.start_with",
26//!             "introduce should be starts with `I am`",
27//!         ),
28//!     ]);
29//!
30//! let person = Person {
31//!     introduce: "hi",
32//!     age: 18,
33//!     weight: 20.0,
34//! };
35//!
36//! let res = validator.validate(person).unwrap_err();
37//! assert!(res.len() == 2);
38//! # }
39//!
40//! fn age_range(age: &mut u8) -> Result<(), Message> {
41//!     if *age >= 25 && *age <= 45 {
42//!         Ok(())
43//!     } else {
44//!         Err("age should be between 25 and 45".into())
45//!     }
46//! }
47//! ```
48//!
49//! The second scheme is [string].
50//!
51//! ## Prerequisite
52//!
53//! input data needs implementation `serde::Serialize`, and if you want to modify data,
54//! it should be also implementation `serde::Deserialize`
55//!
56//! ## Available Rules
57//!
58//! - [`Compare`]
59//! - [`Confirm`]
60//! - [`Contains`]
61//! - [`Email`]
62//! - [`EndWith`]
63//! - [`Length`]
64//! - [`Not`]
65//! - [`Range`]
66//! - [`Regex`]
67//! - [`Required`]
68//! - [`StartWith`]
69//! - [`Trim`]
70//! - customizable
71//!
72//! To get started using all of Valitron's optional rule, add this to your
73//! `Cargo.toml`:
74//!
75//! ```toml
76//! valitron = { version = "0.1", features = ["full"] }
77//! ```
78//!
79//! ## Closure Rule
80//!
81//! This is support closure with a primitive type mutable reference arguments and returning `message type`.
82//!
83//! > ### Tip
84//! > `message type` is [`Validator`]'s only one generics argument, default is `String`, but when `full` features
85//! > enabled default is [`Message`], and it is customable. And it's not immutable, it can be changed by [`map`] method.
86//!
87//! ## Custom Rule
88//!
89//! anything types implemented [`Rule`] trait can be used as a rule
90//!
91//! [`map`]: crate::register::Validator::map
92//! [`Rule`]: crate::Rule
93//! [`Message`]: crate::available::Message
94//! [`Required`]: crate::available::required
95//! [`Email`]: crate::available::email
96//! [`Compare`]: crate::available::compare
97//! [`Contains`]: crate::available::contains
98//! [`StartWith`]: crate::available::start_with
99//! [`EndWith`]: crate::available::end_with
100//! [`Confirm`]: crate::available::confirm
101//! [`Trim`]: crate::available::trim
102//! [`Length`]: crate::available::length
103//! [`Not`]: crate::available::not
104//! [`Range`]: crate::available::range
105//! [`Regex`]: crate::available::regex
106//! [string]: crate::register::string
107
108#![cfg_attr(test, allow(unused_imports, dead_code))]
109#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
110//#![warn(clippy::unwrap_used)]
111//#![doc(html_playground_url = "https://play.rust-lang.org/")]
112
113mod de;
114pub mod register;
115pub mod rule;
116mod ser;
117pub mod value;
118
119#[macro_use]
120pub(crate) mod macros;
121
122pub use register::{ValidPhrase, Validatable, Validator};
123pub use rule::{custom, Rule, RuleExt};
124pub use value::{FromValue, Value, ValueMap};
125
126#[cfg(feature = "full")]
127pub use rule::available;