diff options
| -rw-r--r-- | README.md | 25 | ||||
| -rw-r--r-- | validator_derive/src/lib.rs | 5 | ||||
| -rw-r--r-- | validator_derive/tests/compile-fail/length/wrong_arg_type.rs | 15 | ||||
| -rw-r--r-- | validator_derive/tests/compile-fail/range/wrong_arg_type.rs | 15 | ||||
| -rw-r--r-- | validator_derive/tests/test_derive.rs | 14 |
5 files changed, 24 insertions, 50 deletions
@@ -6,12 +6,14 @@ Status: Experimental - do not use in production Macros 1.1 custom derive to simplify struct validation inspired by [marshmallow](http://marshmallow.readthedocs.io/en/latest/) and [Django validators](https://docs.djangoproject.com/en/1.10/ref/validators/). -It currently only works on nightly as it uses 2 features: `proc_macro` and `attr_literals`. `proc_macro` should be stable in Rust 1.15 but there are no plans for `attr_literals` yet. +It relies on the `proc_macro` feature which will be stable in Rust 1.15. + +By default all args to a `validate` must be strings if you are using stable. +However, if you are using nightly, you can also activate the `attr_literals` feature to be able to use int, float and boolean as well. + A short example: ```rust -#![feature(proc_macro, attr_literals)] - #[macro_use] extern crate validator_derive; extern crate validator; #[macro_use] extern crate serde_derive; @@ -26,10 +28,10 @@ struct SignupData { mail: String, #[validate(url)] site: String, - #[validate(length(min = 1), custom = "validate_unique_username")] + #[validate(length(min = "1"), custom = "validate_unique_username")] #[serde(rename = "firstName")] first_name: String, - #[validate(range(min = 18, max = 20))] + #[validate(range(min = "18", max = "20"))] age: u32, } @@ -54,8 +56,7 @@ field is renamed from/to `firstName`. Any error on that field will be in the `fi not `first_name`. ## Usage -You will need to add the `proc_macro` and `attr_literals` as seen in the example, as -well as importing the `Validate` trait. +You will need to import the `Validate` trait, and optionally use the `attr_literals` feature. The `validator` crate can also be used without the custom derive as it exposes all the validation functions. @@ -86,10 +87,10 @@ At least one argument is required with a maximum of 2 (having `min` and `max` at Examples: ```rust -#[validate(length(min = 1, max = 10))] -#[validate(length(min = 1))] -#[validate(length(max = 10))] -#[validate(length(equal = 10))] +#[validate(length(min = "1", max = "10"))] +#[validate(length(min = "1"))] +#[validate(length(max = "10"))] +#[validate(length(equal = "10"))] ``` ### range @@ -133,7 +134,7 @@ Often, some error validation can only be applied when looking at the full struct ```rust #[derive(Debug, Validate, Deserialize)] -#[validate(schema(function = "validate_category", skip_on_field_errors = false)] +#[validate(schema(function = "validate_category", skip_on_field_errors = "false")] struct CategoryData { category: String, name: String, diff --git a/validator_derive/src/lib.rs b/validator_derive/src/lib.rs index 92f3ea5..fd02e3d 100644 --- a/validator_derive/src/lib.rs +++ b/validator_derive/src/lib.rs @@ -526,6 +526,8 @@ fn lit_to_string(lit: &syn::Lit) -> Option<String> { fn lit_to_int(lit: &syn::Lit) -> Option<u64> { match *lit { syn::Lit::Int(ref s, _) => Some(*s), + // TODO: remove when attr_literals is stable + syn::Lit::Str(ref s, _) => Some(s.parse::<u64>().unwrap()), _ => None, } } @@ -534,6 +536,8 @@ fn lit_to_float(lit: &syn::Lit) -> Option<f64> { match *lit { syn::Lit::Float(ref s, _) => Some(s.parse::<f64>().unwrap()), syn::Lit::Int(ref s, _) => Some(*s as f64), + // TODO: remove when attr_literals is stable + syn::Lit::Str(ref s, _) => Some(s.parse::<f64>().unwrap()), _ => None, } } @@ -541,6 +545,7 @@ fn lit_to_float(lit: &syn::Lit) -> Option<f64> { fn lit_to_bool(lit: &syn::Lit) -> Option<bool> { match *lit { syn::Lit::Bool(ref s) => Some(*s), + syn::Lit::Str(ref s, _) => if s == "true" { Some(true) } else { Some(false) }, _ => None, } } diff --git a/validator_derive/tests/compile-fail/length/wrong_arg_type.rs b/validator_derive/tests/compile-fail/length/wrong_arg_type.rs deleted file mode 100644 index 4d3f602..0000000 --- a/validator_derive/tests/compile-fail/length/wrong_arg_type.rs +++ /dev/null @@ -1,15 +0,0 @@ -#![feature(proc_macro, attr_literals)] - -#[macro_use] extern crate validator_derive; -extern crate validator; -use validator::Validate; - -#[derive(Validate)] -//~^ ERROR: custom derive attribute panicked -//~^^ HELP: Invalid attribute #[validate] on field `s`: invalid argument type for `min` of `length` validator: only integers are allowed -struct Test { - #[validate(length(min = "2"))] - s: String, -} - -fn main() {} diff --git a/validator_derive/tests/compile-fail/range/wrong_arg_type.rs b/validator_derive/tests/compile-fail/range/wrong_arg_type.rs deleted file mode 100644 index a62a5ff..0000000 --- a/validator_derive/tests/compile-fail/range/wrong_arg_type.rs +++ /dev/null @@ -1,15 +0,0 @@ -#![feature(proc_macro, attr_literals)] - -#[macro_use] extern crate validator_derive; -extern crate validator; -use validator::Validate; - -#[derive(Validate)] -//~^ ERROR: custom derive attribute panicked -//~^^ HELP: Invalid attribute #[validate] on field `s`: invalid argument type for `min` of `range` validator: only integers are allowed -struct Test { - #[validate(range(min = "2", max = 3))] - s: i32, -} - -fn main() {} diff --git a/validator_derive/tests/test_derive.rs b/validator_derive/tests/test_derive.rs index 6832762..9b9bad7 100644 --- a/validator_derive/tests/test_derive.rs +++ b/validator_derive/tests/test_derive.rs @@ -1,5 +1,3 @@ -#![feature(attr_literals)] - #[macro_use] extern crate validator_derive; extern crate validator; #[macro_use] extern crate serde_derive; @@ -9,16 +7,16 @@ use validator::Validate; #[derive(Debug, Validate, Deserialize)] -#[validate(schema(function = "validate_signup", skip_on_field_errors = false))] +#[validate(schema(function = "validate_signup", skip_on_field_errors = "false"))] struct SignupData { #[validate(email)] mail: String, #[validate(url)] site: String, - #[validate(length(min = 1), custom = "validate_unique_username")] + #[validate(length(min = "1"), custom = "validate_unique_username")] #[serde(rename = "firstName")] first_name: String, - #[validate(range(min = 18, max = 20))] + #[validate(range(min = "18", max = "20"))] age: u32, } @@ -47,11 +45,11 @@ fn validate_signup(data: &SignupData) -> Option<(String, String)> { } #[derive(Debug, Validate, Deserialize)] -#[validate(schema(function = "validate_signup2", skip_on_field_errors = false))] +#[validate(schema(function = "validate_signup2", skip_on_field_errors = "false"))] struct SignupData2 { #[validate(email)] mail: String, - #[validate(range(min = 18, max = 20))] + #[validate(range(min = "18", max = "20"))] age: u32, } @@ -60,7 +58,7 @@ struct SignupData2 { struct SignupData3 { #[validate(email)] mail: String, - #[validate(range(min = 18, max = 20))] + #[validate(range(min = "18", max = "20"))] age: u32, } |
