Validation of nested structs fails if there are validation errors at both levels of nesting
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
Start by reproducing the supplied test_no_primary_entry case with validator 0.19 and inspect the ValidationErrors handling around the reported types.rs:185 panic. Done means nested and containing validation errors are retained together, the validation no longer panics, and the test assertion passes.
Written by the indexing model from the issue text.
Description
When validating a structure with a nested structure, if there are validation errors in the nested struct and in the containing struct, using a custom validation function, library panics with
thread 'test::test_no_primary_entry' panicked at /Users/bob/.cargo/registry/src/index.crates.io-6f17d22bba15001f/validator-0.19.0/src/types.rs:185:13:
Attempt to replace non-empty ValidationErrors entry
This is using v0.19
Test case:
use validator::{Validate, ValidationError};
use regex::Regex;
use lazy_static::lazy_static;
use serde::Serialize;
lazy_static! {
pub static ref VALID_FIELD_RE: Regex = Regex::new(r"^[a-z0-9A-Z_-]+$").unwrap();
}
#[derive(Validate, Serialize)]
struct Entry {
#[validate(regex(path = *VALID_FIELD_RE))]
field1: String,
primary: bool
}
#[derive(Validate, Serialize)]
struct Entries {
#[validate(nested)]
#[validate(custom(function="validate_entries"))]
entries: Option<Vec<Entry>>
}
fn validate_entries(entries: &[Entry]) -> Result<(), ValidationError> {
let primary_count = entries.iter().filter(|entry| entry.primary).count();
if primary_count != 1 {
Err(ValidationError::new(
"There must be exactly one primary entry.",
))
} else {
Ok(())
}
}
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
pub(crate) mod test {
use super::*;
#[test]
fn test_no_primary_entry() {
let error = Entries {
entries: Some(vec!(Entry { field1: "abc#012".to_string(), primary: false }))
}
.validate()
.unwrap_err();
assert_eq!(error.errors().len(), 1);
}
}
Cargo.toml:
[package]
name = "validator"
version = "0.1.0"
edition = "2021"
[dependencies]
lazy_static = "1.5"
regex = "1"
serde = { version = "1", features = ["derive"] }
validator = { version = "0.19", features = ["derive"] }
- Dominant language
- Rust
- Stars
- 2.5k
- Forks
- 190
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Keats/validator
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 5/5 Over a week Newbie friendliness 18/100
-
Difficulty 3/5 1-2 days Newbie friendliness 52/100
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
documentation
Difficulty 1/5 Under an hour Newbie friendliness 55/100
Similar issues
-
A-git C-bug S-needs-info
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
rust-lang/cargo#17794 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
LadybirdBrowser/ladybird#15821 ·
Maintainers usually reply within 1 day
-
`SortMergeJoinExec` panics instead of returning an error when its output coalescer rejects a batchOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
apache/datafusion#27728 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
limitlessprojects/Go-Hybrid#4764 ·
Maintainers usually reply within 1 day
-
enhancement good first issue ready
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MartinPTielemans/ilo#4238 ·
Maintainers usually reply within 1 day