Skip to content

Commit

Permalink
Nested collections should not always fail (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
einarmo committed Mar 6, 2024
1 parent 0aceb69 commit 503216c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 20 deletions.
35 changes: 15 additions & 20 deletions validator/src/validation/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ where
}
}

let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));

if errors.is_empty() {
if vec_err.is_empty() {
Ok(())
} else {
let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));
Err(errors)
}
}
Expand All @@ -93,12 +92,11 @@ where
}
}

let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));

if errors.is_empty() {
if vec_err.is_empty() {
Ok(())
} else {
let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));
Err(errors)
}
}
Expand All @@ -123,12 +121,11 @@ where
}
}

let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));

if errors.is_empty() {
if vec_err.is_empty() {
Ok(())
} else {
let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));
Err(errors)
}
}
Expand All @@ -153,12 +150,11 @@ where
}
}

let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));

if errors.is_empty() {
if vec_err.is_empty() {
Ok(())
} else {
let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));
Err(errors)
}
}
Expand All @@ -183,12 +179,11 @@ where
}
}

let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));

if errors.is_empty() {
if vec_err.is_empty() {
Ok(())
} else {
let err_kind = ValidationErrorsKind::List(vec_err);
let errors = ValidationErrors(HashMap::from([(field_name, err_kind)]));
Err(errors)
}
}
Expand Down
65 changes: 65 additions & 0 deletions validator_derive_tests/tests/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ fn test_can_validate_vector_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let instance = ParentWithVectorOfChildren {
child: vec![Child { value: "valid".to_string() }, Child { value: "valid".to_string() }],
};
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -352,6 +358,11 @@ fn test_can_validate_slice_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let child = vec![Child { value: "valid".to_string() }, Child { value: "valid".to_string() }];
let instance = ParentWithSliceOfChildren { child: &child };
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -411,6 +422,17 @@ fn test_can_validate_array_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let instance = ParentWithArrayOfChildren {
child: [
Child { value: "valid".to_string() },
Child { value: "valid".to_string() },
Child { value: "valid".to_string() },
Child { value: "valid".to_string() },
],
};
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -470,6 +492,15 @@ fn test_can_validate_option_vector_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let instance = ParentWithOptionVectorOfChildren {
child: Some(vec![
Child { value: "valid".to_string() },
Child { value: "valid".to_string() },
]),
};
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -513,6 +544,12 @@ fn test_can_validate_map_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let instance = ParentWithMapOfChildren {
child: [(0, Child { value: "value".to_string() })].iter().cloned().collect(),
};
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -555,6 +592,11 @@ fn test_can_validate_ref_map_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let child = [(0, Child { value: "value".to_string() })].iter().cloned().collect();
let instance = ParentWithRefMapOfChildren { child: &child };
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -598,6 +640,12 @@ fn test_can_validate_option_map_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let instance = ParentWithOptionMapOfChildren {
child: Some([(0, Child { value: "value".to_string() })].iter().cloned().collect()),
};
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -641,6 +689,12 @@ fn test_can_validate_set_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let instance = ParentWithSetOfChildren {
child: [Child { value: "value".to_string() }].iter().cloned().collect(),
};
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -683,6 +737,11 @@ fn test_can_validate_ref_set_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let child = [Child { value: "value".to_string() }].iter().cloned().collect();
let instance = ParentWithRefSetOfChildren { child: &child };
assert!(instance.validate().is_ok());
}

#[test]
Expand Down Expand Up @@ -726,6 +785,12 @@ fn test_can_validate_option_set_fields() {
} else {
panic!("Expected list validation errors");
}

// A valid struct should not fail
let instance = ParentWithOptionSetOfChildren {
child: Some([Child { value: "value".to_string() }].iter().cloned().collect()),
};
assert!(instance.validate().is_ok());
}

#[test]
Expand Down

0 comments on commit 503216c

Please sign in to comment.