feagi_evolutionary/genome/validators/
mod.rs1use serde_json::Value;
18
19use crate::genome::schema::GenomeSchemaVersion;
20
21pub mod v3;
22pub mod v4;
23pub mod v5;
24pub mod v6;
25pub mod v7;
26
27pub use v3::V3Validator;
28pub use v4::V4Validator;
29pub use v5::V5Validator;
30pub use v6::V6Validator;
31pub use v7::V7Validator;
32
33#[derive(Debug, Clone, Default)]
40pub struct ValidationReport {
41 pub schema_version: Option<GenomeSchemaVersion>,
45 pub errors: Vec<String>,
46 pub warnings: Vec<String>,
47}
48
49impl ValidationReport {
50 pub fn new(schema_version: GenomeSchemaVersion) -> Self {
51 Self {
52 schema_version: Some(schema_version),
53 errors: Vec::new(),
54 warnings: Vec::new(),
55 }
56 }
57
58 pub fn add_error(&mut self, msg: impl Into<String>) {
59 self.errors.push(msg.into());
60 }
61
62 pub fn add_warning(&mut self, msg: impl Into<String>) {
63 self.warnings.push(msg.into());
64 }
65
66 pub fn has_errors(&self) -> bool {
68 !self.errors.is_empty()
69 }
70
71 pub fn is_clean(&self) -> bool {
73 self.errors.is_empty() && self.warnings.is_empty()
74 }
75}
76
77pub trait Validator: Send + Sync {
83 fn schema_version(&self) -> GenomeSchemaVersion;
85
86 fn validate(&self, genome: &Value) -> ValidationReport;
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn report_starts_clean() {
96 let r = ValidationReport::new(GenomeSchemaVersion(3));
97 assert!(r.is_clean());
98 assert!(!r.has_errors());
99 assert_eq!(r.schema_version, Some(GenomeSchemaVersion(3)));
100 }
101
102 #[test]
103 fn add_error_breaks_clean_and_blocks() {
104 let mut r = ValidationReport::new(GenomeSchemaVersion(3));
105 r.add_error("missing field");
106 assert!(r.has_errors());
107 assert!(!r.is_clean());
108 assert_eq!(r.errors, vec!["missing field".to_string()]);
109 }
110
111 #[test]
112 fn warning_is_advisory_only() {
113 let mut r = ValidationReport::new(GenomeSchemaVersion(3));
114 r.add_warning("nudge");
115 assert!(!r.has_errors());
116 assert!(!r.is_clean());
117 assert_eq!(r.warnings, vec!["nudge".to_string()]);
118 }
119}