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
use serde::{Deserialize, Serialize}; use serde_yaml::Value; #[cfg(test)] mod tests; pub mod error; use error::{Result, *}; pub trait YamlValidator<'a> { fn validate(&'a self, value: &'a Value) -> Result<'a>; } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(rename_all = "lowercase")] enum DataSigned { Signed, Unsigned, } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] struct DataNumber { #[serde(skip_serializing_if = "Option::is_none")] pub min: Option<i128>, #[serde(skip_serializing_if = "Option::is_none")] pub max: Option<i128>, #[serde(skip_serializing_if = "Option::is_none")] pub sign: Option<DataSigned>, } impl<'a> YamlValidator<'a> for DataNumber { fn validate(&'a self, value: &'a Value) -> Result<'a> { if let Value::Number(_) = value { Ok(()) } else { Err(YamlValidationError::WrongType("number", value)) } } } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] struct DataString { #[serde(skip_serializing_if = "Option::is_none")] pub max_length: Option<usize>, #[serde(skip_serializing_if = "Option::is_none")] pub min_length: Option<usize>, } impl<'a> YamlValidator<'a> for DataString { fn validate(&'a self, value: &'a Value) -> Result<'a> { if let Value::String(inner) = value { if let Some(max_length) = self.max_length { if inner.len() > max_length { return Err(StringValidationError::TooLong(max_length, inner.len()).into()); } } if let Some(min_length) = self.min_length { if inner.len() < min_length { return Err(StringValidationError::TooShort(min_length, inner.len()).into()); } } Ok(()) } else { Err(YamlValidationError::WrongType("string", value)) } } } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] struct DataDictionary { pub key: Box<PropertyType>, pub value: Box<PropertyType>, } impl<'a> YamlValidator<'a> for DataDictionary { fn validate(&'a self, value: &'a Value) -> Result<'a> { if let Value::Mapping(_) = value { Ok(()) } else { Err(YamlValidationError::WrongType("dictionary", value)) } } } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] struct DataList { pub inner: Box<PropertyType>, } impl<'a> YamlValidator<'a> for DataList { fn validate(&'a self, value: &'a Value) -> Result<'a> { if let serde_yaml::Value::Sequence(items) = value { for item in items.iter() { self.inner.validate(item)?; } Ok(()) } else { Err(YamlValidationError::WrongType("list", value)) } } } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] struct DataObject { pub fields: Vec<Property>, } impl<'a> YamlValidator<'a> for DataObject { fn validate(&'a self, value: &'a Value) -> Result<'a> { if let Value::Mapping(ref obj) = value { for prop in self.fields.iter() { if let Some(field) = obj.get(&serde_yaml::to_value(&prop.name).unwrap()) { prop.datatype.validate(field)? } else { return Err(YamlValidationError::MissingField(&prop.name)); } } Ok(()) } else { Err(YamlValidationError::WrongType("object", value)) } } } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(rename_all = "lowercase", tag = "type")] enum PropertyType { #[serde(rename = "number")] DataNumber(DataNumber), #[serde(rename = "string")] DataString(DataString), #[serde(rename = "list")] DataList(DataList), #[serde(rename = "dictionary")] DataDictionary(DataDictionary), #[serde(rename = "object")] DataObject(DataObject), } impl<'a> YamlValidator<'a> for PropertyType { fn validate(&'a self, value: &'a Value) -> Result<'a> { match self { PropertyType::DataNumber(p) => p.validate(value), PropertyType::DataString(p) => p.validate(value), PropertyType::DataList(p) => p.validate(value), PropertyType::DataDictionary(p) => p.validate(value), PropertyType::DataObject(p) => p.validate(value), } } } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(rename_all = "lowercase")] struct Property { pub name: String, #[serde(flatten)] pub datatype: PropertyType, } #[derive(Serialize, Deserialize, Debug)] pub struct YamlSchema { schema: Vec<Property>, } impl YamlSchema { pub fn from_str(schema: &str) -> YamlSchema { serde_yaml::from_str(schema).expect("failed to parse string as yaml") } pub fn validate_str(&self, yaml: &str) -> std::result::Result<(), String> { match self.validate(&serde_yaml::from_str(yaml).expect("failed to parse string as yaml")) { Ok(()) => Ok(()), Err(e) => Err(format!("{}", e)), } } } impl<'a> YamlValidator<'a> for YamlSchema { fn validate(&'a self, value: &'a Value) -> Result<'a> { if let serde_yaml::Value::Mapping(map) = value { for prop in self.schema.iter() { if let Some(field) = map.get(&serde_yaml::to_value(&prop.name).unwrap()) { prop.datatype.validate(field)? } else { return Ok(()); } } Ok(()) } else { Err(YamlValidationError::WrongType("resource definition", value)) } } }