1#![warn(
2 missing_docs,
3 rust_2018_idioms,
4 clippy::pedantic,
5 missing_debug_implementations
6)]
7#![cfg_attr(docsrs, feature(doc_cfg))]
8
9#![cfg_attr(
30 feature = "derive",
31 doc = r#"
32```rust
33use valid8::Validate;
34
35#[derive(Validate)]
36struct User {
37 #[validate(required)]
38 username: String,
39 #[validate(required, email)]
40 email: String,
41 #[validate(required, min(8))]
42 password: String,
43}
44
45let valid = User {
46 username: String::from("Joe"),
47 email: String::from("joe@example.com"),
48 password: String::from("12345678"),
49};
50
51assert!(valid.validate().is_ok());
52
53let invalid = User {
54 username: String::from(""),
55 email: String::from("joe@example"),
56 password: String::from("1234567"),
57};
58
59assert!(invalid.validate().is_err());
60```
61"#
62)]
63
64pub mod validator;
66
67pub use regex;
68pub use validator::Validator;
69
70#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
71#[cfg(feature = "derive")]
72pub use valid8_derive::Validate;
73
74#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
75#[cfg(feature = "derive")]
76#[derive(Debug)]
78pub enum ValidationError {
79 Missing(String),
81 Email,
83 Min(String),
85}
86
87#[cfg(feature = "derive")]
88impl std::fmt::Display for ValidationError {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 match self {
91 ValidationError::Email => f.write_str("Invalid email."),
92 ValidationError::Missing(field) => {
93 f.write_fmt(format_args!("Missing required field `{field}`."))
94 }
95 ValidationError::Min(field) => f.write_fmt(format_args!(
96 "Field `{field}` failed minimum size validation."
97 )),
98 }
99 }
100}