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 crate::point_cloud::DEFAULT_MAX_POINTS;
12use serde::Serialize;
13#[cfg(feature = "openapi")]
14use utoipa::ToSchema;
15
16/// Validation rules for different field types
17#[derive(serde::Deserialize, Serialize, Clone, Debug)]
18#[cfg_attr(feature = "openapi", derive(ToSchema))]
19#[serde(tag = "type")]
20pub enum ValidationRules {
21    /// String field validation with optional length constraints
22    #[cfg_attr(feature = "openapi", schema(title = "StringHandler"))]
23    StringHandler {
24        max_length: Option<usize>,
25        required: bool,
26    },
27    /// Date field validation with configurable output format
28    #[cfg_attr(feature = "openapi", schema(title = "DateHandler"))]
29    DateHandler {
30        canonical_format: String,
31        required: bool,
32    },
33    /// Enumerated value validation against allowed options
34    #[cfg_attr(feature = "openapi", schema(title = "EnumHandler"))]
35    EnumHandler { values: Vec<String>, required: bool },
36    /// Experiment version field validation with default values
37    #[cfg_attr(feature = "openapi", schema(title = "ExpverHandler"))]
38    ExpverHandler {
39        default: Option<String>,
40        required: bool,
41    },
42    /// Integer validation with optional range constraints
43    #[cfg_attr(feature = "openapi", schema(title = "IntHandler"))]
44    IntHandler {
45        range: Option<[i64; 2]>,
46        required: bool,
47    },
48    /// Floating-point validation with optional range constraints
49    #[cfg_attr(feature = "openapi", schema(title = "FloatHandler"))]
50    FloatHandler {
51        range: Option<[f64; 2]>,
52        required: bool,
53    },
54    /// Time field validation supporting multiple input formats
55    #[cfg_attr(feature = "openapi", schema(title = "TimeHandler"))]
56    TimeHandler { required: bool },
57    /// Polygon coordinate validation for spatial data
58    #[cfg_attr(feature = "openapi", schema(title = "PolygonHandler"))]
59    PolygonHandler { required: bool },
60    /// Point-cloud validation for non-empty JSON arrays of coordinates
61    #[cfg_attr(feature = "openapi", schema(title = "PointCloudHandler"))]
62    PointCloudHandler {
63        required: bool,
64        #[serde(default = "default_max_points")]
65        max_points: usize,
66    },
67}
68
69impl ValidationRules {
70    /// Check if this validation rule requires the field to be present
71    pub fn is_required(&self) -> bool {
72        match self {
73            ValidationRules::StringHandler { required, .. } => *required,
74            ValidationRules::DateHandler { required, .. } => *required,
75            ValidationRules::EnumHandler { required, .. } => *required,
76            ValidationRules::ExpverHandler { required, .. } => *required,
77            ValidationRules::IntHandler { required, .. } => *required,
78            ValidationRules::FloatHandler { required, .. } => *required,
79            ValidationRules::TimeHandler { required } => *required,
80            ValidationRules::PolygonHandler { required } => *required,
81            ValidationRules::PointCloudHandler { required, .. } => *required,
82        }
83    }
84}
85
86const fn default_max_points() -> usize {
87    DEFAULT_MAX_POINTS
88}