From 7087ce68c0a50e850b10cd4e318a036e5a37dacd Mon Sep 17 00:00:00 2001 From: decryphe <12104091+decryphe@users.noreply.github.com> Date: Mon, 13 Nov 2023 10:33:21 +0100 Subject: [PATCH] Enables setting custom messages in complex custom validation functions (#279) Co-authored-by: decryphe --- validator/src/types.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/validator/src/types.rs b/validator/src/types.rs index 3bbbf1f3..9b5ede94 100644 --- a/validator/src/types.rs +++ b/validator/src/types.rs @@ -20,6 +20,13 @@ impl ValidationError { pub fn add_param(&mut self, name: Cow<'static, str>, val: &T) { self.params.insert(name, to_value(val).unwrap()); } + + /// Adds a custom message to a `ValidationError` that will be used when displaying the + /// `ValidationError`, instead of an auto-generated description. + pub fn with_message(mut self, message: Cow<'static, str>) -> ValidationError { + self.message = Some(message); + self + } } impl std::error::Error for ValidationError {