Skip to main content

yaml_schema/
validation.rs

1//! The validation module contains the logic for validating a YAML schema against a YAML value
2
3use 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
17/// A trait for validating a sahpyr::Yaml value against a schema
18pub trait Validator {
19    fn validate(&self, context: &Context, value: &saphyr::MarkedYaml) -> Result<()>;
20}
21
22/// A validation error simply contains a path and an error message
23#[derive(Debug)]
24pub struct ValidationError {
25    /// The path to the value that caused the error
26    pub path: String,
27    /// The line and column of the value that caused the error
28    pub marker: Option<Marker>,
29    /// The error message
30    pub error: String,
31}
32
33/// Display these ValidationErrors as "{path}: {error}"
34impl 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, // contrary to the documentation, columns are 0-indexed
42                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}