armature_validation/
traits.rs1use crate::ValidationError;
4use async_trait::async_trait;
5
6pub trait Validate {
8 fn validate(&self) -> Result<(), Vec<ValidationError>>;
10}
11
12#[async_trait]
14pub trait AsyncValidate {
15 async fn validate_async(&self) -> Result<(), Vec<ValidationError>>;
17}
18
19pub trait Validator: Send + Sync {
21 fn validate(&self, value: &dyn std::any::Any, field: &str) -> Result<(), ValidationError>;
23
24 fn name(&self) -> &'static str;
26}
27
28#[async_trait]
30pub trait AsyncValidator: Send + Sync {
31 async fn validate_async(
33 &self,
34 value: &dyn std::any::Any,
35 field: &str,
36 ) -> Result<(), ValidationError>;
37
38 fn name(&self) -> &'static str;
40}
41
42#[derive(Debug, Clone)]
44pub struct ValidationContext {
45 pub data: std::collections::HashMap<String, String>,
47}
48
49impl ValidationContext {
50 pub fn new() -> Self {
52 Self {
53 data: std::collections::HashMap::new(),
54 }
55 }
56
57 pub fn with_data(mut self, key: String, value: String) -> Self {
59 self.data.insert(key, value);
60 self
61 }
62
63 pub fn get(&self, key: &str) -> Option<&String> {
65 self.data.get(key)
66 }
67}
68
69impl Default for ValidationContext {
70 fn default() -> Self {
71 Self::new()
72 }
73}