Skip to content
Closed
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
8 changes: 7 additions & 1 deletion crates/jsonschema/src/keywords/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::paths::{LazyLocation, Location};

use crate::{error::ValidationError, keywords::CompilationResult, validator::Validate};
use crate::{
error::ValidationError, keywords::CompilationResult, types::JsonTypeSet, validator::Validate,
};
use serde_json::Value;

pub(crate) struct FalseValidator {
Expand Down Expand Up @@ -28,6 +30,10 @@ impl Validate for FalseValidator {
instance,
))
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::empty()
}
}

#[cfg(test)]
Expand Down
35 changes: 34 additions & 1 deletion crates/jsonschema/src/keywords/const_.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::{
compiler, error::ValidationError, ext::cmp, keywords::CompilationResult, paths::Location,
compiler,
error::ValidationError,
ext::cmp,
keywords::CompilationResult,
paths::Location,
types::{JsonType, JsonTypeSet},
validator::Validate,
};
use serde_json::{Map, Number, Value};
Expand Down Expand Up @@ -45,6 +50,10 @@ impl Validate for ConstArrayValidator {
false
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::empty().insert(JsonType::Array)
}
}

struct ConstBooleanValidator {
Expand Down Expand Up @@ -83,6 +92,10 @@ impl Validate for ConstBooleanValidator {
false
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::empty().insert(JsonType::Boolean)
}
}

struct ConstNullValidator {
Expand Down Expand Up @@ -114,6 +127,10 @@ impl Validate for ConstNullValidator {
fn is_valid(&self, instance: &Value) -> bool {
instance.is_null()
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::empty().insert(JsonType::Null)
}
}

struct ConstNumberValidator {
Expand Down Expand Up @@ -157,6 +174,14 @@ impl Validate for ConstNumberValidator {
false
}
}

fn applicable_types(&self) -> JsonTypeSet {
let mut set = JsonTypeSet::empty().insert(JsonType::Number);
if self.original_value.is_i64() || self.original_value.is_u64() {
set = set.insert(JsonType::Integer);
}
set
}
}

pub(crate) struct ConstObjectValidator {
Expand Down Expand Up @@ -198,6 +223,10 @@ impl Validate for ConstObjectValidator {
false
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::empty().insert(JsonType::Object)
}
}

pub(crate) struct ConstStringValidator {
Expand Down Expand Up @@ -239,6 +268,10 @@ impl Validate for ConstStringValidator {
false
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::empty().insert(JsonType::String)
}
}

#[inline]
Expand Down
17 changes: 17 additions & 0 deletions crates/jsonschema/src/keywords/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
keywords::CompilationResult,
node::SchemaNode,
paths::LazyLocation,
types::JsonTypeSet,
validator::{PartialApplication, Validate},
Draft,
};
Expand Down Expand Up @@ -85,6 +86,10 @@ impl Validate for ContainsValidator {
result
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::arrays()
}
}

/// `minContains` validation. Used only if there is no `maxContains` present.
Expand Down Expand Up @@ -164,6 +169,10 @@ impl Validate for MinContainsValidator {
true
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::arrays()
}
}

/// `maxContains` validation. Used only if there is no `minContains` present.
Expand Down Expand Up @@ -247,6 +256,10 @@ impl Validate for MaxContainsValidator {
true
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::arrays()
}
}

/// `maxContains` & `minContains` validation combined.
Expand Down Expand Up @@ -333,6 +346,10 @@ impl Validate for MinMaxContainsValidator {
true
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::arrays()
}
}

#[inline]
Expand Down
14 changes: 13 additions & 1 deletion crates/jsonschema/src/keywords/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
error::ValidationError,
keywords::CompilationResult,
paths::{LazyLocation, Location},
types::JsonType,
types::{JsonType, JsonTypeSet},
validator::Validate,
};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -63,6 +63,10 @@ impl Validate for ContentMediaTypeValidator {
Ok(())
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::strings()
}
}

/// Validator for `contentEncoding` keyword.
Expand Down Expand Up @@ -116,6 +120,10 @@ impl Validate for ContentEncodingValidator {
Ok(())
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::strings()
}
}

/// Combined validator for both `contentEncoding` and `contentMediaType` keywords.
Expand Down Expand Up @@ -190,6 +198,10 @@ impl Validate for ContentMediaTypeAndEncodingValidator {
Ok(())
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::strings()
}
}

