Skip to content

Validation of nested structs fails if there are validation errors at both levels of nesting #363

@BobWall23

Description

@BobWall23

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"] }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions