-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93b393d
commit 49e312d
Showing
28 changed files
with
1,035 additions
and
95 deletions.
There are no files selected for viewing
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
94 changes: 94 additions & 0 deletions
94
engine/baml-lib/baml-core/src/validate/validation_pipeline/validations/tests.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,94 @@ | ||
use baml_types::{Constraint, ConstraintLevel}; | ||
use internal_baml_diagnostics::{DatamodelError, DatamodelWarning, Span}; | ||
use internal_baml_jinja_types::{validate_expression, JinjaContext, PredefinedTypes, Type}; | ||
|
||
use crate::validate::validation_pipeline::context::Context; | ||
|
||
pub(super) fn validate(ctx: &mut Context<'_>) { | ||
let tests = ctx.db.walk_test_cases().collect::<Vec<_>>(); | ||
tests.iter().for_each(|walker| { | ||
let constraints = &walker.test_case().constraints; | ||
let args = &walker.test_case().args; | ||
let mut check_names: Vec<String> = Vec::new(); | ||
for ( | ||
Constraint { | ||
label, | ||
level, | ||
expression, | ||
}, | ||
constraint_span, | ||
expr_span, | ||
) in constraints.iter() | ||
{ | ||
let mut defined_types = PredefinedTypes::default(JinjaContext::Parsing); | ||
defined_types.add_variable("this", Type::Unknown); | ||
defined_types.add_class( | ||
"Checks", | ||
check_names | ||
.iter() | ||
.map(|check_name| (check_name.clone(), Type::Unknown)) | ||
.collect(), | ||
); | ||
defined_types.add_class( | ||
"_", | ||
vec![ | ||
("checks".to_string(), Type::ClassRef("Checks".to_string())), | ||
("result".to_string(), Type::Unknown), | ||
("latency_ms".to_string(), Type::Number), | ||
] | ||
.into_iter() | ||
.collect(), | ||
); | ||
defined_types.add_variable("_", Type::ClassRef("_".to_string())); | ||
args.keys() | ||
.for_each(|arg_name| defined_types.add_variable(arg_name, Type::Unknown)); | ||
match (level, label) { | ||
(ConstraintLevel::Check, Some(check_name)) => { | ||
check_names.push(check_name.to_string()); | ||
} | ||
_ => {} | ||
} | ||
match validate_expression(expression.0.as_str(), &mut defined_types) { | ||
Ok(_) => {} | ||
Err(e) => { | ||
if let Some(e) = e.parsing_errors { | ||
let range = match e.range() { | ||
Some(range) => range, | ||
None => { | ||
ctx.push_error(DatamodelError::new_validation_error( | ||
&format!("Error parsing jinja template: {}", e), | ||
expr_span.clone(), | ||
)); | ||
continue; | ||
} | ||
}; | ||
|
||
let start_offset = expr_span.start + range.start; | ||
let end_offset = expr_span.start + range.end; | ||
|
||
let span = Span::new( | ||
expr_span.file.clone(), | ||
start_offset as usize, | ||
end_offset as usize, | ||
); | ||
|
||
ctx.push_error(DatamodelError::new_validation_error( | ||
&format!("Error parsing jinja template: {}", e), | ||
span, | ||
)) | ||
} else { | ||
e.errors.iter().for_each(|t| { | ||
let tspan = t.span(); | ||
let span = Span::new( | ||
expr_span.file.clone(), | ||
expr_span.start + tspan.start_offset as usize, | ||
expr_span.start + tspan.end_offset as usize, | ||
); | ||
ctx.push_warning(DatamodelWarning::new(t.message().to_string(), span)) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} |
Oops, something went wrong.