Skip to main content

aster_forge_validation/
lib.rs

1//! Shared validation helpers for Aster services.
2//!
3//! The crate collects validation routines that were previously repeated in service code, including
4//! display text, frontend asset URLs, email handling, and filename handling. It keeps validation
5//! errors as plain messages so API layers and domain services can decide how to present or translate
6//! them.
7#![deny(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
8#![cfg_attr(
9    not(test),
10    deny(
11        clippy::unwrap_used,
12        clippy::unreachable,
13        clippy::expect_used,
14        clippy::panic,
15        clippy::unimplemented,
16        clippy::todo
17    )
18)]
19
20/// Display text and public asset URL validation helpers.
21pub mod display;
22/// Email validation and normalization helpers.
23pub mod email;
24/// Email allow/block list normalization and matching helpers.
25pub mod email_policy;
26/// File and folder name validation helpers.
27pub mod filename;
28
29/// Result type returned by validation helpers.
30pub type Result<T> = std::result::Result<T, ValidationError>;
31
32/// Error returned when validation fails.
33#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
34#[error("{message}")]
35pub struct ValidationError {
36    message: String,
37}
38
39impl ValidationError {
40    /// Creates a validation error with a user-facing message.
41    pub fn new(message: impl Into<String>) -> Self {
42        Self {
43            message: message.into(),
44        }
45    }
46
47    /// Returns the validation failure message.
48    pub fn message(&self) -> &str {
49        &self.message
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::ValidationError;
56
57    #[test]
58    fn validation_error_preserves_and_displays_message() {
59        let error = ValidationError::new("invalid value");
60
61        assert_eq!(error.message(), "invalid value");
62        assert_eq!(error.to_string(), "invalid value");
63    }
64}