From 32cd07c241955aac64b9631f64f3d6d6aba882cb Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Thu, 21 Dec 2023 08:32:01 -0800 Subject: [PATCH] Validations::validate_result --- CHANGELOG.md | 5 +++++ src/value.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b219b6..1433449 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [92]: https://github.com/khonsulabs/gooey/issues/92 +### Added + +- `Validations::validate_result` attaches a `Dynamic>` to the + validations. This was already available on `when` conditioned validations. + ## v0.1.3 (2023-12-19) ### Added diff --git a/src/value.rs b/src/value.rs index 0b954c7..235b9e8 100644 --- a/src/value.rs +++ b/src/value.rs @@ -2243,6 +2243,32 @@ impl Validations { validation } + /// Returns a dynamic validation status that is created by transforming the + /// `Err` variant of `result` using [`Display`]. + /// + /// The validation is linked with `self` such that checking `self`'s + /// validation status will include this validation. + #[must_use] + pub fn validate_result( + &self, + result: impl IntoDynamic>, + ) -> Dynamic + where + T: Send + 'static, + E: Display + Send + 'static, + { + let result = result.into_dynamic(); + let error_message = result.map_each(move |value| match value { + Ok(_) => None, + Err(err) => Some(err.to_string()), + }); + + self.validate(&error_message, |error_message| match error_message { + None => Ok(()), + Some(message) => Err(message.clone()), + }) + } + fn map_to_message( mut check: Valid, ) -> impl for<'a> FnMut(&'a GenerationalValue) -> GenerationalValue> + Send + 'static