-
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.
H-3337: Align type-system data-type constraints with Node API (Part I…
…I) (#5239)
- Loading branch information
1 parent
9fdaa3d
commit 89606b9
Showing
33 changed files
with
1,557 additions
and
662 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
49 changes: 49 additions & 0 deletions
49
libs/@blockprotocol/type-system/rust/src/schema/data_type/constraint/any_of.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,49 @@ | ||
use error_stack::{Report, ReportSink, ResultExt}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value as JsonValue; | ||
|
||
use crate::schema::{ConstraintError, ValueLabel, data_type::constraint::SimpleTypedValueSchema}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))] | ||
#[serde(rename_all = "camelCase", deny_unknown_fields)] | ||
pub struct AnyOfConstraints { | ||
#[cfg_attr( | ||
target_arch = "wasm32", | ||
tsify(type = "[SimpleTypedValueSchema, ...SimpleTypedValueSchema[]]") | ||
)] | ||
pub any_of: Vec<SimpleTypedValueSchema>, | ||
} | ||
|
||
impl AnyOfConstraints { | ||
/// Checks if the provided value is valid against any of the schemas in the `any_of` list. | ||
/// | ||
/// # Errors | ||
/// | ||
/// - [`AnyOf`] if the value is not valid against any of the schemas. | ||
/// | ||
/// [`AnyOf`]: ConstraintError::AnyOf | ||
pub fn validate_value(&self, value: &JsonValue) -> Result<(), Report<ConstraintError>> { | ||
let mut status = ReportSink::<ConstraintError>::new(); | ||
for schema in &self.any_of { | ||
if let Err(error) = schema.constraints.validate_value(value) { | ||
status.capture(error); | ||
} else { | ||
return Ok(()); | ||
} | ||
} | ||
status.finish().change_context(ConstraintError::AnyOf) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))] | ||
#[serde(rename_all = "camelCase", deny_unknown_fields)] | ||
pub struct AnyOfSchema { | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub description: Option<String>, | ||
#[serde(default, skip_serializing_if = "ValueLabel::is_empty")] | ||
pub label: ValueLabel, | ||
#[serde(flatten)] | ||
pub constraints: AnyOfConstraints, | ||
} |
Oops, something went wrong.