yaml_schema/
validation.rs1use saphyr::Marker;
4
5use crate::Result;
6
7pub(crate) mod annotations;
8mod context;
9pub(crate) mod formats;
10mod objects;
11mod strings;
12
13pub use annotations::ArrayUnevaluatedAnnotations;
14pub use annotations::ObjectEvaluatedNames;
15pub use context::Context;
16
17pub trait Validator {
19 fn validate(&self, context: &Context, value: &saphyr::MarkedYaml) -> Result<()>;
20}
21
22#[derive(Debug)]
24pub struct ValidationError {
25 pub path: String,
27 pub marker: Option<Marker>,
29 pub error: String,
31}
32
33impl std::fmt::Display for ValidationError {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 if let Some(marker) = &self.marker {
37 write!(
38 f,
39 "[{}:{}] .{}: {}",
40 marker.line(),
41 marker.col() + 1, self.path,
43 self.error
44 )
45 } else {
46 write!(f, ".{}: {}", self.path, self.error)
47 }
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54 use crate::YamlSchema;
55 use saphyr::LoadableYamlNode;
56
57 #[test]
58 fn test_validate_empty_schema() {
59 let schema = YamlSchema::Empty;
60 let context = Context::default();
61 let docs = saphyr::MarkedYaml::load_from_str("value").unwrap();
62 let value = docs.first().unwrap();
63 let result = schema.validate(&context, value);
64 assert!(result.is_ok());
65 assert!(!context.has_errors());
66 }
67
68 #[test]
69 fn test_validate_null() {
70 let schema = YamlSchema::Null;
71 let context = Context::default();
72 let docs = saphyr::MarkedYaml::load_from_str("value").unwrap();
73 let value = docs.first().unwrap();
74 let result = schema.validate(&context, value);
75 assert!(result.is_ok());
76 assert!(context.has_errors());
77 let errors = context.errors.borrow();
78 let error = errors.first().unwrap();
79 assert_eq!(error.error, r#"Expected null, but got: "value""#);
80 }
81}