Skip to content

Commit

Permalink
Add back smarter arguments for custom
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed Apr 5, 2024
1 parent a32eb52 commit dc8cf02
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 65 deletions.
30 changes: 14 additions & 16 deletions validator/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,25 @@ impl_validate_list!(std::vec::Vec<T>);
impl_validate_list!([T]);

impl<T: Validate, const N: usize> Validate for [T; N] {
fn validate(&self) -> Result<(), ValidationErrors> {
let mut vec_err: BTreeMap<usize, Box<ValidationErrors>> = BTreeMap::new();
fn validate(&self) -> Result<(), ValidationErrors> {
let mut vec_err: BTreeMap<usize, Box<ValidationErrors>> = BTreeMap::new();

for (index, item) in self.iter().enumerate() {
if let Err(e) = item.validate() {
vec_err.insert(index, Box::new(e));
for (index, item) in self.iter().enumerate() {
if let Err(e) = item.validate() {
vec_err.insert(index, Box::new(e));
}
}
}

if vec_err.is_empty() {
Ok(())
} else {
let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(std::collections::HashMap::from([(
"_tmp_validator",
err_kind,
)]));
Err(errors)
if vec_err.is_empty() {
Ok(())
} else {
let err_kind = ValidationErrorsKind::List(vec_err);
let errors =
ValidationErrors(std::collections::HashMap::from([("_tmp_validator", err_kind)]));
Err(errors)
}
}
}
}

impl<K, V: Validate, S> Validate for &HashMap<K, V, S> {
fn validate(&self) -> Result<(), ValidationErrors> {
Expand Down
1 change: 0 additions & 1 deletion validator_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ syn = "2"
quote = "1"
proc-macro2 = "1"
proc-macro-error = "1"
regex = { version = "1", default-features = false, features = ["std"] }
darling = { version = "0.20", features = ["suggestions"] }
13 changes: 12 additions & 1 deletion validator_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,19 @@ impl ToTokens for ValidateField {

// Custom validation
let mut custom = quote!();
// We try to be smart when passing arguments
let type_name = self.ty.to_token_stream().to_string();
let is_cow = type_name.contains("Cow <");
let is_number = NUMBER_TYPES.contains(&type_name.as_str());
let custom_actual_field = if is_cow {
quote!(#actual_field.as_ref())
} else if is_number || type_name.starts_with("&") {
quote!(#actual_field)
} else {
quote!(&#actual_field)
};
for c in &self.custom {
let tokens = custom_tokens(c.clone(), &actual_field, &field_name_str);
let tokens = custom_tokens(c.clone(), &custom_actual_field, &field_name_str);
custom = quote!(
#tokens
);
Expand Down
6 changes: 3 additions & 3 deletions validator_derive/src/tokens/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub fn custom_tokens(

let args = if let Some(arg) = custom.use_context {
if arg {
quote!(&#field_name, args)
quote!(#field_name, args)
} else {
quote!(&#field_name)
quote!(#field_name)
}
} else {
quote!(&#field_name)
quote!(#field_name)
};

let message = quote_message(custom.message);
Expand Down
88 changes: 44 additions & 44 deletions validator_derive/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,50 @@ use syn::{Expr, Field, Ident, Path};
use crate::utils::get_attr;

static OPTIONS_TYPE: [&str; 3] = ["Option|", "std|option|Option|", "core|option|Option|"];
// static NUMBER_TYPES: [&str; 42] = [
// "usize",
// "u8",
// "u16",
// "u32",
// "u64",
// "u128",
// "isize",
// "i8",
// "i16",
// "i32",
// "i64",
// "i128",
// "f32",
// "f64",
// "Option<usize>",
// "Option<u8>",
// "Option<u16>",
// "Option<u32>",
// "Option<u64>",
// "Option<u128>",
// "Option<isize>",
// "Option<i8>",
// "Option<i16>",
// "Option<i32>",
// "Option<i64>",
// "Option<i128>",
// "Option<f32>",
// "Option<f64>",
// "Option<Option<usize>>",
// "Option<Option<u8>>",
// "Option<Option<u16>>",
// "Option<Option<u32>>",
// "Option<Option<u64>>",
// "Option<Option<u128>>",
// "Option<Option<isize>>",
// "Option<Option<i8>>",
// "Option<Option<i16>>",
// "Option<Option<i32>>",
// "Option<Option<i64>>",
// "Option<Option<i128>>",
// "Option<Option<f32>>",
// "Option<Option<f64>>",
// ];
pub(crate) static NUMBER_TYPES: [&str; 42] = [
"usize",
"u8",
"u16",
"u32",
"u64",
"u128",
"isize",
"i8",
"i16",
"i32",
"i64",
"i128",
"f32",
"f64",
"Option<usize>",
"Option<u8>",
"Option<u16>",
"Option<u32>",
"Option<u64>",
"Option<u128>",
"Option<isize>",
"Option<i8>",
"Option<i16>",
"Option<i32>",
"Option<i64>",
"Option<i128>",
"Option<f32>",
"Option<f64>",
"Option<Option<usize>>",
"Option<Option<u8>>",
"Option<Option<u16>>",
"Option<Option<u32>>",
"Option<Option<u64>>",
"Option<Option<u128>>",
"Option<Option<isize>>",
"Option<Option<i8>>",
"Option<Option<i16>>",
"Option<Option<i32>>",
"Option<Option<i64>>",
"Option<Option<i128>>",
"Option<Option<f32>>",
"Option<Option<f64>>",
];

// This struct holds all the validation information on a field
// The "ident" and "ty" fields are populated by `darling`
Expand Down
9 changes: 9 additions & 0 deletions validator_derive_tests/tests/run-pass/custom.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use validator::{Validate, ValidationError};

#[derive(Validate)]
Expand All @@ -10,10 +11,18 @@ struct Test {
struct TestPath {
#[validate(custom(function = "crate::validate_something"))]
s: String,
#[validate(custom(function = "crate::validate_number"))]
n: u8,
#[validate(custom(function = "crate::validate_something"))]
r: Cow<'static, str>
}

fn validate_something(_s: &str) -> Result<(), ValidationError> {
Ok(())
}

fn validate_number(_s: u8) -> Result<(), ValidationError> {
Ok(())
}

fn main() {}

0 comments on commit dc8cf02

Please sign in to comment.