Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Draft 4 support #34

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ fn main() {
}
```

Fully supported drafts (with optional test cases included):
Supported drafts:
- Draft 7
- Draft 6
- Draft 4 (except optional `bignum.json` test case)

## Performance

Expand Down
28 changes: 23 additions & 5 deletions draft/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use heck::SnakeCase;
use heck::{SnakeCase, TitleCase};
use proc_macro::TokenStream;
use serde_json::{from_str, Value};
use std::fs;
use std::fs::File;
use std::io::Read;
use std::path::Path;

const TEST_TO_IGNORE: &[&str] = &["draft4_optional_bignum"];

fn should_ignore_test(prefix_test_name: &str) -> bool {
TEST_TO_IGNORE
.iter()
.any(|test_to_ignore| prefix_test_name.starts_with(test_to_ignore))
}

#[proc_macro]
pub fn test_draft(input: TokenStream) -> TokenStream {
let dir_name = input.to_string();
Expand All @@ -28,9 +36,12 @@ pub fn test_draft(input: TokenStream) -> TokenStream {
let description = test.get("description").unwrap().as_str().unwrap();
let data = test.get("data").unwrap();
let valid = test.get("valid").unwrap().as_bool().unwrap();
if should_ignore_test(&file_name) {
output.push_str("\n#[ignore]\n");
}
output.push_str("\n#[test]\n");
output.push_str(&format!("fn {}_{}_{}()", file_name, i, j));
output.push_str(&make_fn_body(schema, data, &description, valid))
output.push_str(&make_fn_body(schema, data, &description, valid, &draft))
}
}
}
Expand Down Expand Up @@ -68,7 +79,13 @@ fn load_tests(dir: &Path, prefix: String) -> Vec<(String, Value)> {
tests
}

fn make_fn_body(schema: &Value, data: &Value, description: &str, valid: bool) -> String {
fn make_fn_body(
schema: &Value,
data: &Value,
description: &str,
valid: bool,
draft: &str,
) -> String {
let mut output = "{".to_string();
output.push_str(&format!(
r###"
Expand All @@ -78,13 +95,14 @@ fn make_fn_body(schema: &Value, data: &Value, description: &str, valid: bool) ->
let data: serde_json::Value = serde_json::from_str(data_str).unwrap();
let description = r#"{}"#;
println!("Description: {{}}", description);
let compiled = jsonschema::JSONSchema::compile(&schema, None).unwrap();
let compiled = jsonschema::JSONSchema::compile(&schema, Some(jsonschema::Draft::{})).unwrap();
let result = compiled.validate(&data);
assert_eq!(result.is_ok(), compiled.is_valid(&data));
"###,
schema.to_string(),
data.to_string(),
description
description,
draft.to_title_case()
));
if valid {
output.push_str(
Expand Down
15 changes: 15 additions & 0 deletions src/keywords/legacy/maximum_draft_4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::super::CompilationResult;
use super::super::{exclusive_maximum, maximum};
use crate::compilation::CompilationContext;
use serde_json::{Map, Value};

pub(crate) fn compile(
parent: &Map<String, Value>,
schema: &Value,
context: &CompilationContext,
) -> Option<CompilationResult> {
match parent.get("exclusiveMaximum") {
Some(Value::Bool(true)) => exclusive_maximum::compile(parent, schema, context),
_ => maximum::compile(parent, schema, context),
}
}
15 changes: 15 additions & 0 deletions src/keywords/legacy/minimum_draft_4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::super::CompilationResult;
use super::super::{exclusive_minimum, minimum};
use crate::compilation::CompilationContext;
use serde_json::{Map, Value};

pub(crate) fn compile(
parent: &Map<String, Value>,
schema: &Value,
context: &CompilationContext,
) -> Option<CompilationResult> {
match parent.get("exclusiveMinimum") {
Some(Value::Bool(true)) => exclusive_minimum::compile(parent, schema, context),
_ => minimum::compile(parent, schema, context),
}
}
3 changes: 3 additions & 0 deletions src/keywords/legacy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) mod maximum_draft_4;
pub(crate) mod minimum_draft_4;
pub(crate) mod type_draft_4;
125 changes: 125 additions & 0 deletions src/keywords/legacy/type_draft_4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use super::super::{type_, CompilationResult, Validate};
use crate::compilation::{CompilationContext, JSONSchema};
use crate::error::{no_error, CompilationError, ErrorIterator, PrimitiveType, ValidationError};
use serde_json::{Map, Number, Value};

pub struct MultipleTypesValidator {
types: Vec<PrimitiveType>,
}

impl MultipleTypesValidator {
pub(crate) fn compile(items: &[Value]) -> CompilationResult {
let mut types = Vec::with_capacity(items.len());
for item in items {
match item {
Value::String(string) => match string.as_str() {
"integer" => types.push(PrimitiveType::Integer),
"null" => types.push(PrimitiveType::Null),
"boolean" => types.push(PrimitiveType::Boolean),
"string" => types.push(PrimitiveType::String),
"array" => types.push(PrimitiveType::Array),
"object" => types.push(PrimitiveType::Object),
"number" => types.push(PrimitiveType::Number),
_ => return Err(CompilationError::SchemaError),
},
_ => return Err(CompilationError::SchemaError),
}
}
Ok(Box::new(MultipleTypesValidator { types }))
}
}

impl Validate for MultipleTypesValidator {
fn validate<'a>(&self, schema: &'a JSONSchema, instance: &'a Value) -> ErrorIterator<'a> {
if !self.is_valid(schema, instance) {
return ValidationError::multiple_type_error(instance.clone(), self.types.clone());
}
no_error()
}
fn is_valid(&self, _: &JSONSchema, instance: &Value) -> bool {
for type_ in self.types.iter() {
match (type_, instance) {
(PrimitiveType::Integer, Value::Number(num)) if is_integer(num) => return true,
(PrimitiveType::Null, Value::Null)
| (PrimitiveType::Boolean, Value::Bool(_))
| (PrimitiveType::String, Value::String(_))
| (PrimitiveType::Array, Value::Array(_))
| (PrimitiveType::Object, Value::Object(_))
| (PrimitiveType::Number, Value::Number(_)) => return true,
(_, _) => continue,
};
}
false
}

fn name(&self) -> String {
format!("<type: {:?}>", self.types)
}
}

pub struct IntegerTypeValidator {}

impl IntegerTypeValidator {
pub(crate) fn compile() -> CompilationResult {
Ok(Box::new(IntegerTypeValidator {}))
}
}

impl Validate for IntegerTypeValidator {
fn validate<'a>(&self, schema: &'a JSONSchema, instance: &'a Value) -> ErrorIterator<'a> {
if !self.is_valid(schema, instance) {
return ValidationError::single_type_error(instance.clone(), PrimitiveType::Integer);
}
no_error()
}

fn is_valid(&self, _: &JSONSchema, instance: &Value) -> bool {
if let Value::Number(num) = instance {
return is_integer(num);
}
false
}

fn name(&self) -> String {
"<type: integer>".to_string()
}
}

fn is_integer(num: &Number) -> bool {
num.is_u64() || num.is_i64()
}

pub(crate) fn compile(
_: &Map<String, Value>,
schema: &Value,
_: &CompilationContext,
) -> Option<CompilationResult> {
match schema {
Value::String(item) => compile_single_type(item.as_str()),
Value::Array(items) => {
if items.len() == 1 {
if let Some(Value::String(item)) = items.iter().next() {
compile_single_type(item.as_str())
} else {
Some(Err(CompilationError::SchemaError))
}
} else {
Some(MultipleTypesValidator::compile(items))
}
}
_ => Some(Err(CompilationError::SchemaError)),
}
}

fn compile_single_type(item: &str) -> Option<CompilationResult> {
match item {
"integer" => Some(IntegerTypeValidator::compile()),
"null" => Some(type_::NullTypeValidator::compile()),
"boolean" => Some(type_::BooleanTypeValidator::compile()),
"string" => Some(type_::StringTypeValidator::compile()),
"array" => Some(type_::ArrayTypeValidator::compile()),
"object" => Some(type_::ObjectTypeValidator::compile()),
"number" => Some(type_::NumberTypeValidator::compile()),
_ => Some(Err(CompilationError::SchemaError)),
}
}
1 change: 1 addition & 0 deletions src/keywords/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub(crate) mod format;
pub(crate) mod helpers;
pub(crate) mod if_;
pub(crate) mod items;
pub(crate) mod legacy;
pub(crate) mod max_items;
pub(crate) mod max_length;
pub(crate) mod max_properties;
Expand Down
3 changes: 1 addition & 2 deletions src/keywords/type_.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::{CompilationResult, Validate};
use crate::compilation::CompilationContext;
use crate::compilation::JSONSchema;
use crate::compilation::{CompilationContext, JSONSchema};
use crate::error::{no_error, CompilationError, ErrorIterator, PrimitiveType, ValidationError};
use serde_json::{Map, Number, Value};

Expand Down
28 changes: 28 additions & 0 deletions src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ impl Draft {
"uniqueItems" => Some(keywords::unique_items::compile),
_ => None,
},
Draft::Draft4 => match keyword {
"additionalItems" => Some(keywords::additional_items::compile),
"additionalProperties" => Some(keywords::additional_properties::compile),
"allOf" => Some(keywords::all_of::compile),
"anyOf" => Some(keywords::any_of::compile),
"dependencies" => Some(keywords::dependencies::compile),
"enum" => Some(keywords::enum_::compile),
"format" => Some(keywords::format::compile),
"items" => Some(keywords::items::compile),
"maximum" => Some(keywords::legacy::maximum_draft_4::compile),
"maxItems" => Some(keywords::max_items::compile),
"maxLength" => Some(keywords::max_length::compile),
"maxProperties" => Some(keywords::max_properties::compile),
"minimum" => Some(keywords::legacy::minimum_draft_4::compile),
"minItems" => Some(keywords::min_items::compile),
"minLength" => Some(keywords::min_length::compile),
"minProperties" => Some(keywords::min_properties::compile),
"multipleOf" => Some(keywords::multiple_of::compile),
"not" => Some(keywords::not::compile),
"oneOf" => Some(keywords::one_of::compile),
"pattern" => Some(keywords::pattern::compile),
"patternProperties" => Some(keywords::pattern_properties::compile),
"properties" => Some(keywords::properties::compile),
"required" => Some(keywords::required::compile),
"type" => Some(keywords::legacy::type_draft_4::compile),
"uniqueItems" => Some(keywords::unique_items::compile),
_ => None,
},
_ => None,
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/test_suite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use draft::test_draft;

test_draft!("tests/suite/tests/draft4/");
test_draft!("tests/suite/tests/draft6/");
test_draft!("tests/suite/tests/draft7/");