Skip to content

Commit ff89a20

Browse files
committed
feat: validate moniker
1 parent 449df60 commit ff89a20

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

models/initia/run_l1_node.go

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ func NewRunL1NodeMonikerInput(state *RunL1NodeState) *RunL1NodeMonikerInput {
302302
question: "Please specify the moniker",
303303
}
304304
model.WithPlaceholder("Enter moniker")
305+
model.WithValidatorFn(utils.ValidateEmptyString)
305306
return model
306307
}
307308

models/initia/run_l1_node_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ func TestRunL1NodeMonikerInput_Update(t *testing.T) {
6262
}
6363

6464
model := NewRunL1NodeMonikerInput(mockState)
65+
m, _ := model.Update(tea.KeyMsg{Type: tea.KeyEnter})
66+
assert.IsType(t, m, &RunL1NodeMonikerInput{})
6567

6668
model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("Node1")})
6769
assert.Equal(t, "Node1", model.TextInput.Text)
6870

69-
m, _ := model.Update(tea.KeyMsg{Type: tea.KeyEnter})
71+
m, _ = model.Update(tea.KeyMsg{Type: tea.KeyEnter})
7072
assert.Equal(t, "Node1", mockState.moniker)
7173
assert.IsType(t, m, &MinGasPriceInput{})
7274
}

utils/text_input.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func NewTextInput() TextInput {
2222
Cursor: 0,
2323
Placeholder: "<todo: Jennie revisit placeholder>",
2424
ValidationFn: NoOps,
25+
IsEntered: false,
2526
}
2627
}
2728

@@ -128,8 +129,14 @@ func moveToNextWord(text string, cursor int) int {
128129
func (ti TextInput) View() string {
129130
var beforeCursor, cursorChar, afterCursor string
130131
bottomText := styles.Text("Press Enter to submit or Ctrl+C to quit.", styles.Gray)
132+
feedback := ""
133+
if ti.IsEntered {
134+
if err := ti.ValidationFn(ti.Text); err != nil {
135+
feedback = styles.RenderError(err)
136+
}
137+
}
131138
if len(ti.Text) == 0 {
132-
return "\n" + styles.Text("> ", styles.Cyan) + styles.Text(ti.Placeholder, styles.Gray) + styles.Cursor(" ") + "\n\n" + bottomText
139+
return fmt.Sprintf("\n%s %s\n\n%s%s", styles.Text(">", styles.Cyan), styles.Text(ti.Placeholder, styles.Gray), feedback, bottomText)
133140
} else if ti.Cursor < len(ti.Text) {
134141
// Cursor is within the text
135142
beforeCursor = styles.Text(ti.Text[:ti.Cursor], styles.White)
@@ -141,13 +148,6 @@ func (ti TextInput) View() string {
141148
cursorChar = styles.Cursor(" ")
142149
}
143150

144-
feedback := ""
145-
if ti.IsEntered {
146-
if err := ti.ValidationFn(ti.Text); err != nil {
147-
feedback = styles.RenderError(err)
148-
}
149-
}
150-
151151
// Compose the full view string
152152
return fmt.Sprintf("\n%s %s%s%s\n\n%s%s", styles.Text(">", styles.Cyan), beforeCursor, cursorChar, afterCursor, feedback, bottomText)
153153
}

utils/validate.go

+7
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,10 @@ func ValidateExactString(expect string) func(s string) error {
247247
return nil
248248
}
249249
}
250+
251+
func ValidateEmptyString(s string) error {
252+
if s == "" {
253+
return fmt.Errorf("cannot be empty string")
254+
}
255+
return nil
256+
}

0 commit comments

Comments
 (0)