-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
a7f3936
commit 43abc52
Showing
8 changed files
with
81 additions
and
77 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
33 changes: 33 additions & 0 deletions
33
libs/@blockprotocol/type-system/rust/src/schema/array/validation.rs
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,33 @@ | ||
use thiserror::Error; | ||
|
||
use crate::{Valid, Validator, schema::PropertyValueArray}; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum ArraySchemaValidationError { | ||
#[error( | ||
"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 }, | ||
} | ||
|
||
pub struct ArraySchemaValidator; | ||
|
||
impl<T: Sync> Validator<PropertyValueArray<T>> for ArraySchemaValidator { | ||
type Error = ArraySchemaValidationError; | ||
|
||
fn validate_ref<'v>( | ||
&self, | ||
value: &'v PropertyValueArray<T>, | ||
) -> Result<&'v Valid<PropertyValueArray<T>>, Self::Error> { | ||
if let Some((min, max)) = value.min_items.zip(value.max_items) { | ||
if min > max { | ||
return Err( | ||
ArraySchemaValidationError::IncompatibleItemNumberConstraints { min, max }, | ||
); | ||
} | ||
} | ||
|
||
Ok(Valid::new_ref_unchecked(value)) | ||
} | ||
} |
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
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