Skip to content

Commit

Permalink
forbid ieq for boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
askurydzin committed May 4, 2019
1 parent 79a3be6 commit a3e2c33
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestValidateFiltering(t *testing.T) {
{`boolean_field in ["tRuE"]`, true},
{`boolean_field=="True"`, false},
{`boolean_field=="Blah"`, true},
{`boolean_field:="True"`, true},
}

for _, test := range tests {
Expand Down
5 changes: 5 additions & 0 deletions options/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func ValidateFiltering(f *query.Filtering, messageInfo map[string]FilteringOptio
}

if fieldInfo.ValueType == QueryValidate_BOOL {

if x.Type != query.StringCondition_EQ {
return fmt.Errorf("Operation %s is not allowed for %q", query.StringCondition_Type_name[int32(x.Type)], fieldTag)
}

if _, err := strconv.ParseBool(x.Value); err != nil {
return fmt.Errorf("Got invalid literal for field %q of type %s, expect 'true' or 'false'", fieldTag, fieldInfo.ValueType)
}
Expand Down
5 changes: 4 additions & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
protoTypeInt64Value = ".google.protobuf.Int64Value"
protoTypeUInt32Value = ".google.protobuf.UInt32Value"
protoTypeUInt64Value = ".google.protobuf.UInt64Value"
protoTypeBoolValue = ".google.protobuf.BoolValue"
)

// QueryValidatePlugin implements the plugin interface and creates validations for collection operation parameters code from .protos
Expand Down Expand Up @@ -409,6 +410,8 @@ func (p *QueryValidatePlugin) getValueType(field *descriptor.FieldDescriptorProt
protoTypeUInt32Value,
protoTypeUInt64Value:
return options.QueryValidate_NUMBER
case protoTypeBoolValue:
return options.QueryValidate_BOOL
default:
return options.QueryValidate_DEFAULT
}
Expand Down Expand Up @@ -526,6 +529,7 @@ func (p *QueryValidatePlugin) getFieldSelectionDataAux(msg *generator.Descriptor
protoTypeUUIDValue,
protoTypeInet,
protoTypeStringValue:
case protoTypeBoolValue:
case protoTypeDoubleValue,
protoTypeFloatValue,
protoTypeInt32Value,
Expand Down Expand Up @@ -589,7 +593,6 @@ func (p *QueryValidatePlugin) getDenyRules(fieldName string, opts *options.Query
supportedOps = []options.QueryValidate_FilterOperator{
options.QueryValidate_EQ,
options.QueryValidate_IN,
options.QueryValidate_IEQ,
}
}

Expand Down

0 comments on commit a3e2c33

Please sign in to comment.