Skip to content

Commit

Permalink
Add: bool request option filter
Browse files Browse the repository at this point in the history
  • Loading branch information
llugin committed Aug 26, 2024
1 parent 0216e0f commit 3492a5d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/query/filter/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ReadableValue[T any] struct {

// RequestOptionType configures the type of control for a field in a request option.
type RequestOptionType struct {
Type ControlType `json:"type" enums:"string,float,integer,enum"`
Type ControlType `json:"type" enums:"string,float,integer,enum,bool"`
}

// RequestField represents a field in a request
Expand Down
1 change: 1 addition & 0 deletions pkg/query/filter/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package filter
/*
ControlType ENUM(
bool
enum
float
integer
Expand Down
7 changes: 5 additions & 2 deletions pkg/query/filter/type_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/query/filter/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func validateFieldValueType(requestOption RequestOption, fieldName string, field
if _, ok := fieldValue.(float64); !ok {
return NewValidationError("field '%s' must be from type '%s'", fieldName, requestOption.Control.Type)
}
} else if requestOption.Control.Type == ControlTypeBool {
if _, ok := fieldValue.(bool); !ok {
return NewValidationError("field '%s' must be from type '%s'", fieldName, requestOption.Control.Type)
}
} else if requestOption.Control.Type == ControlTypeString ||
requestOption.Control.Type == ControlTypeEnum || requestOption.Control.Type == ControlTypeUuid {
if _, ok := fieldValue.(string); !ok {
Expand Down
44 changes: 44 additions & 0 deletions pkg/query/filter/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,47 @@ func TestFloatRequestOption(t *testing.T) {
assert.Equal(t, validationError.Error(), "field 'optionName' must be from type 'float'")
})
}

func TestBoolRequestOption(t *testing.T) {
var requestOptions []RequestOption

setup := func(t *testing.T) {
requestOptions = []RequestOption{
{
Name: ReadableValue[string]{
Value: "optionName",
},
Control: RequestOptionType{
Type: ControlTypeBool,
},
Operators: []ReadableValue[CompareOperator]{
{
Value: CompareOperatorContains,
},
{
Value: CompareOperatorDoesNotContain,
},
},
MultiSelect: false,
},
}
}

t.Run("shouldReturnErrorOnInvalidValueDataType", func(t *testing.T) {
setup(t)

err := ValidateFilter(&Request{
Operator: LogicOperatorAnd,
Fields: []RequestField{
{
Name: "optionName",
Operator: CompareOperatorContains,
Value: "0",
},
},
}, requestOptions)
var validationError *ValidationError
assert.True(t, errors.As(err, &validationError))
assert.Equal(t, validationError.Error(), "field 'optionName' must be from type 'bool'")
})
}

0 comments on commit 3492a5d

Please sign in to comment.