-
Notifications
You must be signed in to change notification settings - Fork 62
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
Showing
13 changed files
with
167 additions
and
239 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
2 changes: 1 addition & 1 deletion
2
engine/baml-lib/baml-core/src/validate/validation_pipeline/validations.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
6 changes: 3 additions & 3 deletions
6
engine/baml-lib/baml-core/src/validate/validation_pipeline/validations/classes.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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
use crate::validate::validation_pipeline::context::Context; | ||
|
||
use super::common::validate_type_exists; | ||
use super::types::validate_type; | ||
|
||
pub(super) fn validate(ctx: &mut Context<'_>) { | ||
for cls in ctx.db.walk_classes() { | ||
let _ast_class = cls.ast_class(); | ||
|
||
for c in cls.static_fields() { | ||
let field = c.ast_field(); | ||
validate_type_exists(ctx, &field.field_type); | ||
validate_type(ctx, &field.field_type); | ||
} | ||
for c in cls.dynamic_fields() { | ||
let field = c.ast_field(); | ||
validate_type_exists(ctx, &field.field_type); | ||
validate_type(ctx, &field.field_type); | ||
} | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
engine/baml-lib/baml-core/src/validate/validation_pipeline/validations/common.rs
This file was deleted.
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
66 changes: 66 additions & 0 deletions
66
engine/baml-lib/baml-core/src/validate/validation_pipeline/validations/types.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,66 @@ | ||
use baml_types::TypeValue; | ||
use internal_baml_diagnostics::DatamodelError; | ||
use internal_baml_schema_ast::ast::{FieldArity, FieldType, Identifier, WithName, WithSpan}; | ||
|
||
use crate::validate::validation_pipeline::context::Context; | ||
|
||
fn errors_with_names<'a>(ctx: &'a mut Context<'_>, idn: &Identifier) { | ||
// Push the error with the appropriate message | ||
ctx.push_error(DatamodelError::new_type_not_found_error( | ||
idn.name(), | ||
ctx.db.valid_type_names(), | ||
idn.span().clone(), | ||
)); | ||
} | ||
|
||
/// Called for each type in the baml_src tree, validates that it is well-formed. | ||
/// | ||
/// Operates in two passes: | ||
/// | ||
/// 1. Verify that the type is resolveable (for REF types) | ||
/// 2. Verify that the type is well-formed/allowed in the language | ||
pub(crate) fn validate_type(ctx: &mut Context<'_>, field_type: &FieldType) { | ||
validate_type_exists(ctx, field_type); | ||
validate_type_allowed(ctx, field_type); | ||
} | ||
|
||
fn validate_type_exists(ctx: &mut Context<'_>, field_type: &FieldType) { | ||
field_type | ||
.flat_idns() | ||
.iter() | ||
.for_each(|f| match ctx.db.find_type(f) { | ||
Some(_) => {} | ||
None => match f { | ||
Identifier::Primitive(..) => {} | ||
_ => errors_with_names(ctx, f), | ||
}, | ||
}); | ||
} | ||
|
||
fn validate_type_allowed(ctx: &mut Context<'_>, field_type: &FieldType) { | ||
match field_type { | ||
FieldType::Map(kv_types, _) => { | ||
match &kv_types.0 { | ||
FieldType::Identifier( | ||
FieldArity::Required, | ||
Identifier::Primitive(TypeValue::String, _), | ||
) => {} | ||
key_type => { | ||
ctx.push_error(DatamodelError::new_validation_error( | ||
"Maps may only have strings as keys", | ||
key_type.span().clone(), | ||
)); | ||
} | ||
} | ||
validate_type_allowed(ctx, &kv_types.1); | ||
// TODO:assert key_type is string or int or null | ||
} | ||
FieldType::Identifier(_, _) => {} | ||
FieldType::List(field_type, _, _) => validate_type_allowed(ctx, field_type), | ||
FieldType::Tuple(_, field_types, _) | FieldType::Union(_, field_types, _) => { | ||
for field_type in field_types { | ||
validate_type_allowed(ctx, field_type); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.