From 5281bb8192451789307b59c4f8256a5e91cc1290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristram=20Gr=C3=A4bener?= Date: Thu, 26 Dec 2024 00:27:41 +0100 Subject: [PATCH] editoast: impl Validate on stdcm request directly (no derive) for uom This allows to use it on uom units --- editoast/src/views/timetable/stdcm/request.rs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/editoast/src/views/timetable/stdcm/request.rs b/editoast/src/views/timetable/stdcm/request.rs index 72945b9608d..fbac1064501 100644 --- a/editoast/src/views/timetable/stdcm/request.rs +++ b/editoast/src/views/timetable/stdcm/request.rs @@ -12,7 +12,7 @@ use itertools::Itertools; use serde::Deserialize; use serde::Serialize; use utoipa::ToSchema; -use validator::Validate; +use validator::{Validate, ValidationError, ValidationErrors}; use crate::core::pathfinding::PathfindingInputError; use crate::error::Result; @@ -68,7 +68,7 @@ struct StepTimingData { /// An STDCM request #[editoast_derive::annotate_units] -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Validate, ToSchema)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)] pub(super) struct Request { /// Deprecated, first step arrival time should be used instead pub(super) start_time: Option>, @@ -105,7 +105,6 @@ pub(super) struct Request { #[schema(value_type = Option, example = json!(["5%", "2min/100km"]))] pub(super) margin: Option, /// Total mass of the consist - //#[validate(range(exclusive_min = 0.0))] TODOUOM #[serde(default, with = "kilogram::option")] pub(super) total_mass: Option, /// Total length of the consist in meters @@ -292,3 +291,18 @@ impl Request { Ok(Some(towed_rolling_stock)) } } + +impl Validate for Request { + fn validate(&self) -> Result<(), ValidationErrors> { + if let Some(mass) = self.total_mass { + if mass <= kilogram::new(0.0) { + let err = ValidationError::new("the total mass must be strictly positive"); + let mut errs = ValidationErrors::new(); + errs.add("total_mall", err); + return Err(errs); + } + } + + Ok(()) + } +}