Repository navigation
Reject malformed conditional-format formulas #2008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harrisonoconnorhover
wants to merge
2
commits into
storytold:main
Choose a base branch
from
harrisonoconnorhover:codex/validate-cf-formulas-1999
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,38 @@ | ||
| # Morning Handoff | ||
|
|
||
| ## Finished | ||
|
|
||
| - Reviewed #1999 and related issues #1458, #1848 and #1165. Their quoting, threshold and editor PRs do not fix malformed formula entry. | ||
| - Reproduced acceptance of invalid expression rules, comparison bounds and data-bar formula thresholds on upstream `23829d8`. | ||
| - Added entry validation using the existing parser, preserving workbook contents and undo/redo on rejection. | ||
| - Added four engine tests and one dialog test: errors retain the entered text; correcting it succeeds. Valid formulas and literal comparisons save and reopen. | ||
|
|
||
| ## Try It | ||
|
|
||
| - Run `cargo +1.95.0 test -p gridcraft-engine --test conditional_formula_validation`. | ||
| - Run `cargo +1.95.0 test -p gridcraft-ui-egui --test conditional_formula_validation`. | ||
| - In Conditional Formatting → New Rule, enter `=A1>`; OK should show an error and keep the dialog open. Change it to `=A1>5` and confirm. | ||
|
|
||
| ## Checks | ||
|
|
||
| - Baseline: three regression tests failed because malformed formulas were accepted; the valid-formula test passed. | ||
| - After the fix: all four engine tests and the dialog test passed. Full workspace: 1,665 passed, 0 failed, 1 ignored (`cargo +1.95.0 test --workspace`). Log: `/tmp/gridcraft-1999-workspace-tests.log`. | ||
| - Focused engine Clippy (`--all-targets -- -D warnings`), formatting, diff, assets, dependency layers, parity, keytips and WASM checks passed. WASM reports an unchanged dead-code warning in `backstage.rs`. | ||
| - `cargo +1.95.0 xtask ci` stopped on unchanged upstream `crates/ui-egui/src/desktop_theme.rs:36`: `clippy::let_unit_value` on macOS. | ||
|
|
||
| ## Decisions | ||
|
|
||
| - Validate at rule creation before any workbook edit; retain the existing dialog error handling. | ||
| - Existing malformed imported rules are not repaired or discarded. Syntax validation does not establish general Excel compatibility. | ||
| - Keep #1822/#1885 integration separate: current main has no rule-edit command or configurable colour-scale formula stops. Their future builders must retain the same validation. | ||
|
|
||
| ## Remaining | ||
|
|
||
| - Published [PR #2008](https://github.com/storytold/gridcraft/pull/2008) from `codex/validate-cf-formulas-1999`, based on upstream `23829d8`. GitHub CI and automated review were pending at publication. | ||
| - Shared `craftrules` instructions and local-only `plan/` files were unavailable; checked-in GridCraft instructions were followed. | ||
|
|
||
| ## Review First | ||
|
|
||
| - `crates/engine/src/cmd/insert.rs`: validation before mutation. | ||
| - `crates/engine/tests/conditional_formula_validation.rs` and `crates/ui-egui/tests/conditional_formula_validation.rs`: rejection, preservation and correction. | ||
| - `docs/decisions.md`: related-PR boundaries and imported-file limitation. |
This file contains hidden or 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 hidden or 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,89 @@ | ||
| //! Malformed conditional-format formulas must be refused before they reach a saved workbook (#1999). | ||
|
|
||
| use gridcraft_engine::Session; | ||
| use serde_json::{Value, json}; | ||
|
|
||
| fn session() -> Session { | ||
| let mut s = Session::new(); | ||
| s.new_workbook(); | ||
| s.execute("home.conditionalFormat", json!({"range": "A1:A3", "rule": {"type": "expression", "formula": "A1>5"}})).unwrap(); | ||
| // A rejected rule must preserve both existing rules and a pending redo. | ||
| s.execute("cell.set", json!({"cell": "B1", "input": "7"})).unwrap(); | ||
| s.execute("edit.undo", json!({})).unwrap(); | ||
| s | ||
| } | ||
|
|
||
| fn rejects_without_changes(rule: Value) { | ||
| let mut s = session(); | ||
| let before = serde_json::to_value(&s.doc().unwrap().wb).unwrap(); | ||
| let history = (s.doc().unwrap().undo.len(), s.doc().unwrap().redo.len()); | ||
| let error = s.execute("home.conditionalFormat", json!({"range": "A1:A3", "rule": rule})).expect_err("malformed formula was accepted"); | ||
| assert!(error.to_string().contains("formula"), "{error}"); | ||
| assert_eq!(serde_json::to_value(&s.doc().unwrap().wb).unwrap(), before); | ||
| assert_eq!((s.doc().unwrap().undo.len(), s.doc().unwrap().redo.len()), history); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_expression_is_refused_without_changing_rules_or_history() { | ||
| for formula in ["=A1>", "=1+", "", " "] { | ||
| rejects_without_changes(json!({"type": "expression", "formula": formula})); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_cell_comparison_bounds_are_refused() { | ||
| rejects_without_changes(json!({"type": "cellIs", "operator": "greaterThan", "value": "=1+"})); | ||
| rejects_without_changes(json!({"type": "cellIs", "operator": "between", "value": 1, "value2": "=A1>"})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_data_bar_formula_bounds_are_refused() { | ||
| for bound in ["min", "max"] { | ||
| let mut rule = json!({"type": "dataBar"}); | ||
| rule[bound] = json!({"type": "formula", "value": "1+"}); | ||
| rejects_without_changes(rule); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn valid_formulas_and_literal_comparisons_still_save_and_reopen() { | ||
| let mut s = Session::new(); | ||
| s.new_workbook(); | ||
| for rule in [ | ||
| json!({"type": "expression", "formula": "A1>5"}), | ||
| json!({"type": "expression", "formula": "=AND(A1>0,A1<10)"}), | ||
| json!({"type": "expression", "formula": "=FutureFunction(A1)"}), | ||
| json!({"type": "expression", "formula": "=#REF!"}), | ||
| json!({"type": "cellIs", "operator": "between", "value": "=$Z$1", "value2": "=MAX($Z$2,10)"}), | ||
| json!({"type": "cellIs", "operator": "equal", "value": "1+"}), | ||
| json!({"type": "cellIs", "operator": "equal", "value": 5}), | ||
| json!({"type": "dataBar", "min": {"type": "formula", "value": "$Z$1"}, "max": {"type": "formula", "value": "MAX($Z$2,10)"}}), | ||
| ] { | ||
| s.execute("home.conditionalFormat", json!({"range": "A1:A3", "rule": rule})).unwrap(); | ||
| } | ||
| let formulas = |s: &Session| { | ||
| use gridcraft_engine::model::{CfRule, CfValueKind}; | ||
| s.doc() | ||
| .unwrap() | ||
| .wb | ||
| .active() | ||
| .unwrap() | ||
| .cond_formats | ||
| .iter() | ||
| .map(|cf| { | ||
| let values = match &cf.rule { | ||
| CfRule::Expression { formula, .. } => vec![formula.as_str()], | ||
| CfRule::CellIs { a, b, .. } => std::iter::once(a.as_str()).chain(b.as_deref()).collect(), | ||
| CfRule::DataBar { min: CfValueKind::Formula(a), max: CfValueKind::Formula(b), .. } => vec![a.as_str(), b.as_str()], | ||
| _ => panic!("unexpected rule"), | ||
| }; | ||
| values.into_iter().map(|v| gridcraft_formula::print(&gridcraft_formula::parse(v).unwrap())).collect::<Vec<_>>() | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| }; | ||
| let expected = formulas(&s); | ||
| let saved = s.execute("file.saveBytes", json!({"format": "xlsx"})).unwrap(); | ||
| let mut reopened = Session::new(); | ||
| reopened.execute("file.open", json!({"name": "rules.xlsx", "base64": saved["base64"]})).unwrap(); | ||
| assert_eq!(formulas(&reopened), expected); | ||
| } |
This file contains hidden or 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,38 @@ | ||
| //! A rejected conditional-format formula stays available for correction (#1999). | ||
|
|
||
| use egui_kittest::kittest::Queryable as _; | ||
| use gridcraft_engine::Session; | ||
| use gridcraft_ui_egui::SheetApp; | ||
| use serde_json::json; | ||
|
|
||
| #[test] | ||
| fn invalid_formula_keeps_the_dialog_open_until_corrected() { | ||
| let mut session = Session::new(); | ||
| session.new_workbook(); | ||
| let mut app = SheetApp::new(session, Default::default()); | ||
| app.open_dialog("cfQuick", json!({"type": "expression"})); | ||
| app.dialog.as_mut().unwrap().values.insert("formula".into(), json!("=A1>")); | ||
| let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state( | ||
| |ui, app: &mut SheetApp| { | ||
| let ctx = ui.ctx().clone(); | ||
| app.logic(&ctx); | ||
| app.ui(ui); | ||
| }, | ||
| app, | ||
| ); | ||
| h.run_steps(4); | ||
| h.get_by_label(" OK ").click(); | ||
| h.run_steps(3); | ||
| assert!(h.state().session.doc().unwrap().wb.active().unwrap().cond_formats.is_empty()); | ||
| assert_eq!(h.state().dialog.as_ref().unwrap().values.get("formula"), Some(&json!("=A1>"))); | ||
| assert!(h.state().message.as_ref().unwrap().1.contains("formula")); | ||
|
|
||
| h.state_mut().message = None; | ||
| h.state_mut().dialog.as_mut().unwrap().values.insert("formula".into(), json!("=A1>5")); | ||
| h.run_steps(3); | ||
| h.get_by_label(" OK ").click(); | ||
| h.run_steps(3); | ||
| assert!(h.state().dialog.is_none()); | ||
| assert!(h.state().message.is_none()); | ||
| assert_eq!(h.state().session.doc().unwrap().wb.active().unwrap().cond_formats.len(), 1); | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Reject repeated leading equals signs before storing a rule.
If a user enters
==A1, both paths remove every leading=beforecf_formularuns. The command accepts the malformed input and storesA1instead. The parser itself removes only one leading=, so validate the original formula and remove at most one prefix for storage. (raw.githubusercontent.com)Also applies to: 1401-1401
🤖 Prompt for AI Agents