-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Began implementing 'not' modifier. Still need validation testing * Add tests for 'not' modifier * Fix modifier nesting for 'not' inversion * Implement 'required' modifier for object, but have not made fields optional yet, nor written tests for them * Remove debug printing statement * Implement oneOf modifier. Still needs more tests * Add test case for oneOf modifier for complex(ish) validators like 'string' with min and max length set, where overlap may occur * Make object members optional by default and add tests for 'required' tag on objects * Remove unnecessary import of smallvec * Implement anyOf & allOf * Numerous linting fixes * Remove unnecessary return type
- Loading branch information
1 parent
2a9efc5
commit 7c09d93
Showing
10 changed files
with
818 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use crate::error::{add_path_name, condense_errors, SchemaError, SchemaErrorKind}; | ||
use crate::utils::YamlUtils; | ||
use crate::{Context, PropertyType, Validate}; | ||
use std::convert::TryFrom; | ||
use yaml_rust::Yaml; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct SchemaAllOf<'schema> { | ||
items: Vec<PropertyType<'schema>>, | ||
} | ||
|
||
impl<'schema> TryFrom<&'schema Yaml> for SchemaAllOf<'schema> { | ||
type Error = SchemaError<'schema>; | ||
|
||
fn try_from(yaml: &'schema Yaml) -> Result<Self, Self::Error> { | ||
yaml.strict_contents(&["allOf"], &[])?; | ||
let (items, errs): (Vec<_>, Vec<_>) = yaml | ||
.lookup("allOf", "array", Yaml::as_vec)? | ||
.iter() | ||
.map(|property| PropertyType::try_from(property).map_err(add_path_name("items"))) | ||
.partition(Result::is_ok); | ||
|
||
condense_errors(&mut errs.into_iter())?; | ||
|
||
if items.is_empty() { | ||
return Err(SchemaErrorKind::MalformedField { | ||
error: "allOf modifier requires an array of schemas to validate against".to_owned(), | ||
} | ||
.with_path_name("allOf")); | ||
} | ||
|
||
Ok(SchemaAllOf { | ||
items: items.into_iter().map(Result::unwrap).collect(), | ||
}) | ||
} | ||
} | ||
|
||
impl<'yaml, 'schema: 'yaml> Validate<'yaml, 'schema> for SchemaAllOf<'schema> { | ||
fn validate( | ||
&self, | ||
ctx: &'schema Context<'schema>, | ||
yaml: &'yaml Yaml, | ||
) -> Result<(), SchemaError<'yaml>> { | ||
let errs: Vec<_> = self | ||
.items | ||
.iter() | ||
.map(|schema| schema.validate(ctx, yaml).map_err(add_path_name("allOf"))) | ||
.filter(Result::is_err) | ||
.collect(); | ||
|
||
condense_errors(&mut errs.into_iter())?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::utils::load_simple; | ||
|
||
#[test] | ||
fn one_of_from_yaml() { | ||
SchemaAllOf::try_from(&load_simple( | ||
r#" | ||
allOf: | ||
- type: integer | ||
- type: string | ||
"#, | ||
)) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
SchemaAllOf::try_from(&load_simple( | ||
r#" | ||
allOff: | ||
- type: integer | ||
"#, | ||
)) | ||
.unwrap_err(), | ||
SchemaErrorKind::Multiple { | ||
errors: vec![ | ||
SchemaErrorKind::FieldMissing { field: "allOf" }.into(), | ||
SchemaErrorKind::ExtraField { field: "allOff" }.into(), | ||
] | ||
} | ||
.into() | ||
) | ||
} | ||
|
||
#[test] | ||
fn validate_unit_case() { | ||
let yaml = load_simple( | ||
r#" | ||
allOf: | ||
- type: integer | ||
"#, | ||
); | ||
let schema = SchemaAllOf::try_from(&yaml).unwrap(); | ||
|
||
schema | ||
.validate(&Context::default(), &load_simple("10")) | ||
.unwrap(); | ||
} | ||
|
||
#[test] | ||
fn validate_multiple_subvalidators() { | ||
let yaml = load_simple( | ||
r#" | ||
allOf: | ||
- type: string | ||
minLength: 10 | ||
- type: string | ||
maxLength: 10 | ||
"#, | ||
); | ||
|
||
let schema = SchemaAllOf::try_from(&yaml).unwrap(); | ||
|
||
// Validate against a 10-character long string, causing overlap! | ||
schema | ||
.validate(&Context::default(), &load_simple("hello you!")) | ||
.unwrap(); | ||
} | ||
} |
Oops, something went wrong.