Merge pull request #2448 from hannobraun/validate

Fix bug where validation errors could overwrite previous validation errors
This commit is contained in:
Hanno Braun 2024-08-12 21:23:02 +02:00 committed by GitHub
commit c157b66f5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 15 deletions

View File

@ -38,10 +38,7 @@ impl Command<Validation> for ValidateObject<'_> {
panic!("{:#?}", err); panic!("{:#?}", err);
} }
events.push(ValidationFailed { events.push(ValidationFailed { err });
object: self.object.clone(),
err,
});
} }
} }
} }
@ -60,7 +57,7 @@ impl Command<Validation> for TakeErrors {
state: &Validation, state: &Validation,
events: &mut Vec<Self::Event>, events: &mut Vec<Self::Event>,
) -> Self::Result { ) -> Self::Result {
let errors = ValidationErrors(state.errors.values().cloned().collect()); let errors = ValidationErrors(state.errors.to_vec());
events.push(self); events.push(self);
@ -83,15 +80,12 @@ impl Event<Validation> for TakeErrors {
/// Event produced by `Layer<Validation>`. /// Event produced by `Layer<Validation>`.
#[derive(Clone)] #[derive(Clone)]
pub struct ValidationFailed { pub struct ValidationFailed {
/// The object for which validation failed
pub object: AnyObject<Stored>,
/// The validation error /// The validation error
pub err: ValidationError, pub err: ValidationError,
} }
impl Event<Validation> for ValidationFailed { impl Event<Validation> for ValidationFailed {
fn evolve(&self, state: &mut Validation) { fn evolve(&self, state: &mut Validation) {
state.errors.insert(self.object.id(), self.err.clone()); state.errors.push(self.err.clone());
} }
} }

View File

@ -1,6 +1,4 @@
use std::{collections::HashMap, error::Error, thread}; use std::{error::Error, thread};
use crate::storage::ObjectId;
use super::{ValidationConfig, ValidationError}; use super::{ValidationConfig, ValidationError};
@ -8,7 +6,7 @@ use super::{ValidationConfig, ValidationError};
#[derive(Default)] #[derive(Default)]
pub struct Validation { pub struct Validation {
/// All unhandled validation errors /// All unhandled validation errors
pub errors: HashMap<ObjectId, ValidationError>, pub errors: Vec<ValidationError>,
/// Validation configuration for the validation service /// Validation configuration for the validation service
pub config: ValidationConfig, pub config: ValidationConfig,
@ -17,7 +15,7 @@ pub struct Validation {
impl Validation { impl Validation {
/// Construct an instance of `Validation`, using the provided configuration /// Construct an instance of `Validation`, using the provided configuration
pub fn with_validation_config(config: ValidationConfig) -> Self { pub fn with_validation_config(config: ValidationConfig) -> Self {
let errors = HashMap::new(); let errors = Vec::new();
Self { errors, config } Self { errors, config }
} }
} }
@ -31,7 +29,7 @@ impl Drop for Validation {
errors:" errors:"
); );
for err in self.errors.values() { for err in self.errors.iter() {
println!("{}", err); println!("{}", err);
// Once `Report` is stable, we can replace this: // Once `Report` is stable, we can replace this: