use crate::{
compilation::{compile_validators, context::CompilationContext},
error::{error, no_error, ErrorIterator, ValidationError},
keywords::CompilationResult,
output::{Annotations, BasicOutput, OutputUnit},
paths::{AbsolutePath, JSONPointer, JsonPointerNode},
properties::*,
schema_node::SchemaNode,
validator::{format_validators, PartialApplication, Validate},
};
use serde_json::{Map, Value};
macro_rules! is_valid {
($node:expr, $value:ident) => {{
$node.is_valid($value)
}};
}
macro_rules! is_valid_pattern_schema {
($node:expr, $value:ident) => {{
if $node.is_valid($value) {
continue;
}
return false;
}};
}
macro_rules! is_valid_patterns {
($patterns:expr, $property:ident, $value:ident) => {{
let mut has_match = false;
for (re, node) in $patterns {
if re.is_match($property).unwrap_or(false) {
has_match = true;
is_valid_pattern_schema!(node, $value)
}
}
if !has_match {
return false;
}
}};
}
macro_rules! validate {
($node:expr, $value:ident, $instance_path:expr, $property_name:expr) => {{
let instance_path = $instance_path.push($property_name.as_str());
$node.validate($value, &instance_path)
}};
}
pub(crate) struct AdditionalPropertiesValidator {
node: SchemaNode,
}
impl AdditionalPropertiesValidator {
#[inline]
pub(crate) fn compile<'a>(
schema: &'a Value,
context: &CompilationContext,
) -> CompilationResult<'a> {
let keyword_context = context.with_path("additionalProperties");
Ok(Box::new(AdditionalPropertiesValidator {
node: compile_validators(schema, &keyword_context)?,
}))
}
}
impl Validate for AdditionalPropertiesValidator {
fn is_valid(&self, instance: &Value) -> bool {
if let Value::Object(item) = instance {
item.values().all(|i| self.node.is_valid(i))
} else {
true
}
}
#[allow(clippy::needless_collect)]
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
if let Value::Object(item) = instance {
let errors: Vec<_> = item
.iter()
.flat_map(|(name, value)| validate!(self.node, value, instance_path, name))
.collect();
Box::new(errors.into_iter())
} else {
no_error()
}
}
fn apply<'a>(
&'a self,
instance: &Value,
instance_path: &JsonPointerNode,
) -> PartialApplication<'a> {
if let Value::Object(item) = instance {
let mut matched_props = Vec::with_capacity(item.len());
let mut output = BasicOutput::default();
for (name, value) in item {
let path = instance_path.push(name.as_str());
output += self.node.apply_rooted(value, &path);
matched_props.push(name.clone());
}
let mut result: PartialApplication = output.into();
result.annotate(Value::from(matched_props).into());
result
} else {
PartialApplication::valid_empty()
}
}
}
impl core::fmt::Display for AdditionalPropertiesValidator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"additionalProperties: {}",
format_validators(self.node.validators())
)
}
}
pub(crate) struct AdditionalPropertiesFalseValidator {
schema_path: JSONPointer,
}
impl AdditionalPropertiesFalseValidator {
#[inline]
pub(crate) fn compile<'a>(schema_path: JSONPointer) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesFalseValidator { schema_path }))
}
}
impl Validate for AdditionalPropertiesFalseValidator {
fn is_valid(&self, instance: &Value) -> bool {
if let Value::Object(item) = instance {
item.iter().next().is_none()
} else {
true
}
}
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
if let Value::Object(item) = instance {
if let Some((_, value)) = item.iter().next() {
return error(ValidationError::false_schema(
self.schema_path.clone(),
instance_path.into(),
value,
));
}
}
no_error()
}
}
impl core::fmt::Display for AdditionalPropertiesFalseValidator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
"additionalProperties: false".fmt(f)
}
}
pub(crate) struct AdditionalPropertiesNotEmptyFalseValidator<M: PropertiesValidatorsMap> {
properties: M,
schema_path: JSONPointer,
}
impl AdditionalPropertiesNotEmptyFalseValidator<SmallValidatorsMap> {
#[inline]
pub(crate) fn compile<'a>(
map: &'a Map<String, Value>,
context: &CompilationContext,
) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesNotEmptyFalseValidator {
properties: compile_small_map(map, context)?,
schema_path: context.as_pointer_with("additionalProperties"),
}))
}
}
impl AdditionalPropertiesNotEmptyFalseValidator<BigValidatorsMap> {
#[inline]
pub(crate) fn compile<'a>(
map: &'a Map<String, Value>,
context: &CompilationContext,
) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesNotEmptyFalseValidator {
properties: compile_big_map(map, context)?,
schema_path: context.as_pointer_with("additionalProperties"),
}))
}
}
impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesNotEmptyFalseValidator<M> {
fn is_valid(&self, instance: &Value) -> bool {
if let Value::Object(props) = instance {
are_properties_valid(&self.properties, props, |_| false)
} else {
true
}
}
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
if let Value::Object(item) = instance {
let mut errors = vec![];
let mut unexpected = vec![];
for (property, value) in item {
if let Some((name, node)) = self.properties.get_key_validator(property) {
errors.extend(validate!(node, value, instance_path, name));
} else {
unexpected.push(property.clone());
}
}
if !unexpected.is_empty() {
errors.push(ValidationError::additional_properties(
self.schema_path.clone(),
instance_path.into(),
instance,
unexpected,
))
}
Box::new(errors.into_iter())
} else {
no_error()
}
}
fn apply<'a>(
&'a self,
instance: &Value,
instance_path: &JsonPointerNode,
) -> PartialApplication<'a> {
if let Value::Object(item) = instance {
let mut unexpected = Vec::with_capacity(item.len());
let mut output = BasicOutput::default();
for (property, value) in item {
if let Some((_name, node)) = self.properties.get_key_validator(property) {
let path = instance_path.push(property.as_str());
output += node.apply_rooted(value, &path);
} else {
unexpected.push(property.clone())
}
}
let mut result: PartialApplication = output.into();
if !unexpected.is_empty() {
result.mark_errored(
ValidationError::additional_properties(
self.schema_path.clone(),
instance_path.into(),
instance,
unexpected,
)
.into(),
);
}
result
} else {
PartialApplication::valid_empty()
}
}
}
impl<M: PropertiesValidatorsMap> core::fmt::Display
for AdditionalPropertiesNotEmptyFalseValidator<M>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
"additionalProperties: false".fmt(f)
}
}
pub(crate) struct AdditionalPropertiesNotEmptyValidator<M: PropertiesValidatorsMap> {
node: SchemaNode,
properties: M,
}
impl AdditionalPropertiesNotEmptyValidator<SmallValidatorsMap> {
#[inline]
pub(crate) fn compile<'a>(
map: &'a Map<String, Value>,
schema: &'a Value,
context: &CompilationContext,
) -> CompilationResult<'a> {
let keyword_context = context.with_path("additionalProperties");
Ok(Box::new(AdditionalPropertiesNotEmptyValidator {
properties: compile_small_map(map, context)?,
node: compile_validators(schema, &keyword_context)?,
}))
}
}
impl AdditionalPropertiesNotEmptyValidator<BigValidatorsMap> {
#[inline]
pub(crate) fn compile<'a>(
map: &'a Map<String, Value>,
schema: &'a Value,
context: &CompilationContext,
) -> CompilationResult<'a> {
let keyword_context = context.with_path("additionalProperties");
Ok(Box::new(AdditionalPropertiesNotEmptyValidator {
properties: compile_big_map(map, context)?,
node: compile_validators(schema, &keyword_context)?,
}))
}
}
impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesNotEmptyValidator<M> {
fn is_valid(&self, instance: &Value) -> bool {
if let Value::Object(props) = instance {
are_properties_valid(&self.properties, props, |instance| {
self.node.is_valid(instance)
})
} else {
true
}
}
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
if let Value::Object(map) = instance {
let mut errors = vec![];
for (property, value) in map {
if let Some((name, property_validators)) =
self.properties.get_key_validator(property)
{
errors.extend(validate!(property_validators, value, instance_path, name))
} else {
errors.extend(validate!(self.node, value, instance_path, property))
}
}
Box::new(errors.into_iter())
} else {
no_error()
}
}
fn apply<'a>(
&'a self,
instance: &Value,
instance_path: &JsonPointerNode,
) -> PartialApplication<'a> {
if let Value::Object(map) = instance {
let mut matched_propnames = Vec::with_capacity(map.len());
let mut output = BasicOutput::default();
for (property, value) in map {
let path = instance_path.push(property.as_str());
if let Some((_name, property_validators)) =
self.properties.get_key_validator(property)
{
output += property_validators.apply_rooted(value, &path);
} else {
output += self.node.apply_rooted(value, &path);
matched_propnames.push(property.clone());
}
}
let mut result: PartialApplication = output.into();
if !matched_propnames.is_empty() {
result.annotate(Value::from(matched_propnames).into());
}
result
} else {
PartialApplication::valid_empty()
}
}
}
impl<M: PropertiesValidatorsMap> core::fmt::Display for AdditionalPropertiesNotEmptyValidator<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"additionalProperties: {}",
format_validators(self.node.validators())
)
}
}
pub(crate) struct AdditionalPropertiesWithPatternsValidator {
node: SchemaNode,
patterns: PatternedValidators,
pattern_keyword_path: JSONPointer,
pattern_keyword_absolute_path: Option<AbsolutePath>,
}
impl AdditionalPropertiesWithPatternsValidator {
#[inline]
pub(crate) fn compile<'a>(
schema: &'a Value,
patterns: PatternedValidators,
context: &CompilationContext,
) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesWithPatternsValidator {
node: compile_validators(schema, &context.with_path("additionalProperties"))?,
patterns,
pattern_keyword_path: context.as_pointer_with("patternProperties"),
pattern_keyword_absolute_path: context
.with_path("patternProperties")
.base_uri()
.map(Into::into),
}))
}
}
impl Validate for AdditionalPropertiesWithPatternsValidator {
fn is_valid(&self, instance: &Value) -> bool {
if let Value::Object(item) = instance {
for (property, value) in item {
let mut has_match = false;
for (re, node) in &self.patterns {
if re.is_match(property).unwrap_or(false) {
has_match = true;
is_valid_pattern_schema!(node, value)
}
}
if !has_match && !is_valid!(self.node, value) {
return false;
}
}
}
true
}
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
if let Value::Object(item) = instance {
let mut errors = vec![];
for (property, value) in item {
let mut has_match = false;
errors.extend(
self.patterns
.iter()
.filter(|(re, _)| re.is_match(property).unwrap_or(false))
.flat_map(|(_, node)| {
has_match = true;
validate!(node, value, instance_path, property)
}),
);
if !has_match {
errors.extend(validate!(self.node, value, instance_path, property))
}
}
Box::new(errors.into_iter())
} else {
no_error()
}
}
fn apply<'a>(
&'a self,
instance: &Value,
instance_path: &JsonPointerNode,
) -> PartialApplication<'a> {
if let Value::Object(item) = instance {
let mut output = BasicOutput::default();
let mut pattern_matched_propnames = Vec::with_capacity(item.len());
let mut additional_matched_propnames = Vec::with_capacity(item.len());
for (property, value) in item {
let path = instance_path.push(property.as_str());
let mut has_match = false;
for (pattern, node) in &self.patterns {
if pattern.is_match(property).unwrap_or(false) {
has_match = true;
pattern_matched_propnames.push(property.clone());
output += node.apply_rooted(value, &path)
}
}
if !has_match {
additional_matched_propnames.push(property.clone());
output += self.node.apply_rooted(value, &path)
}
}
if !pattern_matched_propnames.is_empty() {
output += OutputUnit::<Annotations<'_>>::annotations(
self.pattern_keyword_path.clone(),
instance_path.into(),
self.pattern_keyword_absolute_path.clone(),
Value::from(pattern_matched_propnames).into(),
)
.into();
}
let mut result: PartialApplication = output.into();
if !additional_matched_propnames.is_empty() {
result.annotate(Value::from(additional_matched_propnames).into())
}
result
} else {
PartialApplication::valid_empty()
}
}
}
impl core::fmt::Display for AdditionalPropertiesWithPatternsValidator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"additionalProperties: {}",
format_validators(self.node.validators())
)
}
}
pub(crate) struct AdditionalPropertiesWithPatternsFalseValidator {
patterns: PatternedValidators,
schema_path: JSONPointer,
pattern_keyword_path: JSONPointer,
pattern_keyword_absolute_path: Option<AbsolutePath>,
}
impl AdditionalPropertiesWithPatternsFalseValidator {
#[inline]
pub(crate) fn compile<'a>(
patterns: PatternedValidators,
context: &CompilationContext<'_>,
) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesWithPatternsFalseValidator {
patterns,
schema_path: context.as_pointer_with("additionalProperties"),
pattern_keyword_path: context.as_pointer_with("patternProperties"),
pattern_keyword_absolute_path: context
.with_path("patternProperties")
.base_uri()
.map(Into::into),
}))
}
}
impl Validate for AdditionalPropertiesWithPatternsFalseValidator {
fn is_valid(&self, instance: &Value) -> bool {
if let Value::Object(item) = instance {
for (property, value) in item {
is_valid_patterns!(&self.patterns, property, value);
}
}
true
}
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
if let Value::Object(item) = instance {
let mut errors = vec![];
let mut unexpected = vec![];
for (property, value) in item {
let mut has_match = false;
errors.extend(
self.patterns
.iter()
.filter(|(re, _)| re.is_match(property).unwrap_or(false))
.flat_map(|(_, node)| {
has_match = true;
validate!(node, value, instance_path, property)
}),
);
if !has_match {
unexpected.push(property.clone());
}
}
if !unexpected.is_empty() {
errors.push(ValidationError::additional_properties(
self.schema_path.clone(),
instance_path.into(),
instance,
unexpected,
))
}
Box::new(errors.into_iter())
} else {
no_error()
}
}
fn apply<'a>(
&'a self,
instance: &Value,
instance_path: &JsonPointerNode,
) -> PartialApplication<'a> {
if let Value::Object(item) = instance {
let mut output = BasicOutput::default();
let mut unexpected = Vec::with_capacity(item.len());
let mut pattern_matched_props = Vec::with_capacity(item.len());
for (property, value) in item {
let path = instance_path.push(property.as_str());
let mut has_match = false;
for (pattern, node) in &self.patterns {
if pattern.is_match(property).unwrap_or(false) {
has_match = true;
pattern_matched_props.push(property.clone());
output += node.apply_rooted(value, &path);
}
}
if !has_match {
unexpected.push(property.clone());
}
}
if !pattern_matched_props.is_empty() {
output += OutputUnit::<Annotations<'_>>::annotations(
self.pattern_keyword_path.clone(),
instance_path.into(),
self.pattern_keyword_absolute_path.clone(),
Value::from(pattern_matched_props).into(),
)
.into();
}
let mut result: PartialApplication = output.into();
if !unexpected.is_empty() {
result.mark_errored(
ValidationError::additional_properties(
self.schema_path.clone(),
instance_path.into(),
instance,
unexpected,
)
.into(),
);
}
result
} else {
PartialApplication::valid_empty()
}
}
}
impl core::fmt::Display for AdditionalPropertiesWithPatternsFalseValidator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
"additionalProperties: false".fmt(f)
}
}
pub(crate) struct AdditionalPropertiesWithPatternsNotEmptyValidator<M: PropertiesValidatorsMap> {
node: SchemaNode,
properties: M,
patterns: PatternedValidators,
}
impl AdditionalPropertiesWithPatternsNotEmptyValidator<SmallValidatorsMap> {
#[inline]
pub(crate) fn compile<'a>(
map: &'a Map<String, Value>,
schema: &'a Value,
patterns: PatternedValidators,
context: &CompilationContext,
) -> CompilationResult<'a> {
let keyword_context = context.with_path("additionalProperties");
Ok(Box::new(
AdditionalPropertiesWithPatternsNotEmptyValidator {
node: compile_validators(schema, &keyword_context)?,
properties: compile_small_map(map, context)?,
patterns,
},
))
}
}
impl AdditionalPropertiesWithPatternsNotEmptyValidator<BigValidatorsMap> {
#[inline]
pub(crate) fn compile<'a>(
map: &'a Map<String, Value>,
schema: &'a Value,
patterns: PatternedValidators,
context: &CompilationContext,
) -> CompilationResult<'a> {
let keyword_context = context.with_path("additionalProperties");
Ok(Box::new(
AdditionalPropertiesWithPatternsNotEmptyValidator {
node: compile_validators(schema, &keyword_context)?,
properties: compile_big_map(map, context)?,
patterns,
},
))
}
}
impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesWithPatternsNotEmptyValidator<M> {
fn is_valid(&self, instance: &Value) -> bool {
if let Value::Object(item) = instance {
for (property, value) in item {
if let Some(node) = self.properties.get_validator(property) {
if is_valid!(node, value) {
for (re, node) in &self.patterns {
if re.is_match(property).unwrap_or(false) {
is_valid_pattern_schema!(node, value)
}
}
} else {
return false;
}
} else {
let mut has_match = false;
for (re, node) in &self.patterns {
if re.is_match(property).unwrap_or(false) {
has_match = true;
is_valid_pattern_schema!(node, value)
}
}
if !has_match && !is_valid!(self.node, value) {
return false;
}
}
}
true
} else {
true
}
}
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
if let Value::Object(item) = instance {
let mut errors = vec![];
for (property, value) in item {
if let Some((name, node)) = self.properties.get_key_validator(property) {
errors.extend(validate!(node, value, instance_path, name));
errors.extend(
self.patterns
.iter()
.filter(|(re, _)| re.is_match(property).unwrap_or(false))
.flat_map(|(_, node)| validate!(node, value, instance_path, name)),
);
} else {
let mut has_match = false;
errors.extend(
self.patterns
.iter()
.filter(|(re, _)| re.is_match(property).unwrap_or(false))
.flat_map(|(_, node)| {
has_match = true;
validate!(node, value, instance_path, property)
}),
);
if !has_match {
errors.extend(validate!(self.node, value, instance_path, property))
}
}
}
Box::new(errors.into_iter())
} else {
no_error()
}
}
fn apply<'a>(
&'a self,
instance: &Value,
instance_path: &JsonPointerNode,
) -> PartialApplication<'a> {
if let Value::Object(item) = instance {
let mut output = BasicOutput::default();
let mut additional_matches = Vec::with_capacity(item.len());
for (property, value) in item {
let path = instance_path.push(property.as_str());
if let Some((_name, node)) = self.properties.get_key_validator(property) {
output += node.apply_rooted(value, &path);
for (pattern, node) in &self.patterns {
if pattern.is_match(property).unwrap_or(false) {
output += node.apply_rooted(value, &path);
}
}
} else {
let mut has_match = false;
for (pattern, node) in &self.patterns {
if pattern.is_match(property).unwrap_or(false) {
has_match = true;
output += node.apply_rooted(value, &path);
}
}
if !has_match {
additional_matches.push(property.clone());
output += self.node.apply_rooted(value, &path);
}
}
}
let mut result: PartialApplication = output.into();
result.annotate(Value::from(additional_matches).into());
result
} else {
PartialApplication::valid_empty()
}
}
}
impl<M: PropertiesValidatorsMap> core::fmt::Display
for AdditionalPropertiesWithPatternsNotEmptyValidator<M>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"additionalProperties: {}",
format_validators(self.node.validators())
)
}
}
pub(crate) struct AdditionalPropertiesWithPatternsNotEmptyFalseValidator<M: PropertiesValidatorsMap>
{
properties: M,
patterns: PatternedValidators,
schema_path: JSONPointer,
}
impl AdditionalPropertiesWithPatternsNotEmptyFalseValidator<SmallValidatorsMap> {
#[inline]
pub(crate) fn compile<'a>(
map: &'a Map<String, Value>,
patterns: PatternedValidators,
context: &CompilationContext,
) -> CompilationResult<'a> {
Ok(Box::new(
AdditionalPropertiesWithPatternsNotEmptyFalseValidator::<SmallValidatorsMap> {
properties: compile_small_map(map, context)?,
patterns,
schema_path: context.schema_path.push("additionalProperties").into(),
},
))
}
}
impl AdditionalPropertiesWithPatternsNotEmptyFalseValidator<BigValidatorsMap> {
#[inline]
pub(crate) fn compile<'a>(
map: &'a Map<String, Value>,
patterns: PatternedValidators,
context: &CompilationContext,
) -> CompilationResult<'a> {
Ok(Box::new(
AdditionalPropertiesWithPatternsNotEmptyFalseValidator {
properties: compile_big_map(map, context)?,
patterns,
schema_path: context.schema_path.push("additionalProperties").into(),
},
))
}
}
impl<M: PropertiesValidatorsMap> Validate
for AdditionalPropertiesWithPatternsNotEmptyFalseValidator<M>
{
fn is_valid(&self, instance: &Value) -> bool {
if let Value::Object(item) = instance {
for (property, value) in item {
if let Some(node) = self.properties.get_validator(property) {
if is_valid!(node, value) {
for (re, node) in &self.patterns {
if re.is_match(property).unwrap_or(false) {
is_valid_pattern_schema!(node, value)
}
}
} else {
return false;
}
} else {
is_valid_patterns!(&self.patterns, property, value);
}
}
}
true
}
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
if let Value::Object(item) = instance {
let mut errors = vec![];
let mut unexpected = vec![];
for (property, value) in item {
if let Some((name, node)) = self.properties.get_key_validator(property) {
errors.extend(validate!(node, value, instance_path, name));
errors.extend(
self.patterns
.iter()
.filter(|(re, _)| re.is_match(property).unwrap_or(false))
.flat_map(|(_, node)| validate!(node, value, instance_path, name)),
);
} else {
let mut has_match = false;
errors.extend(
self.patterns
.iter()
.filter(|(re, _)| re.is_match(property).unwrap_or(false))
.flat_map(|(_, node)| {
has_match = true;
validate!(node, value, instance_path, property)
}),
);
if !has_match {
unexpected.push(property.clone());
}
}
}
if !unexpected.is_empty() {
errors.push(ValidationError::additional_properties(
self.schema_path.clone(),
instance_path.into(),
instance,
unexpected,
))
}
Box::new(errors.into_iter())
} else {
no_error()
}
}
fn apply<'a>(
&'a self,
instance: &Value,
instance_path: &JsonPointerNode,
) -> PartialApplication<'a> {
if let Value::Object(item) = instance {
let mut output = BasicOutput::default();
let mut unexpected = vec![];
for (property, value) in item {
let path = instance_path.push(property.as_str());
if let Some((_name, node)) = self.properties.get_key_validator(property) {
output += node.apply_rooted(value, &path);
for (pattern, node) in &self.patterns {
if pattern.is_match(property).unwrap_or(false) {
output += node.apply_rooted(value, &path);
}
}
} else {
let mut has_match = false;
for (pattern, node) in &self.patterns {
if pattern.is_match(property).unwrap_or(false) {
has_match = true;
output += node.apply_rooted(value, &path);
}
}
if !has_match {
unexpected.push(property.clone());
}
}
}
let mut result: PartialApplication = output.into();
if !unexpected.is_empty() {
result.mark_errored(
ValidationError::additional_properties(
self.schema_path.clone(),
instance_path.into(),
instance,
unexpected,
)
.into(),
)
}
result
} else {
PartialApplication::valid_empty()
}
}
}
impl<M: PropertiesValidatorsMap> core::fmt::Display
for AdditionalPropertiesWithPatternsNotEmptyFalseValidator<M>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
"additionalProperties: false".fmt(f)
}
}
#[inline]
pub(crate) fn compile<'a>(
parent: &'a Map<String, Value>,
schema: &'a Value,
context: &CompilationContext,
) -> Option<CompilationResult<'a>> {
let properties = parent.get("properties");
if let Some(patterns) = parent.get("patternProperties") {
if let Value::Object(obj) = patterns {
if let Ok(compiled_patterns) = compile_patterns(obj, context) {
match schema {
Value::Bool(true) => None, Value::Bool(false) => {
if let Some(properties) = properties {
compile_dynamic_prop_map_validator!(
AdditionalPropertiesWithPatternsNotEmptyFalseValidator,
properties,
compiled_patterns,
context
)
} else {
Some(AdditionalPropertiesWithPatternsFalseValidator::compile(
compiled_patterns,
context,
))
}
}
_ => {
if let Some(properties) = properties {
compile_dynamic_prop_map_validator!(
AdditionalPropertiesWithPatternsNotEmptyValidator,
properties,
schema,
compiled_patterns,
context
)
} else {
Some(AdditionalPropertiesWithPatternsValidator::compile(
schema,
compiled_patterns,
context,
))
}
}
}
} else {
Some(Err(ValidationError::null_schema()))
}
} else {
Some(Err(ValidationError::null_schema()))
}
} else {
match schema {
Value::Bool(true) => None, Value::Bool(false) => {
if let Some(properties) = properties {
compile_dynamic_prop_map_validator!(
AdditionalPropertiesNotEmptyFalseValidator,
properties,
context
)
} else {
let schema_path = context.as_pointer_with("additionalProperties");
Some(AdditionalPropertiesFalseValidator::compile(schema_path))
}
}
_ => {
if let Some(properties) = properties {
compile_dynamic_prop_map_validator!(
AdditionalPropertiesNotEmptyValidator,
properties,
schema,
context
)
} else {
Some(AdditionalPropertiesValidator::compile(schema, context))
}
}
}
}
}
#[cfg(test)]
mod tests {
use crate::tests_util;
use serde_json::{json, Value};
use test_case::test_case;
fn schema_1() -> Value {
json!({
"additionalProperties": false,
"properties": {
"foo": {"type": "string"},
"barbaz": {"type": "integer", "multipleOf": 3},
},
"patternProperties": {
"^bar": {"type": "integer", "minimum": 5},
"spam$": {"type": "integer", "maximum": 10},
}
})
}
#[test_case(&json!([1]))]
#[test_case(&json!({}))]
#[test_case(&json!({"foo": "a"}))]
#[test_case(&json!({"barbaz": 6}))]
#[test_case(&json!({"bar": 6}))]
#[test_case(&json!({"spam": 7}))]
#[test_case(&json!({"bar": 6, "spam": 7}))]
#[test_case(&json!({"barspam": 7}))]
#[test_case(&json!({"barspam": 7, "bar": 6, "spam": 7, "foo": "a", "barbaz": 6}))]
fn schema_1_valid(instance: &Value) {
let schema = schema_1();
tests_util::is_valid(&schema, instance)
}
#[test_case(&json!({"foo": 3}), &["3 is not of type \"string\""], &["/properties/foo/type"])]
#[test_case(&json!({"faz": 1}), &["Additional properties are not allowed (\'faz\' was unexpected)"], &["/additionalProperties"])]
#[test_case(&json!({"faz": 1, "haz": 1}), &["Additional properties are not allowed (\'faz\', \'haz\' were unexpected)"], &["/additionalProperties"])]
#[test_case(&json!({"foo": 3, "bar": 4}), &["4 is less than the minimum of 5", "3 is not of type \"string\""], &["/patternProperties/^bar/minimum", "/properties/foo/type"])]
#[test_case(&json!({"barbaz": 3}), &["3 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"bar": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 4, "spam": 11}), &["4 is less than the minimum of 5", "11 is greater than the maximum of 10"], &["/patternProperties/^bar/minimum", "/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 6, "spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 4, "spam": 8}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"barspam": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"barspam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(
&json!({"bar": 4, "spam": 11, "foo": 3, "faz": 1}),
&[
"4 is less than the minimum of 5",
"3 is not of type \"string\"",
"11 is greater than the maximum of 10",
"Additional properties are not allowed (\'faz\' was unexpected)"
],
&[
"/patternProperties/^bar/minimum",
"/properties/foo/type",
"/patternProperties/spam$/maximum",
"/additionalProperties"
]
)]
fn schema_1_invalid(instance: &Value, expected: &[&str], schema_paths: &[&str]) {
let schema = schema_1();
tests_util::is_not_valid(&schema, instance);
tests_util::expect_errors(&schema, instance, expected);
tests_util::assert_schema_paths(&schema, instance, schema_paths)
}
fn schema_2() -> Value {
json!({
"additionalProperties": false,
"patternProperties": {
"^bar": {"type": "integer", "minimum": 5},
"spam$": {"type": "integer", "maximum": 10},
}
})
}
#[test_case(&json!([1]))]
#[test_case(&json!({}))]
#[test_case(&json!({"bar": 6}))]
#[test_case(&json!({"spam": 7}))]
#[test_case(&json!({"bar": 6, "spam": 7}))]
#[test_case(&json!({"barspam": 7}))]
#[test_case(&json!({"barspam": 7, "bar": 6, "spam": 7}))]
fn schema_2_valid(instance: &Value) {
let schema = schema_2();
tests_util::is_valid(&schema, instance)
}
#[test_case(&json!({"faz": "a"}), &["Additional properties are not allowed (\'faz\' was unexpected)"], &["/additionalProperties"])]
#[test_case(&json!({"bar": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 4, "spam": 11}), &["4 is less than the minimum of 5", "11 is greater than the maximum of 10"], &["/patternProperties/^bar/minimum", "/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 6, "spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 4, "spam": 8}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"barspam": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"barspam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(
&json!({"bar": 4, "spam": 11, "faz": 1}),
&[
"4 is less than the minimum of 5",
"11 is greater than the maximum of 10",
"Additional properties are not allowed (\'faz\' was unexpected)"
],
&[
"/patternProperties/^bar/minimum",
"/patternProperties/spam$/maximum",
"/additionalProperties"
]
)]
fn schema_2_invalid(instance: &Value, expected: &[&str], schema_paths: &[&str]) {
let schema = schema_2();
tests_util::is_not_valid(&schema, instance);
tests_util::expect_errors(&schema, instance, expected);
tests_util::assert_schema_paths(&schema, instance, schema_paths)
}
fn schema_3() -> Value {
json!({
"additionalProperties": false,
"properties": {
"foo": {"type": "string"}
}
})
}
#[test_case(&json!([1]))]
#[test_case(&json!({}))]
#[test_case(&json!({"foo": "a"}))]
fn schema_3_valid(instance: &Value) {
let schema = schema_3();
tests_util::is_valid(&schema, instance)
}
#[test_case(&json!({"foo": 3}), &["3 is not of type \"string\""], &["/properties/foo/type"])]
#[test_case(&json!({"faz": "a"}), &["Additional properties are not allowed (\'faz\' was unexpected)"], &["/additionalProperties"])]
#[test_case(
&json!(
{"foo": 3, "faz": "a"}),
&[
"3 is not of type \"string\"",
"Additional properties are not allowed (\'faz\' was unexpected)",
],
&[
"/properties/foo/type",
"/additionalProperties",
]
)]
fn schema_3_invalid(instance: &Value, expected: &[&str], schema_paths: &[&str]) {
let schema = schema_3();
tests_util::is_not_valid(&schema, instance);
tests_util::expect_errors(&schema, instance, expected);
tests_util::assert_schema_paths(&schema, instance, schema_paths)
}
fn schema_4() -> Value {
json!({
"additionalProperties": {"type": "integer"},
"properties": {
"foo": {"type": "string"}
}
})
}
#[test_case(&json!([1]))]
#[test_case(&json!({}))]
#[test_case(&json!({"foo": "a"}))]
#[test_case(&json!({"bar": 4}))]
#[test_case(&json!({"foo": "a", "bar": 4}))]
fn schema_4_valid(instance: &Value) {
let schema = schema_4();
tests_util::is_valid(&schema, instance)
}
#[test_case(&json!({"foo": 3}), &["3 is not of type \"string\""], &["/properties/foo/type"])]
#[test_case(&json!({"bar": "a"}), &["\"a\" is not of type \"integer\""], &["/additionalProperties/type"])]
#[test_case(
&json!(
{"foo": 3, "bar": "a"}),
&[
"\"a\" is not of type \"integer\"",
"3 is not of type \"string\""
],
&[
"/additionalProperties/type",
"/properties/foo/type",
]
)]
fn schema_4_invalid(instance: &Value, expected: &[&str], schema_paths: &[&str]) {
let schema = schema_4();
tests_util::is_not_valid(&schema, instance);
tests_util::expect_errors(&schema, instance, expected);
tests_util::assert_schema_paths(&schema, instance, schema_paths)
}
fn schema_5() -> Value {
json!({
"additionalProperties": {"type": "integer"},
"properties": {
"foo": {"type": "string"},
"barbaz": {"type": "integer", "multipleOf": 3},
},
"patternProperties": {
"^bar": {"type": "integer", "minimum": 5},
"spam$": {"type": "integer", "maximum": 10},
}
})
}
#[test_case(&json!([1]))]
#[test_case(&json!({}))]
#[test_case(&json!({"foo": "a"}))]
#[test_case(&json!({"faz": 42}))]
#[test_case(&json!({"barbaz": 6}))]
#[test_case(&json!({"bar": 6}))]
#[test_case(&json!({"spam": 7}))]
#[test_case(&json!({"bar": 6, "spam": 7}))]
#[test_case(&json!({"barspam": 7}))]
#[test_case(&json!({"barspam": 7, "bar": 6, "spam": 7, "foo": "a", "barbaz": 6, "faz": 42}))]
fn schema_5_valid(instance: &Value) {
let schema = schema_5();
tests_util::is_valid(&schema, instance)
}
#[test_case(&json!({"foo": 3}), &["3 is not of type \"string\""], &["/properties/foo/type"])]
#[test_case(&json!({"faz": "a"}), &["\"a\" is not of type \"integer\""], &["/additionalProperties/type"])]
#[test_case(&json!({"faz": "a", "haz": "a"}), &["\"a\" is not of type \"integer\"", "\"a\" is not of type \"integer\""], &["/additionalProperties/type", "/additionalProperties/type"])]
#[test_case(&json!({"foo": 3, "bar": 4}), &["4 is less than the minimum of 5", "3 is not of type \"string\""], &["/patternProperties/^bar/minimum", "/properties/foo/type"])]
#[test_case(&json!({"barbaz": 3}), &["3 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"bar": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 4, "spam": 11}), &["4 is less than the minimum of 5", "11 is greater than the maximum of 10"], &["/patternProperties/^bar/minimum", "/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 6, "spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 4, "spam": 8}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"barspam": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"barspam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(
&json!({"bar": 4, "spam": 11, "foo": 3, "faz": "a", "fam": 42}),
&[
"4 is less than the minimum of 5",
"\"a\" is not of type \"integer\"",
"3 is not of type \"string\"",
"11 is greater than the maximum of 10",
],
&[
"/patternProperties/^bar/minimum",
"/additionalProperties/type",
"/properties/foo/type",
"/patternProperties/spam$/maximum",
]
)]
fn schema_5_invalid(instance: &Value, expected: &[&str], schema_paths: &[&str]) {
let schema = schema_5();
tests_util::is_not_valid(&schema, instance);
tests_util::expect_errors(&schema, instance, expected);
tests_util::assert_schema_paths(&schema, instance, schema_paths)
}
fn schema_6() -> Value {
json!({
"additionalProperties": {"type": "integer"},
"patternProperties": {
"^bar": {"type": "integer", "minimum": 5},
"spam$": {"type": "integer", "maximum": 10},
}
})
}
#[test_case(&json!([1]))]
#[test_case(&json!({}))]
#[test_case(&json!({"faz": 42}))]
#[test_case(&json!({"bar": 6}))]
#[test_case(&json!({"spam": 7}))]
#[test_case(&json!({"bar": 6, "spam": 7}))]
#[test_case(&json!({"barspam": 7}))]
#[test_case(&json!({"barspam": 7, "bar": 6, "spam": 7, "faz": 42}))]
fn schema_6_valid(instance: &Value) {
let schema = schema_6();
tests_util::is_valid(&schema, instance)
}
#[test_case(&json!({"faz": "a"}), &["\"a\" is not of type \"integer\""], &["/additionalProperties/type"])]
#[test_case(&json!({"faz": "a", "haz": "a"}), &["\"a\" is not of type \"integer\"", "\"a\" is not of type \"integer\""], &["/additionalProperties/type", "/additionalProperties/type"])]
#[test_case(&json!({"foo": "a", "bar": 4}), &["4 is less than the minimum of 5", "\"a\" is not of type \"integer\""], &["/patternProperties/^bar/minimum", "/additionalProperties/type"])]
#[test_case(&json!({"bar": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 4, "spam": 11}), &["4 is less than the minimum of 5", "11 is greater than the maximum of 10"], &["/patternProperties/^bar/minimum", "/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 6, "spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(&json!({"bar": 4, "spam": 8}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"barspam": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])]
#[test_case(&json!({"barspam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])]
#[test_case(
&json!({"bar": 4, "spam": 11, "faz": "a", "fam": 42}),
&[
"4 is less than the minimum of 5",
"\"a\" is not of type \"integer\"",
"11 is greater than the maximum of 10",
],
&[
"/patternProperties/^bar/minimum",
"/additionalProperties/type",
"/patternProperties/spam$/maximum",
]
)]
fn schema_6_invalid(instance: &Value, expected: &[&str], schema_paths: &[&str]) {
let schema = schema_6();
tests_util::is_not_valid(&schema, instance);
tests_util::expect_errors(&schema, instance, expected);
tests_util::assert_schema_paths(&schema, instance, schema_paths)
}
}