Skip to main content

aviso_validators/
types.rs

1// (C) Copyright 2024- ECMWF and individual contributors.
2//
3// This software is licensed under the terms of the Apache Licence Version 2.0
4// which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5// In applying this licence, ECMWF does not waive the privileges and immunities
6// granted to it by virtue of its status as an intergovernmental organisation nor
7// does it submit to any jurisdiction.
8
9//! Validation rule types for field validation
10
11use serde::Serialize;
12#[cfg(feature = "openapi")]
13use utoipa::ToSchema;
14
15/// Validation rules for different field types
16#[derive(serde::Deserialize, Serialize, Clone, Debug)]
17#[cfg_attr(feature = "openapi", derive(ToSchema))]
18#[serde(tag = "type")]
19pub enum ValidationRules {
20    /// String field validation with optional length constraints
21    #[cfg_attr(feature = "openapi", schema(title = "StringHandler"))]
22    StringHandler {
23        max_length: Option<usize>,
24        required: bool,
25    },
26    /// Date field validation with configurable output format
27    #[cfg_attr(feature = "openapi", schema(title = "DateHandler"))]
28    DateHandler {
29        canonical_format: String,
30        required: bool,
31    },
32    /// Enumerated value validation against allowed options
33    #[cfg_attr(feature = "openapi", schema(title = "EnumHandler"))]
34    EnumHandler { values: Vec<String>, required: bool },
35    /// Experiment version field validation with default values
36    #[cfg_attr(feature = "openapi", schema(title = "ExpverHandler"))]
37    ExpverHandler {
38        default: Option<String>,
39        required: bool,
40    },
41    /// Integer validation with optional range constraints
42    #[cfg_attr(feature = "openapi", schema(title = "IntHandler"))]
43    IntHandler {
44        range: Option<[i64; 2]>,
45        required: bool,
46    },
47    /// Floating-point validation with optional range constraints
48    #[cfg_attr(feature = "openapi", schema(title = "FloatHandler"))]
49    FloatHandler {
50        range: Option<[f64; 2]>,
51        required: bool,
52    },
53    /// Time field validation supporting multiple input formats
54    #[cfg_attr(feature = "openapi", schema(title = "TimeHandler"))]
55    TimeHandler { required: bool },
56    /// Polygon coordinate validation for spatial data
57    #[cfg_attr(feature = "openapi", schema(title = "PolygonHandler"))]
58    PolygonHandler { required: bool },
59}
60
61impl ValidationRules {
62    /// Check if this validation rule requires the field to be present
63    pub fn is_required(&self) -> bool {
64        match self {
65            ValidationRules::StringHandler { required, .. } => *required,
66            ValidationRules::DateHandler { required, .. } => *required,
67            ValidationRules::EnumHandler { required, .. } => *required,
68            ValidationRules::ExpverHandler { required, .. } => *required,
69            ValidationRules::IntHandler { required, .. } => *required,
70            ValidationRules::FloatHandler { required, .. } => *required,
71            ValidationRules::TimeHandler { required } => *required,
72            ValidationRules::PolygonHandler { required } => *required,
73        }
74    }
75}