Skip to content

Commit

Permalink
[terminal/input] Add input validation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 30, 2024
1 parent dfbd8d9 commit c94d544
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 51 deletions.
7 changes: 0 additions & 7 deletions terminal/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ type HintHandler = func(input string) string

// ////////////////////////////////////////////////////////////////////////////////// //

// Validator is input validator type
type Validator interface {
Validate(input string) (string, error)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// ErrKillSignal is error type when user cancel input
var ErrKillSignal = linenoise.ErrKillSignal

Expand Down
43 changes: 2 additions & 41 deletions terminal/input/input_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ type CompletionHandler = func(input string) []string
// HintHandler is hint handler
type HintHandler = func(input string) string

// Validator is input validation function
type Validator = func(input string) (string, error)

// ////////////////////////////////////////////////////////////////////////////////// //

// ❗ ErrKillSignal is error type when user cancel input
Expand Down Expand Up @@ -64,44 +61,8 @@ var NewLine = false

// ////////////////////////////////////////////////////////////////////////////////// //

var (
// NotEmpty returns an error if input is empty
NotEmpty = func(input string) (string, error) { return "", nil }

// IsNumber returns an error if the input is not a valid number
IsNumber = func(input string) (string, error) { return "", nil }

// IsFloat returns an error if the input is not a valid floating number
IsFloat = func(input string) (string, error) { return "", nil }

// IsEmail returns an error if the input is not a valid email
IsEmail = func(input string) (string, error) { return "", nil }

// IsURL returns an error if the input is not a valid URL
IsURL = func(input string) (string, error) { return "", nil }
)

// ////////////////////////////////////////////////////////////////////////////////// //

var (
// ErrInvalidAnswer is error for wrong answer for Y/N question
ErrInvalidAnswer = errors.New("")

// ErrIsEmpty is error for empty input
ErrIsEmpty = errors.New("")

// ErrInvalidNumber is error for invalid number
ErrInvalidNumber = errors.New("")

// ErrInvalidFloat is error for invalid floating number
ErrInvalidFloat = errors.New("")

// ErrInvalidEmail is error for invalid email
ErrInvalidEmail = errors.New("")

// ErrInvalidURL is error for invalid URL
ErrInvalidURL = errors.New("")
)
// ErrInvalidAnswer is error for wrong answer for Y/N question
var ErrInvalidAnswer = errors.New("")

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
10 changes: 7 additions & 3 deletions terminal/input/input_validators.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build !windows
// +build !windows

package input

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -18,6 +15,13 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

// Validator is input validator type
type Validator interface {
Validate(input string) (string, error)
}

// ////////////////////////////////////////////////////////////////////////////////// //

type notEmptyValidator struct{}
type isNumberValidator struct{}
type isFloatValidator struct{}
Expand Down

0 comments on commit c94d544

Please sign in to comment.