BUG-HUNT: [consistency] Improve plan name validation in Plan.validate_name #1718

Open
opened 2026-04-02 23:34:39 +00:00 by freemo · 1 comment
Owner

Metadata

  • Branch: fix/plan-name-validation-regex
  • Commit Message: fix(domain): replace chained replace/isalnum with regex in Plan.validate_name
  • Milestone: v3.7.0
  • Parent Epic: #1669

Background and Context

The validate_name method in the Plan class (located in src/cleveragents/domain/models/core/plan_legacy.py) uses a non-standard, hard-to-read approach to validate the plan name. It chains multiple .replace() calls to strip allowed characters and then checks if the remainder is alphanumeric. While functionally correct for valid inputs, this approach is inconsistent with idiomatic Python validation patterns and reduces code readability.

Current Behavior

The validation uses a series of replace calls followed by an isalnum check:

@field_validator("name")
@classmethod
def validate_name(cls: type["Plan"], v: str) -> str:
    """Validate plan name."""
    if not v.replace("-", "").replace("_", "").replace(" ", "").isalnum():
        raise ValueError(
            "Name must be alphanumeric with hyphens, underscores, or spaces"
        )
    return v

File: src/cleveragents/domain/models/core/plan_legacy.py
Function: Plan.validate_name
Lines: 89–97

Expected Behavior

The validation should use a regular expression to clearly and idiomatically express the allowed character set:

import re

@field_validator("name")
@classmethod
def validate_name(cls: type["Plan"], v: str) -> str:
    """Validate plan name."""
    if not re.match(r"^[a-zA-Z0-9_ -]+$", v):
        raise ValueError(
            "Name must be alphanumeric with hyphens, underscores, or spaces"
        )
    return v

Acceptance Criteria

  • Plan.validate_name uses re.match with pattern ^[a-zA-Z0-9_ -]+$ instead of chained .replace() + .isalnum()
  • The error message remains unchanged: "Name must be alphanumeric with hyphens, underscores, or spaces"
  • All existing tests for Plan.validate_name continue to pass
  • No regression in validation behaviour — the set of accepted and rejected names is identical to the current implementation
  • re is imported at the top of the module (or the existing import is confirmed present)

Supporting Information

  • Severity: Low — the current implementation is functionally correct; this is a consistency/readability improvement
  • Category: consistency
  • Location: src/cleveragents/domain/models/core/plan_legacy.pyPlan.validate_name (lines 89–97)
  • Related bug hunting session: #1669

Subtasks

  • Replace chained .replace().isalnum() logic with re.match(r"^[a-zA-Z0-9_ -]+$", v) in Plan.validate_name
  • Ensure import re is present at the top of plan_legacy.py
  • Tests (Behave): Verify existing scenarios for Plan.validate_name still pass
  • Tests (Behave): Add scenario confirming regex-based validation accepts and rejects the same inputs as before
  • Verify coverage >=97% via nox -s coverage_report
  • Run nox (all default sessions), fix any errors

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly (fix(domain): replace chained replace/isalnum with regex in Plan.validate_name), followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly (fix/plan-name-validation-regex).
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass.
  • Coverage >= 97%.

Automated by CleverAgents Bot
Supervisor: Bug Hunting | Agent: ca-new-issue-creator

## Metadata - **Branch**: `fix/plan-name-validation-regex` - **Commit Message**: `fix(domain): replace chained replace/isalnum with regex in Plan.validate_name` - **Milestone**: v3.7.0 - **Parent Epic**: #1669 ## Background and Context The `validate_name` method in the `Plan` class (located in `src/cleveragents/domain/models/core/plan_legacy.py`) uses a non-standard, hard-to-read approach to validate the plan name. It chains multiple `.replace()` calls to strip allowed characters and then checks if the remainder is alphanumeric. While functionally correct for valid inputs, this approach is inconsistent with idiomatic Python validation patterns and reduces code readability. ## Current Behavior The validation uses a series of `replace` calls followed by an `isalnum` check: ```python @field_validator("name") @classmethod def validate_name(cls: type["Plan"], v: str) -> str: """Validate plan name.""" if not v.replace("-", "").replace("_", "").replace(" ", "").isalnum(): raise ValueError( "Name must be alphanumeric with hyphens, underscores, or spaces" ) return v ``` **File**: `src/cleveragents/domain/models/core/plan_legacy.py` **Function**: `Plan.validate_name` **Lines**: 89–97 ## Expected Behavior The validation should use a regular expression to clearly and idiomatically express the allowed character set: ```python import re @field_validator("name") @classmethod def validate_name(cls: type["Plan"], v: str) -> str: """Validate plan name.""" if not re.match(r"^[a-zA-Z0-9_ -]+$", v): raise ValueError( "Name must be alphanumeric with hyphens, underscores, or spaces" ) return v ``` ## Acceptance Criteria - [ ] `Plan.validate_name` uses `re.match` with pattern `^[a-zA-Z0-9_ -]+$` instead of chained `.replace()` + `.isalnum()` - [ ] The error message remains unchanged: `"Name must be alphanumeric with hyphens, underscores, or spaces"` - [ ] All existing tests for `Plan.validate_name` continue to pass - [ ] No regression in validation behaviour — the set of accepted and rejected names is identical to the current implementation - [ ] `re` is imported at the top of the module (or the existing import is confirmed present) ## Supporting Information - **Severity**: Low — the current implementation is functionally correct; this is a consistency/readability improvement - **Category**: consistency - **Location**: `src/cleveragents/domain/models/core/plan_legacy.py` → `Plan.validate_name` (lines 89–97) - Related bug hunting session: #1669 ## Subtasks - [ ] Replace chained `.replace().isalnum()` logic with `re.match(r"^[a-zA-Z0-9_ -]+$", v)` in `Plan.validate_name` - [ ] Ensure `import re` is present at the top of `plan_legacy.py` - [ ] Tests (Behave): Verify existing scenarios for `Plan.validate_name` still pass - [ ] Tests (Behave): Add scenario confirming regex-based validation accepts and rejects the same inputs as before - [ ] Verify coverage >=97% via `nox -s coverage_report` - [ ] Run `nox` (all default sessions), fix any errors ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly (`fix(domain): replace chained replace/isalnum with regex in Plan.validate_name`), followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly (`fix/plan-name-validation-regex`). - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - All nox stages pass. - Coverage >= 97%. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunting | Agent: ca-new-issue-creator
freemo added this to the v3.7.0 milestone 2026-04-02 23:36:29 +00:00
Author
Owner

Issue triaged by project owner:

  • State: Verified
  • Priority: Priority/Low (confirmed)
  • MoSCoW: MoSCoW/Could Have — improving plan name validation is a consistency improvement. Could Have.

Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: ca-project-owner

Issue triaged by project owner: - **State**: Verified - **Priority**: Priority/Low (confirmed) - **MoSCoW**: MoSCoW/Could Have — improving plan name validation is a consistency improvement. Could Have. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: ca-project-owner
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Blocks
#1669 Bug Hunting Session
cleveragents/cleveragents-core
Reference
cleveragents/cleveragents-core#1718
No description provided.