MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / validate

Method validate

aiscript-directive/src/validator/format.rs:57–185  ·  view source on GitHub ↗
(&self, value: &Value)

Source from the content-addressed store, hash-verified

55 }
56
57 fn validate(&self, value: &Value) -> Result<(), String> {
58 let value_str = match value.as_str() {
59 Some(s) => s,
60 None => return Err("Value must be a string".into()),
61 };
62
63 match self.format_type.as_str() {
64 // Other formats remain unchanged
65 "time" => {
66 if !TIME_REGEX.is_match(value_str) {
67 return Err("Value doesn't match time format (HH:MM:SS)".into());
68 }
69
70 // Validate time components
71 let parts: Vec<&str> = value_str.split(':').collect();
72 if parts.len() != 3 {
73 return Err("Time must have hours, minutes, and seconds".into());
74 }
75
76 let hours: u32 = parts[0].parse().map_err(|_| "Invalid hours")?;
77 let minutes: u32 = parts[1].parse().map_err(|_| "Invalid minutes")?;
78 let seconds: u32 = parts[2].parse().map_err(|_| "Invalid seconds")?;
79
80 if hours >= 24 || minutes >= 60 || seconds >= 60 {
81 return Err("Invalid time components".into());
82 }
83
84 Ok(())
85 }
86 "month" => {
87 if !MONTH_REGEX.is_match(value_str) {
88 return Err("Value doesn't match month format (YYYY-MM)".into());
89 }
90
91 // Validate month components
92 let parts: Vec<&str> = value_str.split('-').collect();
93 if parts.len() != 2 {
94 return Err("Month must have year and month parts".into());
95 }
96
97 let month: u32 = parts[1].parse().map_err(|_| "Invalid month")?;
98
99 if !(1..=12).contains(&month) {
100 return Err("Month must be between 1 and 12".into());
101 }
102
103 Ok(())
104 }
105 "week" => {
106 if !WEEK_REGEX.is_match(value_str) {
107 return Err("Value doesn't match week format (YYYY-Www)".into());
108 }
109
110 // Validate week components
111 let year_part = &value_str[0..4];
112 let week_part = &value_str[6..8];
113
114 let year: i32 = year_part.parse().map_err(|_| "Invalid year")?;

Callers

nothing calls this directly

Calls 4

lenMethod · 0.80
containsMethod · 0.80
as_strMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected