1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::collections::BTreeMap;
use std::convert::TryFrom;
pub use yaml_rust;
use yaml_rust::Yaml;

mod error;
mod types;
mod utils;
use types::*;

use error::{add_path_name, optional};
pub use error::{SchemaError, SchemaErrorKind};

use utils::YamlUtils;

/// Validation trait implemented by all types, as well as the [Schema](crate::Schema) type
pub trait Validate<'yaml, 'schema: 'yaml> {
    fn validate(
        &self,
        ctx: &'schema Context<'schema>,
        yaml: &'yaml Yaml,
    ) -> Result<(), SchemaError<'yaml>>;
}

/// Contains a number of schemas that may or may not be dependent on each other.
#[derive(Debug, Default)]
pub struct Context<'schema> {
    schemas: BTreeMap<&'schema str, Schema<'schema>>,
}

impl<'schema> Context<'schema> {
    /// Get a reference to a single schema within the context to use for validation.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use yaml_rust::YamlLoader;
    /// # use std::convert::TryFrom;
    /// # use yaml_validator::{Validate, Context};
    /// #
    /// let schemas = vec![
    ///     YamlLoader::load_from_str(r#"
    ///         uri: just-a-number
    ///         schema:
    ///             type: integer
    ///     "#).unwrap().remove(0)
    /// ];
    ///
    /// let context = Context::try_from(&schemas).unwrap();
    /// let document = YamlLoader::load_from_str("10").unwrap().remove(0);
    ///
    /// context.get_schema("just-a-number").unwrap()
    ///     .validate(&context, &document).unwrap();
    /// ```
    pub fn get_schema(&self, uri: &str) -> Option<&Schema<'schema>> {
        self.schemas.get(uri)
    }
}

/// A context can only be created from a vector of Yaml documents, all of which must fit the schema layout.
impl<'schema> TryFrom<&'schema Vec<Yaml>> for Context<'schema> {
    type Error = SchemaError<'schema>;
    fn try_from(documents: &'schema Vec<Yaml>) -> Result<Self, Self::Error> {
        let (schemas, errs): (Vec<_>, Vec<_>) = documents
            .iter()
            .map(Schema::try_from)
            .partition(Result::is_ok);

        if !errs.is_empty() {
            let mut errors: Vec<SchemaError<'schema>> =
                errs.into_iter().map(Result::unwrap_err).collect();
            if errors.len() == 1 {
                return Err(errors.pop().unwrap());
            } else {
                return Err(SchemaErrorKind::Multiple { errors }.into());
            }
        }

        Ok(Context {
            schemas: schemas
                .into_iter()
                .map(Result::unwrap)
                .map(|schema| (schema.uri, schema))
                .collect(),
        })
    }
}

#[derive(Debug)]
enum PropertyType<'schema> {
    Object(SchemaObject<'schema>),
    Array(SchemaArray<'schema>),
    Hash(SchemaHash<'schema>),
    String(SchemaString),
    Integer(SchemaInteger),
    Real(SchemaReal),
    Reference(SchemaReference<'schema>),
}

impl<'schema> TryFrom<&'schema Yaml> for PropertyType<'schema> {
    type Error = SchemaError<'schema>;
    fn try_from(yaml: &'schema Yaml) -> Result<Self, Self::Error> {
        let reference = yaml
            .lookup("$ref", "string", Yaml::as_str)
            .map(Option::from)
            .or_else(optional(None))?;

        if let Some(uri) = reference {
            return Ok(PropertyType::Reference(SchemaReference { uri }));
        }

        let typename = yaml.lookup("type", "string", Yaml::as_str)?;

        match typename {
            "object" => Ok(PropertyType::Object(SchemaObject::try_from(yaml)?)),
            "string" => Ok(PropertyType::String(SchemaString::try_from(yaml)?)),
            "integer" => Ok(PropertyType::Integer(SchemaInteger::try_from(yaml)?)),
            "real" => Ok(PropertyType::Real(SchemaReal::try_from(yaml)?)),
            "array" => Ok(PropertyType::Array(SchemaArray::try_from(yaml)?)),
            "hash" => Ok(PropertyType::Hash(SchemaHash::try_from(yaml)?)),
            unknown_type => Err(SchemaErrorKind::UnknownType { unknown_type }.into()),
        }
    }
}

impl<'yaml, 'schema: 'yaml> Validate<'yaml, 'schema> for PropertyType<'schema> {
    fn validate(
        &self,
        ctx: &'schema Context<'schema>,
        yaml: &'yaml Yaml,
    ) -> Result<(), SchemaError<'yaml>> {
        match self {
            PropertyType::Integer(p) => p.validate(ctx, yaml),
            PropertyType::Real(p) => p.validate(ctx, yaml),
            PropertyType::String(p) => p.validate(ctx, yaml),
            PropertyType::Object(p) => p.validate(ctx, yaml),
            PropertyType::Array(p) => p.validate(ctx, yaml),
            PropertyType::Hash(p) => p.validate(ctx, yaml),
            PropertyType::Reference(p) => p.validate(ctx, yaml),
        }
    }
}

/// A single schema unit used for validation.
#[derive(Debug)]
pub struct Schema<'schema> {
    uri: &'schema str,
    schema: PropertyType<'schema>,
}

impl<'schema> TryFrom<&'schema Yaml> for Schema<'schema> {
    type Error = SchemaError<'schema>;
    fn try_from(yaml: &'schema Yaml) -> Result<Self, Self::Error> {
        yaml.strict_contents(&["uri", "schema"], &[])?;

        let uri = yaml.lookup("uri", "string", Yaml::as_str)?;
        let schema = PropertyType::try_from(yaml.lookup("schema", "yaml", Option::from)?)
            .map_err(add_path_name(uri))?;

        Ok(Schema { uri, schema })
    }
}

impl<'yaml, 'schema: 'yaml> Validate<'yaml, 'schema> for Schema<'schema> {
    fn validate(
        &self,
        ctx: &'schema Context<'schema>,
        yaml: &'yaml Yaml,
    ) -> Result<(), SchemaError<'yaml>> {
        self.schema.validate(ctx, yaml)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::load_simple;
    use crate::Context;
    use yaml_rust::YamlLoader;

    #[test]
    fn from_yaml() {
        let yaml = YamlLoader::load_from_str(
            r#"---
uri: test
schema:
  type: integer
---
uri: another
schema:
  $ref: test
"#,
        )
        .unwrap();

        let context = Context::try_from(&yaml).unwrap();
        let schema = context.get_schema("another").unwrap();
        dbg!(&context);
        dbg!(&schema);
        schema.validate(&context, &load_simple("20")).unwrap();
    }
}