Skip to content

Commit

Permalink
Merge pull request #2261 from hannobraun/layer
Browse files Browse the repository at this point in the history
Add `ValidateObject` command
  • Loading branch information
hannobraun authored Mar 13, 2024
2 parents da2cb41 + 62cb209 commit 315bf19
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions crates/fj-core/src/layers/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
validation::Validation,
};

use super::{Command, Event, Layer};
use super::{validation::ValidateObject, Command, Event, Layer};

impl Layer<Objects> {
/// Insert an object into the stores
Expand All @@ -20,15 +20,15 @@ impl Layer<Objects> {
self.process(InsertObject { object }, &mut events);

for event in events {
let event = ValidateObject {
object: event.object.into(),
};
validation.process(event, &mut Vec::new());
}
}
}

/// 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)]
pub struct InsertObject {
/// The object to insert
Expand Down
16 changes: 10 additions & 6 deletions crates/fj-core/src/layers/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
validation::{Validation, ValidationError, ValidationErrors},
};

use super::{objects::InsertObject, Command, Event, Layer};
use super::{Command, Event, Layer};

impl Layer<Validation> {
/// Take all errors stored in the validation layer
Expand All @@ -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 Event = ValidationFailed;

fn decide(self, state: &Validation, events: &mut Vec<Self::Event>) {
let mut errors = Vec::new();

let object: AnyObject<Stored> = self.object.into();
object.validate(&state.config, &mut errors);
self.object.validate(&state.config, &mut errors);

for err in errors {
events.push(ValidationFailed {
object: object.clone(),
object: self.object.clone(),
err,
});
}
Expand Down

0 comments on commit 315bf19

Please sign in to comment.