Skip to content

Commit 28b092e

Browse files
authored
fix: add constraint flags and don't return only if checks fail, also validate asserts (#2233)
<!-- ELLIPSIS_HIDDEN --> > [!IMPORTANT] > Modify `try_cast` to not return `None` on failing checks, add tests for constraints across types. > > - **Behavior**: > - Modify `try_cast` in `field_type.rs` to not return `None` if only check constraints fail, but still return `None` if assert constraints fail. > - Add flags for constraint results when checks fail. > - **Tests**: > - Add tests in `test_constraints.rs` to verify behavior for failing checks and asserts across various types, including primitives, arrays, maps, and classes. > - Include tests for edge cases like null values and unsupported types (tuples and arrows). > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for 5bcbcbe. You can [customize](https://app.ellipsis.dev/BoundaryML/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent bf90892 commit 28b092e

2 files changed

Lines changed: 790 additions & 4 deletions

File tree

‎engine/baml-lib/jsonish/src/deserializer/coercer/field_type.rs‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ impl TypeCoercer for TypeIR {
4444
};
4545

4646
match result {
47-
Some(v) => {
47+
Some(mut v) => {
4848
// run user checks
49-
let Ok(constrainted_results) =
49+
let Ok(constrained_results) =
5050
run_user_checks(&v.clone().into(), self).map_err(|e| ParsingError {
5151
reason: format!("Failed to evaluate constraints: {e:?}"),
5252
scope: ctx.scope.clone(),
@@ -55,9 +55,27 @@ impl TypeCoercer for TypeIR {
5555
else {
5656
return None;
5757
};
58-
let Ok(()) = validate_asserts(&constrainted_results) else {
58+
59+
// Don't return None if only checks fail (not asserts)
60+
let check_results = constrained_results
61+
.iter()
62+
.filter_map(|(maybe_check, result)| {
63+
maybe_check
64+
.clone()
65+
.as_check()
66+
.map(|(label, expr)| (label, expr, *result))
67+
})
68+
.collect::<Vec<_>>();
69+
70+
if !check_results.is_empty() {
71+
v.add_flag(Flag::ConstraintResults(check_results));
72+
}
73+
74+
// Only validate asserts - if they fail, then return None
75+
if validate_asserts(&constrained_results).is_err() {
5976
return None;
60-
};
77+
}
78+
6179
Some(v)
6280
}
6381
None => None,

0 commit comments

Comments
 (0)