Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate boolean inputs #2010

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/flowpilot/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,5 @@ var (
)

func createMustBeOneOfError(values []string) InputError {
return NewInputError("value_invalid_error", fmt.Sprintf("The value is invalid. Must be one of: %s", strings.Join(values, ",")))
return NewInputError("value_invalid_error", fmt.Sprintf("The value is invalid. Must be one of: %s", strings.Join(values, ", ")))
}
20 changes: 15 additions & 5 deletions backend/flowpilot/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flowpilot

import (
"regexp"
"strconv"
)

// inputType represents the type of the input field.
Expand All @@ -17,6 +18,10 @@ const (
inputTypeJSON inputType = "json"
)

var (
inputTypeBooleanAllowedValues = []string{"1", "t", "T", "TRUE", "true", "True", "0", "f", "F", "FALSE", "false", "False"}
)

// Input defines the interface for input fields.
type Input interface {
MinLength(minLength int) Input
Expand Down Expand Up @@ -209,10 +214,6 @@ func (i *defaultInput) validate(inputData readOnlyActionInput) bool {
return true
}

if i.dataType == inputTypeBoolean {
return true
}

isRequired := i.required != nil && *i.required
hasEmptyOrNilValue := inputValue == nil || len(*inputValue) <= 0

Expand All @@ -235,7 +236,16 @@ func (i *defaultInput) validate(inputData readOnlyActionInput) bool {
}
}

if i.dataType == inputTypeEmail && (isRequired || (!isRequired && !hasEmptyOrNilValue)) {
if i.dataType == inputTypeBoolean && (isRequired || !hasEmptyOrNilValue) {
if _, err := strconv.ParseBool(*inputValue); err != nil {
i.error = createMustBeOneOfError(inputTypeBooleanAllowedValues)
return false
}

return true
}

if i.dataType == inputTypeEmail && (isRequired || !hasEmptyOrNilValue) {
pattern := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
if matched := pattern.MatchString(*inputValue); !matched {
i.error = ErrorValueInvalid
Expand Down
Loading