Skip to content

Extract validation functions from interactive forms#7243

Merged
pelikhan merged 3 commits intomainfrom
copilot/extract-validation-functions
Dec 22, 2025
Merged

Extract validation functions from interactive forms#7243
pelikhan merged 3 commits intomainfrom
copilot/extract-validation-functions

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

Inline validation logic in the interactive workflow builder made testing and reuse difficult. This extracts validation into dedicated, testable functions.

Changes

  • New pkg/cli/validators.go: Exports ValidateWorkflowName(string) error with regex pattern for alphanumeric, hyphen, underscore validation
  • New pkg/cli/validators_test.go: 35 test cases covering valid inputs, invalid characters, edge cases, and error messages
  • Updated pkg/cli/interactive.go: Replaced inline validator with function call, removed unused imports (errors, regexp)
  • Updated pkg/cli/interactive_test.go: Refactored existing tests to use extracted function

Before/After

// Before: inline validation in form builder
huh.NewInput().
    Validate(func(s string) error {
        if s == "" {
            return errors.New("workflow name cannot be empty")
        }
        if !workflowNameRegex.MatchString(s) {
            return errors.New("workflow name must contain only alphanumeric characters, hyphens, and underscores")
        }
        return nil
    })

// After: reusable validation function
huh.NewInput().
    Validate(ValidateWorkflowName)

The validation behavior is unchanged. Additional validators can now be added to the same file.

Original prompt

This section details on the original issue you should resolve

<issue_title>[plan] Extract validation functions from interactive forms</issue_title>
<issue_description>## Objective

Extract inline validation logic into separate, testable functions in a new pkg/cli/validators.go file.

Context

Currently, validation logic is defined inline within the form builder, making it difficult to test, reuse, and maintain. Extracting these into dedicated functions improves code quality and enables unit testing.

Approach

  1. Create pkg/cli/validators.go with exported validation functions
  2. Extract existing validation logic from interactive.go:
    • Workflow name validation (alphanumeric, hyphens, underscores)
    • Any other field validations currently inline
  3. Update the form builder to use these functions
  4. Add unit tests in pkg/cli/validators_test.go

Example structure:

// pkg/cli/validators.go
package cli

import (
    "errors"
    "regexp"
)

var workflowNameRegex = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)

func ValidateWorkflowName(s string) error {
    if s == "" {
        return errors.New("workflow name cannot be empty")
    }
    if !workflowNameRegex.MatchString(s) {
        return errors.New("workflow name must contain only alphanumeric characters, hyphens, and underscores")
    }
    return nil
}

// Then in interactive.go:
huh.NewInput().
    Title("Workflow Name").
    Value(&name).
    Validate(ValidateWorkflowName)

Files to Create

  • Create: pkg/cli/validators.go - Validation functions
  • Create: pkg/cli/validators_test.go - Unit tests

Files to Modify

  • Update: pkg/cli/interactive.go - Use extracted validation functions

Acceptance Criteria

AI generated by Plan Command for discussion #7214

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 22, 2025 13:11
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
- Created pkg/cli/validators.go with ValidateWorkflowName function
- Created pkg/cli/validators_test.go with comprehensive unit tests
- Updated pkg/cli/interactive.go to use extracted validator
- Removed inline validation logic and unused imports
- Updated existing tests to use new validation function
- All tests passing

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Extract validation functions from interactive forms Extract validation functions from interactive forms Dec 22, 2025
Copilot AI requested a review from mnkiefer December 22, 2025 13:19
@pelikhan pelikhan marked this pull request as ready for review December 22, 2025 14:41
@pelikhan pelikhan merged commit d4359b0 into main Dec 22, 2025
4 checks passed
@pelikhan pelikhan deleted the copilot/extract-validation-functions branch December 22, 2025 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[plan] Extract validation functions from interactive forms

3 participants