You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!{pubstatic ref VALID_FIELD_RE:Regex = Regex::new(r"^[a-z0-9A-Z_-]+$").unwrap();}#[derive(Validate,Serialize)]structEntry{#[validate(regex(path = *VALID_FIELD_RE))]field1:String,primary:bool}#[derive(Validate,Serialize)]structEntries{#[validate(nested)]#[validate(custom(function="validate_entries"))]entries:Option<Vec<Entry>>}fnvalidate_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(())}}fnmain(){println!("Hello, world!");}#[cfg(test)]pub(crate)mod test {usesuper::*;#[test]fntest_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"]}
The text was updated successfully, but these errors were encountered:
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
This is using v0.19
Test case:
Cargo.toml
:The text was updated successfully, but these errors were encountered: