skp_validator_core/
schema.rs

1//! Schema introspection types.
2
3use std::collections::BTreeMap;
4#[cfg(feature = "serde")]
5use serde::{Serialize, Deserialize};
6
7/// Metadata about the validation rules on a type.
8pub trait ValidationMetadata {
9    /// Get the validation rules for this type.
10    fn get_validation_rules() -> TypeValidation;
11}
12
13/// Description of validation rules for a type.
14#[derive(Debug, Clone, PartialEq, Default)]
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16pub struct TypeValidation {
17    /// Rules for each field.
18    pub fields: BTreeMap<String, FieldValidation>,
19    /// Nested types that also have validation.
20    pub nested: BTreeMap<String, TypeValidation>,
21}
22
23/// Description of validation rules for a single field.
24#[derive(Debug, Clone, PartialEq, Default)]
25#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
26pub struct FieldValidation {
27    /// The validation rules applied to this field.
28    pub rules: Vec<RuleSchema>,
29}
30
31/// Schema description of a single validation rule.
32#[derive(Debug, Clone, PartialEq)]
33#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
34#[cfg_attr(feature = "serde", serde(tag = "type", content = "params"))]
35pub enum RuleSchema {
36    /// String length
37    Length { min: Option<u64>, max: Option<u64>, equal: Option<u64> },
38    /// Email format
39    Email,
40    /// URL format
41    Url,
42    /// IP address
43    Ip { version: Option<String> },
44    /// UUID
45    Uuid { version: Option<usize> },
46    /// Phone number
47    Phone,
48    /// Credit card
49    CreditCard,
50    /// Regex pattern
51    Pattern { regex: String },
52    /// Numeric range
53    Range { min: Option<f64>, max: Option<f64>, min_exclusive: Option<f64>, max_exclusive: Option<f64> },
54    /// Allow values
55    AllowedValues { values: Vec<String> },
56    /// Must match another field
57    MustMatch { other_field: String },
58    /// Required field
59    Required,
60    /// Custom validation
61    Custom { name: String },
62}
63
64impl TypeValidation {
65    /// Create a new TypeValidation
66    pub fn new() -> Self {
67        Self::default()
68    }
69}
70
71mod impls;