Skip to main content

ras_validation/domain/
validated.rs

1use ras_errors::AppError;
2use serde::de::DeserializeOwned;
3use validator::Validate;
4
5#[derive(Debug, Clone)]
6pub struct Validated<T>(pub T);
7
8#[derive(Debug)]
9pub struct ValidationFailure(pub String);
10
11impl<T> Validated<T>
12where
13    T: Validate + DeserializeOwned,
14{
15    pub fn from_json(value: serde_json::Value) -> Result<Self, AppError> {
16        let inner: T =
17            serde_json::from_value(value).map_err(|e| AppError::ValidationError(e.to_string()))?;
18        inner
19            .validate()
20            .map_err(|e| AppError::ValidationError(e.to_string()))?;
21        Ok(Self(inner))
22    }
23}
24
25impl<T> std::ops::Deref for Validated<T> {
26    type Target = T;
27    fn deref(&self) -> &T {
28        &self.0
29    }
30}