Skip to content

Commit

Permalink
editoast: impl Validate on stdcm request directly (no derive) for uom
Browse files Browse the repository at this point in the history
This allows to use it on uom units

Signed-off-by: Tristram Gräbener <[email protected]>
  • Loading branch information
Tristramg committed Dec 26, 2024
1 parent 2b0bd8a commit 89a22df
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions editoast/src/views/timetable/stdcm/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DateTime<Utc>>,
Expand Down Expand Up @@ -105,7 +105,6 @@ pub(super) struct Request {
#[schema(value_type = Option<String>, example = json!(["5%", "2min/100km"]))]
pub(super) margin: Option<MarginValue>,
/// Total mass of the consist
//#[validate(range(exclusive_min = 0.0))] TODOUOM
#[serde(default, with = "kilogram::option")]
pub(super) total_mass: Option<Mass>,
/// Total length of the consist in meters
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 89a22df

Please sign in to comment.