Skip to content

Commit

Permalink
Implement folding for number constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Oct 14, 2024
1 parent d43ce6b commit 161a08a
Show file tree
Hide file tree
Showing 3 changed files with 966 additions and 48 deletions.
40 changes: 15 additions & 25 deletions libs/@blockprotocol/type-system/rust/src/schema/data_type/closed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,7 @@ mod tests {
assert_eq!(integer.r#abstract, defs.integer.r#abstract);
assert_eq!(
json!(integer.all_of),
json!([
defs.integer.constraints,
defs.number.constraints,
defs.value.constraints
])
json!([defs.integer.constraints, defs.value.constraints])
);
}

Expand All @@ -475,11 +471,7 @@ mod tests {
assert_eq!(unsigned.r#abstract, defs.unsigned.r#abstract);
assert_eq!(
json!(unsigned.all_of),
json!([
defs.unsigned.constraints,
defs.number.constraints,
defs.value.constraints
])
json!([defs.unsigned.constraints, defs.value.constraints])
);
}

Expand All @@ -499,10 +491,12 @@ mod tests {
assert_eq!(
json!(unsigned_int.all_of),
json!([
defs.unsigned_int.constraints,
defs.unsigned.constraints,
defs.integer.constraints,
defs.number.constraints,
{
"type": "number",
"minimum": 0.0,
"maximum": 4_294_967_295.0,
"multipleOf": 1.0,
},
defs.value.constraints
])
);
Expand All @@ -523,11 +517,7 @@ mod tests {
assert_eq!(small.r#abstract, defs.small.r#abstract);
assert_eq!(
json!(small.all_of),
json!([
defs.small.constraints,
defs.number.constraints,
defs.value.constraints
])
json!([defs.small.constraints, defs.value.constraints])
);
}

Expand Down Expand Up @@ -556,12 +546,12 @@ mod tests {
assert_eq!(
json!(unsigned_small_int.all_of),
json!([
defs.unsigned_small_int.constraints,
defs.small.constraints,
defs.unsigned_int.constraints,
defs.unsigned.constraints,
defs.integer.constraints,
defs.number.constraints,
{
"type": "number",
"minimum": 0.0,
"maximum": 100.0,
"multipleOf": 1.0,
},
defs.value.constraints
])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,13 @@ mod tests {
use core::fmt::Display;
use std::collections::HashSet;

use error_stack::Frame;
use serde_json::Value as JsonValue;
use error_stack::{Frame, Report};
use serde_json::{Value as JsonValue, json};

use crate::schema::data_type::constraint::{ConstraintValidator, ValueConstraints};
use crate::schema::data_type::{
closed::ResolveClosedDataTypeError,
constraint::{ConstraintValidator, ValueConstraints},
};

pub(crate) fn read_schema(schema: &JsonValue) -> ValueConstraints {
let parsed = serde_json::from_value(schema.clone()).expect("Failed to parse schema");
Expand Down Expand Up @@ -313,4 +316,52 @@ mod tests {

assert_eq!(errors, actual_errors);
}

pub(crate) fn intersect_schemas(
schemas: impl IntoIterator<Item = JsonValue>,
) -> Result<Vec<JsonValue>, Report<ResolveClosedDataTypeError>> {
let schemas = schemas
.into_iter()
.map(|schema| serde_json::from_value(schema).expect("Failed to parse schema"))
.collect::<Vec<_>>();
ValueConstraints::fold_intersections(schemas).map(|schemas| {
schemas
.into_iter()
.map(|schema| serde_json::to_value(schema).expect("Failed to serialize schema"))
.collect()
})
}

pub(crate) fn check_schema_intersection(
schemas: impl IntoIterator<Item = JsonValue>,
expected: impl IntoIterator<Item = JsonValue>,
) {
let intersection = intersect_schemas(schemas).expect("Failed to intersect schemas");
let expected = expected.into_iter().collect::<Vec<_>>();
assert_eq!(
intersection,
expected,
"Schemas do not match: expected: {:#}, actual: {:#}",
json!(intersection),
json!(expected)
);
}

pub(crate) fn check_schema_intersection_error<E: Display + Send + Sync + 'static>(
schemas: impl IntoIterator<Item = JsonValue>,
expected_errors: impl IntoIterator<Item = E>,
) {
let err = intersect_schemas(schemas).expect_err("Intersected invalid schemas");
let errors = expected_errors
.into_iter()
.map(|error| error.to_string())
.collect::<HashSet<_>>();
let actual_errors = err
.frames()
.filter_map(Frame::downcast_ref::<E>)
.map(ToString::to_string)
.collect::<HashSet<_>>();

assert_eq!(errors, actual_errors);
}
}
Loading

0 comments on commit 161a08a

Please sign in to comment.