Engine: one contract check for a call, used by the preflight, the bridge and the app (#9339)
`schema::issues(id, params)` looks up the command's typed contract and lists what is wrong with the parameters (none without a contract or for an unknown id). The batch preflight, the MCP bridge's pre-send check and the app's check of a design-switching command each did that lookup themselves; they now call it. No behaviour change. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -285,6 +285,14 @@ pub(crate) fn parse_issue(e: &serde_path_to_error::Error<serde_json::Error>) ->
|
||||
Issue { path, message: truncate(&message, MAX_MESSAGE_LEN), suggestion: None }
|
||||
}
|
||||
|
||||
/// What is wrong with `params` for the command `id` by its typed contract: the issues a call gets
|
||||
/// at [`ParamCheck::Warn`] (the first is the refusal at [`ParamCheck::Strict`]). None for a
|
||||
/// command without a contract, or an id the registry lacks (a runner in another process may be
|
||||
/// another version).
|
||||
pub fn issues(id: &str, params: &Value) -> Vec<Issue> {
|
||||
crate::cmd::find_command(id).and_then(|c| c.contract).map(|c| c.check(params)).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Run `handler` as the command `id`, checking the call against the contract `id` publishes
|
||||
/// (its [`CommandSpec::contract`](crate::cmd::CommandSpec::contract)), so the contract enforced is
|
||||
/// always the one described. With the level off and no audit recorder, this is exactly
|
||||
|
||||
@@ -68,7 +68,7 @@ pub mod sketch_create;
|
||||
pub mod sketch_equation;
|
||||
pub mod view_navigation;
|
||||
|
||||
pub use check::{Contract, Issue, ParamCheck, TypedContract, input_schema_for, output_schema_for, run_checked, run_checked_as};
|
||||
pub use check::{Contract, Issue, ParamCheck, TypedContract, input_schema_for, issues, output_schema_for, run_checked, run_checked_as};
|
||||
pub use types::*;
|
||||
|
||||
/// A command's `run` that checks the call against the contract command `$id` publishes
|
||||
|
||||
@@ -361,6 +361,16 @@ fn the_level_survives_a_new_design() {
|
||||
assert_eq!(s.scratch().param_check, ParamCheck::Strict);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issues_are_read_from_the_commands_own_contract() {
|
||||
let issues = super::issues("solid.box", &json!({"length": 1, "width": 1, "height": 1, "operaton": "cut"}));
|
||||
assert_eq!(messages(&issues), ["unknown key `operaton`; did you mean `operation`?"]);
|
||||
assert_eq!(issues, box_contract().check(&json!({"length": 1, "width": 1, "height": 1, "operaton": "cut"})));
|
||||
// No contract, or no such command: nothing to say.
|
||||
assert!(super::issues("solid.fillet", &json!({"nonsense": 1})).is_empty());
|
||||
assert!(super::issues("no.such", &json!({"nonsense": 1})).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_level_is_read_back_from_its_name() {
|
||||
for level in ParamCheck::ALL {
|
||||
|
||||
Reference in New Issue
Block a user