Merge pull request #2261 from hannobraun/layer

Add `ValidateObject` command
This commit is contained in:
Hanno Braun 2024-03-13 14:10:20 +01:00 committed by GitHub
commit 315bf19b68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 10 deletions

View File

@ -5,7 +5,7 @@ use crate::{
validation::Validation, validation::Validation,
}; };
use super::{Command, Event, Layer}; use super::{validation::ValidateObject, Command, Event, Layer};
impl Layer<Objects> { impl Layer<Objects> {
/// Insert an object into the stores /// Insert an object into the stores
@ -20,15 +20,15 @@ impl Layer<Objects> {
self.process(InsertObject { object }, &mut events); self.process(InsertObject { object }, &mut events);
for event in events { for event in events {
let event = ValidateObject {
object: event.object.into(),
};
validation.process(event, &mut Vec::new()); validation.process(event, &mut Vec::new());
} }
} }
} }
/// Insert an object into the stores /// Insert an object into the stores
///
/// This struct serves as both event and command for `Layer<Objects>`, as well
/// as a command for `Layer<Validation>`.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct InsertObject { pub struct InsertObject {
/// The object to insert /// The object to insert

View File

@ -5,7 +5,7 @@ use crate::{
validation::{Validation, ValidationError, ValidationErrors}, validation::{Validation, ValidationError, ValidationErrors},
}; };
use super::{objects::InsertObject, Command, Event, Layer}; use super::{Command, Event, Layer};
impl Layer<Validation> { impl Layer<Validation> {
/// Take all errors stored in the validation layer /// Take all errors stored in the validation layer
@ -14,19 +14,23 @@ impl Layer<Validation> {
} }
} }
impl Command<Validation> for InsertObject { /// Validate an object
pub struct ValidateObject {
/// The object to validate
pub object: AnyObject<Stored>,
}
impl Command<Validation> for ValidateObject {
type Result = (); type Result = ();
type Event = ValidationFailed; type Event = ValidationFailed;
fn decide(self, state: &Validation, events: &mut Vec<Self::Event>) { fn decide(self, state: &Validation, events: &mut Vec<Self::Event>) {
let mut errors = Vec::new(); let mut errors = Vec::new();
self.object.validate(&state.config, &mut errors);
let object: AnyObject<Stored> = self.object.into();
object.validate(&state.config, &mut errors);
for err in errors { for err in errors {
events.push(ValidationFailed { events.push(ValidationFailed {
object: object.clone(), object: self.object.clone(),
err, err,
}); });
} }