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

Open
#363 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
backend

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Keats/validator

All issues in Keats/validator

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.