From a2100179959b8912476ca3c40fb9861eb114ea87 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 22 Nov 2026 19:23:37 +0000 Subject: [PATCH] Precheck runs the same parameter checks as dispatch Session::precheck skipped the file.new value check that dispatch runs, so a bad file.new passed precheck and failed only at dispatch. Both now call one function, in dispatch's existing order. --- crates/engine/src/jobs.rs | 6 +----- crates/engine/src/session/execute.rs | 12 ++++++++++-- crates/engine/tests/it/precheck.rs | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs index 53b0e343bd..5959603026 100644 --- a/crates/engine/src/jobs.rs +++ b/crates/engine/src/jobs.rs @@ -1781,11 +1781,7 @@ impl Session { } return self.dispatch(&filter, p, background); } - if id == "file.new" { - crate::document_preset_cmds::validate_new_params(self, ¶ms)?; - } - crate::param_check::check(spec, ¶ms)?; - crate::filters::validate_params(id, ¶ms)?; + self.validate_params(id, spec, ¶ms)?; // A command that may not run leaves a floating selection floating (#13169): it is refused // before the piece drops, without a History step. One that may run is checked again // below, once the piece has dropped, on the pixels and selection it will change. diff --git a/crates/engine/src/session/execute.rs b/crates/engine/src/session/execute.rs index 219d2a0d65..e5f646338a 100644 --- a/crates/engine/src/session/execute.rs +++ b/crates/engine/src/session/execute.rs @@ -59,6 +59,15 @@ impl Session { self.live_commit = None; } + /// The per-command value checks `dispatch` and `precheck` share, in the order `dispatch` runs them. + pub(crate) fn validate_params(&self, id: &str, spec: &crate::registry::CommandSpec, params: &Value) -> Result<()> { + if id == "file.new" { + crate::document_preset_cmds::validate_new_params(self, params)?; + } + crate::param_check::check(spec, params)?; + crate::filters::validate_params(id, params) + } + /// Check a call without running it: the command exists, its params have the shape /// [`Self::execute`] requires (New Layer's fully), and it can run now. For a caller that does /// something first only when the command will go through: Free Transform applies its box @@ -71,8 +80,7 @@ impl Session { if !(params.is_object() || params.is_null()) { return Err(EngineError::BadParams { cmd: id.to_string(), msg: "params must be a JSON object".into() }); } - crate::param_check::check(spec, params)?; - crate::filters::validate_params(id, params)?; + self.validate_params(id, spec, params)?; if id == "layer.new.layer" { crate::new_layer::parse(params)?; } diff --git a/crates/engine/tests/it/precheck.rs b/crates/engine/tests/it/precheck.rs index b737ddea5a..45dc027071 100644 --- a/crates/engine/tests/it/precheck.rs +++ b/crates/engine/tests/it/precheck.rs @@ -80,3 +80,18 @@ fn precheck_agrees_with_execute() { } assert!(disagreements.is_empty(), "{disagreements:#?}"); } + +#[test] +fn precheck_reports_file_new_value_errors_like_execute() { + for p in + [json!({"width": 0}), json!({"width": 300_001}), json!({"height": "x"}), json!({"mode": "Bogus"}), json!({"width": 0, "name": 5}), json!({"name": 5})] + { + let pre = Session::new().precheck("file.new", &p).unwrap_err().to_string(); + let ran = Session::new().execute("file.new", p.clone()).unwrap_err().to_string(); + assert_eq!(pre, ran, "{p}"); + } + // Where both checks fail, the value check's message (not the parameter check's) wins, as in dispatch. + let both = Session::new().precheck("file.new", &json!({"width": 0, "name": 5})).unwrap_err().to_string(); + assert_eq!(both, "invalid parameters for `file.new`: `name` must be a string"); + Session::new().precheck("file.new", &json!({"width": 64, "height": 32})).unwrap(); +}