From 864216e40fcc276b350ae963b0076a810aa29bc1 Mon Sep 17 00:00:00 2001 From: Ross Carter Date: Sun, 13 Apr 2025 22:01:40 -0400 Subject: [PATCH 1/2] Feature flag out the external dependencies --- validator/Cargo.toml | 10 +++++++--- validator/src/lib.rs | 3 +++ validator/src/validation/mod.rs | 3 +++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/validator/Cargo.toml b/validator/Cargo.toml index a61f5eda..93991b4e 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -12,9 +12,9 @@ readme = "../README.md" rust-version = { workspace = true } [dependencies] -url = "2" -regex = { version = "1", default-features = false, features = ["std"] } -idna = "1" +url = { version = "2", optional = true } +regex = { version = "1", default-features = false, features = ["std"], optional = true } +idna = { version = "1", optional = true } serde = "1" serde_derive = "1" serde_json = "1" @@ -23,6 +23,10 @@ card-validate = { version = "2.3", optional = true } indexmap = { version = "2.0.0", features = ["serde"], optional = true } [features] +default = ["regex", "url", "email"] card = ["card-validate"] derive = ["validator_derive"] derive_nightly_features = ["derive", "validator_derive/nightly_features"] +regex = ["dep:regex"] +url = ["dep:url"] +email = ["dep:idna", "regex"] diff --git a/validator/src/lib.rs b/validator/src/lib.rs index 2cb6d108..089a7504 100644 --- a/validator/src/lib.rs +++ b/validator/src/lib.rs @@ -69,14 +69,17 @@ mod validation; pub use validation::cards::ValidateCreditCard; pub use validation::contains::ValidateContains; pub use validation::does_not_contain::ValidateDoesNotContain; +#[cfg(feature = "email")] pub use validation::email::ValidateEmail; pub use validation::ip::ValidateIp; pub use validation::length::ValidateLength; pub use validation::must_match::validate_must_match; pub use validation::non_control_character::ValidateNonControlCharacter; pub use validation::range::ValidateRange; +#[cfg(feature = "regex")] pub use validation::regex::{AsRegex, ValidateRegex}; pub use validation::required::ValidateRequired; +#[cfg(feature = "url")] pub use validation::urls::ValidateUrl; pub use traits::{Validate, ValidateArgs}; diff --git a/validator/src/validation/mod.rs b/validator/src/validation/mod.rs index 44831d32..447dbf0c 100644 --- a/validator/src/validation/mod.rs +++ b/validator/src/validation/mod.rs @@ -2,6 +2,7 @@ pub mod cards; pub mod contains; pub mod does_not_contain; +#[cfg(feature = "email")] pub mod email; pub mod ip; pub mod length; @@ -9,6 +10,8 @@ pub mod must_match; // pub mod nested; pub mod non_control_character; pub mod range; +#[cfg(feature = "regex")] pub mod regex; pub mod required; +#[cfg(feature = "url")] pub mod urls; From a39ef68a98683676013f94a5ffd8ea1c03152f1c Mon Sep 17 00:00:00 2001 From: Ross Carter Date: Tue, 29 Apr 2025 21:08:39 -0400 Subject: [PATCH 2/2] Add docs and bump version --- README.md | 11 ++++++++--- validator/Cargo.toml | 2 +- validator_derive/Cargo.toml | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4a2e0412..02505849 100644 --- a/README.md +++ b/README.md @@ -155,12 +155,12 @@ validation functions and types. ## Validators The crate comes with some built-in validators and you can have several validators for a given field. -### email +### email (optional) Tests whether the String is a valid email according to the HTML5 regex, which means it will mark some esoteric emails as invalid that won't be valid in a `email` input as well. This validator doesn't take any arguments: `#[validate(email)]`. -### url +### url (optional) Tests whether the String is a valid URL. This validator doesn't take any arguments: `#[validate(url)]`; @@ -244,7 +244,7 @@ Examples: #[validate(does_not_contain(pattern = "gmail"))] ``` -### regex +### regex (optional) Tests whether the string matches the regex given. `regex` takes 1 string argument: the path to a static Regex instance. @@ -402,5 +402,10 @@ For example, the following attributes all work: ``` ## Features +By default, the `email`, `url` and `regex` features are enabled to provide compatibility with versions < 0.21.0.s + `derive` - This allows for the use of the derive macro. `derive_nightly_features` - This imports both derive as well as proc-macro-error2 nightly features. This allows proc-macro-error2 to emit extra nightly warnings. +`email` - Allows for email validation, pulls in idna and turns on `regex` feature flag. +`url` - Allows for url validation, pulls in url optional dependency. +`regex` - Allows for regex validation, pulling in regex optional dependency. diff --git a/validator/Cargo.toml b/validator/Cargo.toml index 93991b4e..70ab2646 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "validator" -version = "0.20.0" +version = "0.21.0" authors = ["Vincent Prouillet