Skip to content

Commit

Permalink
Fix min/max validation and add link type number validation
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Dec 9, 2024
1 parent cd42218 commit 7565585
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{Valid, Validator, schema::PropertyValueArray};
#[derive(Debug, Error)]
pub enum ArraySchemaValidationError {
#[error(
"Unsatisfiable item number constraints, expected minimum of {min} and maximum of {max} \
items"
"Unsatisfiable item number constraints, expected minimum amount of items ({min}) to be \
less than or equal to the maximum amount of items ({max})"
)]
IncompatibleItemNumberConstraints { min: usize, max: usize },
}
Expand All @@ -21,7 +21,11 @@ impl<T: Sync> Validator<PropertyValueArray<T>> for ArraySchemaValidator {
value: &'v PropertyValueArray<T>,
) -> Result<&'v Valid<PropertyValueArray<T>>, Self::Error> {
if let Some((min, max)) = value.min_items.zip(value.max_items) {
return Err(ArraySchemaValidationError::IncompatibleItemNumberConstraints { min, max });
if min > max {
return Err(
ArraySchemaValidationError::IncompatibleItemNumberConstraints { min, max },
);
}
}

Ok(Valid::new_ref_unchecked(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub enum EntityTypeValidationError {
#[display("Property object validation failed: {_0}")]
#[from]
ObjectValidationFailed(ObjectSchemaValidationError),
#[display(
"Unsatisfiable link number constraints, expected minimum amount of items ({min}) to be \
less than or equal to the maximum amount of items ({max})"
)]
IncompatibleLinkNumberConstraints { min: usize, max: usize },
}

#[derive(Debug)]
Expand All @@ -42,6 +47,16 @@ impl Validator<EntityType> for EntityTypeValidator {
}
}

for links in value.constraints.links.values() {
if let Some((min, max)) = links.min_items.zip(links.max_items) {
if min > max {
return Err(
EntityTypeValidationError::IncompatibleLinkNumberConstraints { min, max },
);
}
}
}

Ok(Valid::new_ref_unchecked(value))
}
}
Expand All @@ -68,6 +83,16 @@ impl Validator<ClosedEntityType> for EntityTypeValidator {
}
}

for links in value.constraints.links.values() {
if let Some((min, max)) = links.min_items.zip(links.max_items) {
if min > max {
return Err(
EntityTypeValidationError::IncompatibleLinkNumberConstraints { min, max },
);
}
}
}

Ok(Valid::new_ref_unchecked(value))
}
}

0 comments on commit 7565585

Please sign in to comment.