Skip to main content

age_setup/types/
validation.rs

1use crate::errors::{Error, Result, ValidationError};
2pub(crate) fn validate_age_prefix(key: &str) -> Result<()> {
3    if key.is_empty() {
4        return Err(Error::from(ValidationError::invalid_public_key(
5            "Key is empty",
6        )));
7    }
8    if !key.starts_with("age1") {
9        return Err(Error::from(ValidationError::invalid_public_key(format!(
10            "Key must start with 'age1', got: {}",
11            &key[..key.len().min(10)]
12        ))));
13    }
14    Ok(())
15}
16#[cfg(test)]
17mod tests {
18    use super::*;
19    use crate::errors::Error;
20    #[test]
21    fn test_validate_age_prefix_empty() {
22        let result = validate_age_prefix("");
23        assert!(result.is_err());
24        match result.unwrap_err() {
25            Error::Validation(e) => {
26                let msg = format!("{}", e);
27                assert!(msg.contains("Key is empty"));
28            }
29            _ => panic!(),
30        }
31    }
32    #[test]
33    fn test_validate_age_prefix_wrong_prefix() {
34        let result = validate_age_prefix("xyz123");
35        assert!(result.is_err());
36        match result.unwrap_err() {
37            Error::Validation(e) => {
38                let msg = format!("{}", e);
39                assert!(msg.contains("must start with 'age1'"));
40                assert!(msg.contains("xyz123"));
41            }
42            _ => panic!(),
43        }
44    }
45    #[test]
46    fn test_validate_age_prefix_short_prefix() {
47        let result = validate_age_prefix("age");
48        assert!(result.is_err());
49    }
50    #[test]
51    fn test_validate_age_prefix_valid() {
52        let result = validate_age_prefix("age1abcdef");
53        assert!(result.is_ok());
54    }
55}