#[inline]
Expand Down
12 changes: 11 additions & 1 deletion crates/jsonschema/src/keywords/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl EnumValidator {
) -> CompilationResult<'a> {
let mut types = JsonTypeSet::empty();
for item in items {
types = types.insert(JsonType::from(item));
types = types.union(JsonTypeSet::from_value(item));
}
Ok(Box::new(EnumValidator {
options: schema.clone(),
Expand Down Expand Up @@ -66,13 +66,18 @@ impl Validate for EnumValidator {
false
}
}

fn applicable_types(&self) -> JsonTypeSet {
self.types
}
}

#[derive(Debug)]
pub(crate) struct SingleValueEnumValidator {
value: Value,
options: Value,
location: Location,
types: JsonTypeSet,
}

impl SingleValueEnumValidator {
Expand All @@ -86,6 +91,7 @@ impl SingleValueEnumValidator {
options: schema.clone(),
value: value.clone(),
location,
types: JsonTypeSet::from_value(value),
}))
}
}
Expand All @@ -111,6 +117,10 @@ impl Validate for SingleValueEnumValidator {
fn is_valid(&self, instance: &Value) -> bool {
cmp::equal(&self.value, instance)
}

fn applicable_types(&self) -> JsonTypeSet {
self.types
}
}

#[inline]
Expand Down
8 changes: 8 additions & 0 deletions crates/jsonschema/src/keywords/legacy/type_draft_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl Validate for MultipleTypesValidator {
))
}
}

fn applicable_types(&self) -> JsonTypeSet {
self.types
}
}

pub(crate) struct IntegerTypeValidator {
Expand Down Expand Up @@ -105,6 +109,10 @@ impl Validate for IntegerTypeValidator {
))
}
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::empty().insert(JsonType::Integer)
}
}

fn is_integer(num: &Number) -> bool {
Expand Down
5 changes: 5 additions & 0 deletions crates/jsonschema/src/keywords/max_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
error::ValidationError,
keywords::{helpers::fail_on_non_positive_integer, CompilationResult},
paths::{LazyLocation, Location},
types::JsonTypeSet,
validator::Validate,
};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -65,6 +66,10 @@ impl Validate for MaxItemsValidator {
}
Ok(())
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::arrays()
}
}

#[inline]
Expand Down
5 changes: 5 additions & 0 deletions crates/jsonschema/src/keywords/max_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
error::ValidationError,
keywords::{helpers::fail_on_non_positive_integer, CompilationResult},
paths::{LazyLocation, Location},
types::JsonTypeSet,
validator::Validate,
};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -65,6 +66,10 @@ impl Validate for MaxLengthValidator {
}
Ok(())
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::strings()
}
}

#[inline]
Expand Down
5 changes: 5 additions & 0 deletions crates/jsonschema/src/keywords/max_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
error::ValidationError,
keywords::{helpers::fail_on_non_positive_integer, CompilationResult},
paths::{LazyLocation, Location},
types::JsonTypeSet,
validator::Validate,
};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -65,6 +66,10 @@ impl Validate for MaxPropertiesValidator {
}
Ok(())
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::objects()
}
}

#[inline]
Expand Down
5 changes: 5 additions & 0 deletions crates/jsonschema/src/keywords/min_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
error::ValidationError,
keywords::{helpers::fail_on_non_positive_integer, CompilationResult},
paths::{LazyLocation, Location},
types::JsonTypeSet,
validator::Validate,
};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -65,6 +66,10 @@ impl Validate for MinItemsValidator {
}
Ok(())
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::arrays()
}
}

#[inline]
Expand Down
5 changes: 5 additions & 0 deletions crates/jsonschema/src/keywords/min_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
error::ValidationError,
keywords::{helpers::fail_on_non_positive_integer, CompilationResult},
paths::{LazyLocation, Location},
types::JsonTypeSet,
validator::Validate,
};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -65,6 +66,10 @@ impl Validate for MinLengthValidator {
}
Ok(())
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::strings()
}
}

#[inline]
Expand Down
5 changes: 5 additions & 0 deletions crates/jsonschema/src/keywords/min_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
error::ValidationError,
keywords::{helpers::fail_on_non_positive_integer, CompilationResult},
paths::{LazyLocation, Location},
types::JsonTypeSet,
validator::Validate,
};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -65,6 +66,10 @@ impl Validate for MinPropertiesValidator {
}
Ok(())
}

fn applicable_types(&self) -> JsonTypeSet {
JsonTypeSet::objects()
}
}

#[inline]
Expand Down
Loading
Loading