diff --git a/editoast/src/views/timetable/stdcm/request.rs b/editoast/src/views/timetable/stdcm/request.rs index 72945b9608d..aa71719870c 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_mass", err); + return Err(errs); + } + } + + Ok(()) + } +}