Skip to content

Commit

Permalink
Allow unsupported_field_type to be ignored (#189)
Browse files Browse the repository at this point in the history
* validatior_derive: allow unsupported_field_type to be ignored
  • Loading branch information
IniterWorker committed Apr 5, 2022
1 parent 7e85875 commit e64a5ef
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
7 changes: 1 addition & 6 deletions validator_derive/src/lib.rs
Expand Up @@ -342,12 +342,7 @@ fn find_fields_type(fields: &[syn::Field]) -> HashMap<String, String> {
_ => {
let mut field_type = proc_macro2::TokenStream::new();
field.ty.to_tokens(&mut field_type);
abort!(
field.ty.span(),
"Type `{}` of field `{}` not supported",
field_type,
field_ident
)
field_type.to_string().replace(' ', "")
}
};

Expand Down

This file was deleted.

This file was deleted.

@@ -0,0 +1,8 @@
use validator::Validate;

#[derive(Validate)]
struct Test {
s: [u8; 11],
}

fn main() {}
60 changes: 60 additions & 0 deletions validator_derive_tests/tests/unsupported_array.rs
@@ -0,0 +1,60 @@
use validator::{Validate, ValidationError};

fn valid_custom_fn(arr: &[u8; 2]) -> Result<(), ValidationError> {
match arr[0] == 1 {
true => Ok(()),
false => Err(ValidationError::new("meh")),
}
}

#[test]
fn can_validate_valid_email_with_unsupported_array() {
#[derive(Debug, Validate)]
struct TestStruct {
#[validate(email)]
val: String,
#[allow(dead_code)]
array: [u8; 2],
}

let s = TestStruct { val: "bob@bob.com".to_string(), array: [0u8; 2] };

assert!(s.validate().is_ok());
}

#[test]
fn can_validate_custom_with_unsupported_array() {
#[derive(Debug, Validate)]
struct TestStruct {
#[validate(email)]
val: String,
#[validate(custom = "valid_custom_fn")]
array: [u8; 2],
}

let s = TestStruct { val: "bob@bob.com".to_string(), array: [1u8, 1u8] };

assert!(s.validate().is_ok());
}

#[test]
fn can_fail_custom_with_unsupported_array() {
#[derive(Debug, Validate)]
struct TestStruct {
#[validate(email)]
val: String,
#[validate(custom = "valid_custom_fn")]
array: [u8; 2],
}

let s = TestStruct { val: "bob@bob.com".to_string(), array: [0u8, 1u8] };

let res = s.validate();
assert!(res.is_err());
let err = res.unwrap_err();
let errs = err.field_errors();
assert!(errs.contains_key("array"));
assert_eq!(errs["array"].len(), 1);
assert_eq!(errs["array"][0].code, "meh");
assert_eq!(errs["array"][0].params["value"][0], 0);
}

0 comments on commit e64a5ef

Please sign in to comment.