Validation Logic
Relevant source files
The following files were used as context for generating this wiki page:
The wizard-tutorial project implements a terminal-based wizard using the Bubble Tea framework. The "Validation Logic" within this system primarily focuses on state management and input handling rather than complex data sanitization. It ensures that user inputs are captured, associated with specific questions, and transitioned through a sequence of steps until the wizard completes.
The system relies on a central state machine managed by the Main struct, which orchestrates the flow between different input fields (shortAnswerField and longAnswerField). While the current implementation lacks explicit character-level or pattern-matching validation rules, it enforces a structural validation where the completion of one input triggers the transition to the next state or the final results display.
Sources: README.md:1-20, main.go:27-34, input.go:9-17
Input State Management
Input validation and handling are abstracted through an Input interface. This allows the system to treat different types of input fields—such as short text inputs and multi-line text areas—uniformly. The state of each input is maintained within its respective struct, ensuring that the Value() can be retrieved only when the wizard logic deems the input "submitted" via the "enter" key.
Input Interface and Wrappers
The Input interface defines the contract for any field capable of capturing data.
| Method | Purpose |
|---|---|
Value() | Returns the current string content of the input field. |
Update(tea.Msg) | Processes keystrokes and updates the internal state. |
Blink() | Returns a command to handle cursor blinking. |
Blur() | Returns a command to remove focus from the input. |
Sources: input.go:9-17, input.go:49-51, input.go:87-89
Component Architecture
The following diagram illustrates the relationship between the Main application state and the individual input components.
Sources: main.go:27-40, input.go:19-21, input.go:57-59
Submission Flow and Transition Logic
The core logic for "validating" the end of an input session occurs within the Update method of the Main struct. When a user presses the enter key, the system captures the current value of the active input and assigns it to the Question.answer field.
Key Message Handling
The system handles specific tea.KeyMsg types to control the lifecycle of the data input process:
- Termination:
ctrl+corqtriggerstea.Quit. - Submission:
entertriggers the transfer of data from theInputcomponent to theQuestionstructure. - Completion Check: If the current
indexmatches the last element of thequestionsslice, thedoneflag is set totrue.
Sources: main.go:71-88, main.go:110-116
Data Capture Logic
case "enter":
if m.index == len(m.questions)-1 {
m.done = true
}
current.answer = current.input.Value()
m.Next()
return m, current.input.Blur
Sources: main.go:80-87
Data Rendering and Results
Once the done flag is set, the View function switches from the input form to a summary display. This effectively serves as the final validation of the data collected, iterating through all questions and printing the stored answers.
| Field | Type | Description |
|---|---|---|
question | string | The prompt presented to the user. |
answer | string | The value captured from the Input field upon "enter". |
styles | *Styles | Lip Gloss styles applied to the input container. |
Sources: main.go:15-25, main.go:94-101
The terminal wizard relies on the structural integrity of the Question slice and the stateful transitions of the Main model to ensure that all inputs are collected before the final output is generated. The use of the Input interface provides a modular way to extend validation in the future by adding logic to the Update or Value methods of specific input implementations.
Sources: main.go:120-123, input.go:40-43, input.go:78-81