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