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

Method validate

aiscript-directive/src/validator/date.rs:21–62  ·  view source on GitHub ↗
(&self, value: &Value)

Source from the content-addressed store, hash-verified

19 }
20
21 fn validate(&self, value: &Value) -> Result<(), String> {
22 let value_str = match value.as_str() {
23 Some(s) => s,
24 None => return Err("Value must be a string".into()),
25 };
26
27 // Get the format and convert it to chrono format
28 let user_format = self.format.as_deref().unwrap_or("YYYY-MM-DD");
29 let chrono_format = convert_format(user_format);
30
31 // Parse the date
32 let date = match NaiveDate::parse_from_str(value_str, &chrono_format) {
33 Ok(date) => date,
34 Err(_) => return Err(format!("Invalid date format, expected {}", user_format)),
35 };
36
37 // Check minimum date constraint
38 if let Some(min_str) = &self.min {
39 match NaiveDate::parse_from_str(min_str, &chrono_format) {
40 Ok(min_date) => {
41 if date < min_date {
42 return Err(format!("Date must be on or after {}", min_str));
43 }
44 }
45 Err(_) => return Err(format!("Invalid minimum date: {}", min_str)),
46 }
47 }
48
49 // Check maximum date constraint
50 if let Some(max_str) = &self.max {
51 match NaiveDate::parse_from_str(max_str, &chrono_format) {
52 Ok(max_date) => {
53 if date > max_date {
54 return Err(format!("Date must be on or before {}", max_str));
55 }
56 }
57 Err(_) => return Err(format!("Invalid maximum date: {}", max_str)),
58 }
59 }
60
61 Ok(())
62 }
63
64 fn as_any(&self) -> &dyn Any {
65 self

Callers

nothing calls this directly

Calls 2

convert_formatFunction · 0.85
as_strMethod · 0.45

Tested by

no test coverage detected