Skip to content

Commit 34514fb

Browse files
marcelsafinCopilot
andauthored
fix(workflows): report validation errors instead of crashing on non-string workflow.yml scalars (#3421)
* fix(workflows): validate scalar types before string operations in workflow validation YAML parses unquoted scalars like version: 1.0 and id: 123 as float/int, which crashed validate_workflow and workflow add with raw tracebacks. Type-check id, name, version and step ids before regex and string operations so these surface as validation errors. Accept an unquoted schema_version: 1.0 instead of printing a self-identical rejection message. Fixes #3420 Co-authored-by: Copilot <[email protected]> * fix(workflows): treat falsey non-strings as type errors, not missing fields Co-authored-by: Copilot <[email protected]> * fix(workflows): only accept schema_version 1.0 so the error message is accurate The check also accepted "1" while the error said Expected '1.0'. Unquoted YAML 1.0 still works via str(); plain 1 is now rejected with the message that matches. Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 87a9690 commit 34514fb

3 files changed

Lines changed: 188 additions & 6 deletions

File tree

‎src/specify_cli/workflows/_commands.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,15 @@ def _validate_and_install_local(yaml_path: Path, source_label: str) -> None:
601601
except (ValueError, yaml.YAMLError) as exc:
602602
console.print(f"[red]Error:[/red] Invalid workflow YAML: {exc}")
603603
raise typer.Exit(1)
604-
if not definition.id or not definition.id.strip():
604+
# Non-string ids (e.g. unquoted ``id: 123`` or ``id: 0``) fall through
605+
# to validate_workflow below, which reports a typed error instead of
606+
# crashing on ``.strip()`` here. Only None/empty/whitespace-only ids
607+
# are rejected as missing.
608+
if (
609+
definition.id is None
610+
or definition.id == ""
611+
or (isinstance(definition.id, str) and not definition.id.strip())
612+
):
605613
console.print("[red]Error:[/red] Workflow definition has an empty or missing 'id'")
606614
raise typer.Exit(1)
607615

‎src/specify_cli/workflows/engine.py‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,49 @@ def validate_workflow(definition: WorkflowDefinition) -> list[str]:
129129
errors: list[str] = []
130130

131131
# -- Schema version ---------------------------------------------------
132-
if definition.schema_version not in ("1.0", "1"):
132+
# str() so an unquoted ``schema_version: 1.0`` (YAML float) is accepted —
133+
# rejecting it would print "Unsupported schema_version 1.0. Expected '1.0'."
134+
if str(definition.schema_version) != "1.0":
133135
errors.append(
134136
f"Unsupported schema_version {definition.schema_version!r}. "
135137
f"Expected '1.0'."
136138
)
137139

138140
# -- Top-level fields -------------------------------------------------
139-
if not definition.id:
141+
# YAML parses unquoted scalars like ``id: 123`` or ``version: 1.0`` as
142+
# int/float; check types before regex/string operations so authoring
143+
# mistakes surface as validation errors instead of tracebacks. Only
144+
# ``None``/empty-string count as missing so falsey non-strings
145+
# (``id: 0``, ``name: false``) still get the typed error.
146+
if definition.id is None or definition.id == "":
140147
errors.append("Workflow is missing 'workflow.id'.")
148+
elif not isinstance(definition.id, str):
149+
errors.append(
150+
f"'workflow.id' must be a string, got "
151+
f"{type(definition.id).__name__} ({definition.id!r})."
152+
)
141153
elif not _ID_PATTERN.match(definition.id):
142154
errors.append(
143155
f"Workflow ID {definition.id!r} must be lowercase alphanumeric "
144156
f"with hyphens."
145157
)
146158

147-
if not definition.name:
159+
if definition.name is None or definition.name == "":
148160
errors.append("Workflow is missing 'workflow.name'.")
161+
elif not isinstance(definition.name, str):
162+
errors.append(
163+
f"'workflow.name' must be a string, got "
164+
f"{type(definition.name).__name__} ({definition.name!r})."
165+
)
149166

150-
if not definition.version:
167+
if definition.version is None or definition.version == "":
151168
errors.append("Workflow is missing 'workflow.version'.")
169+
elif not isinstance(definition.version, str):
170+
errors.append(
171+
f"'workflow.version' must be a string, got "
172+
f"{type(definition.version).__name__} ({definition.version!r}) — "
173+
f'quote it in YAML (version: "1.0.0").'
174+
)
152175
elif not re.match(r"^\d+\.\d+\.\d+$", definition.version):
153176
errors.append(
154177
f"Workflow version {definition.version!r} is not valid "
@@ -256,9 +279,15 @@ def _validate_steps(
256279
continue
257280

258281
step_id = step_config.get("id")
259-
if not step_id:
282+
if step_id is None or step_id == "":
260283
errors.append("Step is missing 'id' field.")
261284
continue
285+
if not isinstance(step_id, str):
286+
errors.append(
287+
f"Step ID must be a string, got "
288+
f"{type(step_id).__name__} ({step_id!r})."
289+
)
290+
continue
262291

263292
if ":" in step_id:
264293
errors.append(

‎tests/test_workflows.py‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,101 @@ def test_invalid_id_format(self):
27942794
errors = validate_workflow(definition)
27952795
assert any("lowercase alphanumeric" in e for e in errors)
27962796

2797+
def test_non_string_workflow_id_reports_error(self):
2798+
from specify_cli.workflows.engine import WorkflowDefinition, validate_workflow
2799+
2800+
definition = WorkflowDefinition.from_string("""
2801+
workflow:
2802+
id: 123
2803+
name: "Test"
2804+
version: "1.0.0"
2805+
steps:
2806+
- id: step-one
2807+
command: speckit.specify
2808+
""")
2809+
errors = validate_workflow(definition)
2810+
assert any("workflow.id" in e and "string" in e for e in errors)
2811+
2812+
def test_non_string_name_reports_error(self):
2813+
from specify_cli.workflows.engine import WorkflowDefinition, validate_workflow
2814+
2815+
definition = WorkflowDefinition.from_string("""
2816+
workflow:
2817+
id: "test"
2818+
name: 123
2819+
version: "1.0.0"
2820+
steps:
2821+
- id: step-one
2822+
command: speckit.specify
2823+
""")
2824+
errors = validate_workflow(definition)
2825+
assert any("workflow.name" in e and "string" in e for e in errors)
2826+
2827+
def test_unquoted_float_version_reports_error(self):
2828+
from specify_cli.workflows.engine import WorkflowDefinition, validate_workflow
2829+
2830+
definition = WorkflowDefinition.from_string("""
2831+
workflow:
2832+
id: "test"
2833+
name: "Test"
2834+
version: 1.0
2835+
steps:
2836+
- id: step-one
2837+
command: speckit.specify
2838+
""")
2839+
errors = validate_workflow(definition)
2840+
assert any("workflow.version" in e and "quote" in e for e in errors)
2841+
2842+
def test_non_string_step_id_reports_error(self):
2843+
from specify_cli.workflows.engine import WorkflowDefinition, validate_workflow
2844+
2845+
definition = WorkflowDefinition.from_string("""
2846+
workflow:
2847+
id: "test"
2848+
name: "Test"
2849+
version: "1.0.0"
2850+
steps:
2851+
- id: 123
2852+
command: speckit.specify
2853+
""")
2854+
errors = validate_workflow(definition)
2855+
assert any("Step ID" in e and "string" in e for e in errors)
2856+
2857+
def test_falsey_non_string_scalars_report_typed_errors(self):
2858+
from specify_cli.workflows.engine import WorkflowDefinition, validate_workflow
2859+
2860+
definition = WorkflowDefinition.from_string("""
2861+
workflow:
2862+
id: 0
2863+
name: false
2864+
version: 0.0
2865+
steps:
2866+
- id: 0
2867+
command: speckit.specify
2868+
""")
2869+
errors = validate_workflow(definition)
2870+
assert any("'workflow.id' must be a string" in e for e in errors)
2871+
assert any("'workflow.name' must be a string" in e for e in errors)
2872+
assert any("'workflow.version' must be a string" in e for e in errors)
2873+
assert any("Step ID must be a string" in e for e in errors)
2874+
assert not any("missing" in e for e in errors)
2875+
2876+
def test_unquoted_schema_version_accepted(self):
2877+
from specify_cli.workflows.engine import WorkflowDefinition, validate_workflow
2878+
2879+
definition = WorkflowDefinition.from_string("""
2880+
schema_version: 1.0
2881+
workflow:
2882+
id: "test"
2883+
name: "Test"
2884+
version: "1.0.0"
2885+
steps:
2886+
- id: step-one
2887+
command: speckit.specify
2888+
""")
2889+
errors = validate_workflow(definition)
2890+
assert errors == []
2891+
27972892
def test_no_steps(self):
27982893
from specify_cli.workflows.engine import WorkflowDefinition, validate_workflow
27992894

@@ -7049,3 +7144,53 @@ def test_non_gate_step_without_type_is_not_a_gate(self):
70497144
},
70507145
)
70517146
assert _gate_outcome(state) is None
7147+
7148+
7149+
class TestWorkflowAddNonStringScalars:
7150+
"""`workflow add` reports clean errors for non-string YAML scalars (#3420)."""
7151+
7152+
@pytest.mark.parametrize(
7153+
("field_yaml", "expected"),
7154+
[
7155+
('id: 123\n name: "Probe"\n version: "1.0.0"', "workflow.id"),
7156+
('id: "probe"\n name: "Probe"\n version: 1.0', "workflow.version"),
7157+
],
7158+
)
7159+
def test_add_reports_validation_error_not_traceback(
7160+
self, project_dir, monkeypatch, field_yaml, expected
7161+
):
7162+
from typer.testing import CliRunner
7163+
from specify_cli import app
7164+
7165+
monkeypatch.chdir(project_dir)
7166+
wf = project_dir / "workflow.yml"
7167+
wf.write_text(
7168+
"schema_version: \"1.0\"\n"
7169+
f"workflow:\n {field_yaml}\n"
7170+
"steps:\n - id: s1\n type: shell\n run: \"echo hi\"\n",
7171+
encoding="utf-8",
7172+
)
7173+
runner = CliRunner()
7174+
result = runner.invoke(app, ["workflow", "add", str(wf)])
7175+
assert result.exit_code == 1
7176+
assert result.exception is None or isinstance(result.exception, SystemExit)
7177+
assert expected in result.output
7178+
7179+
def test_add_non_string_step_id_reports_validation_error(
7180+
self, project_dir, monkeypatch
7181+
):
7182+
from typer.testing import CliRunner
7183+
from specify_cli import app
7184+
7185+
monkeypatch.chdir(project_dir)
7186+
wf = project_dir / "workflow.yml"
7187+
wf.write_text(
7188+
"workflow:\n id: \"probe\"\n name: \"Probe\"\n version: \"1.0.0\"\n"
7189+
"steps:\n - id: 123\n type: shell\n run: \"echo hi\"\n",
7190+
encoding="utf-8",
7191+
)
7192+
runner = CliRunner()
7193+
result = runner.invoke(app, ["workflow", "add", str(wf)])
7194+
assert result.exit_code == 1
7195+
assert result.exception is None or isinstance(result.exception, SystemExit)
7196+
assert "Step ID" in result.output

0 commit comments

Comments
 (0)