Skip to content
Open
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
13 changes: 13 additions & 0 deletions external/epsearchast/v3/type_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package epsearchast_v3
import (
"fmt"
"strconv"
"time"
)

type FieldType int
Expand All @@ -12,6 +13,7 @@ const (
Int64
Boolean
Float64
RFC3339Milli
)

func (f FieldType) String() string {
Expand All @@ -24,6 +26,8 @@ func (f FieldType) String() string {
return "bool"
case Float64:
return "float64"
case RFC3339Milli:
return "rfc3339milli"
default:
return "unknown"
}
Expand All @@ -46,6 +50,9 @@ func Convert(t FieldType, v string) (interface{}, error) {
newV, _ = strconv.ParseBool(v)
case Float64:
newV, _ = strconv.ParseFloat(v, 64)
case RFC3339Milli:
// Parse RFC3339 datetime with millisecond support
newV, _ = time.Parse(time.RFC3339Nano, v)
}

return newV, nil
Expand Down Expand Up @@ -92,6 +99,12 @@ func ValidateValue(t FieldType, v string) error {
return fmt.Errorf("invalid value for boolean: `%v`", v)
}
return nil
case RFC3339Milli:
_, e := time.Parse(time.RFC3339Nano, v)
if e != nil {
return fmt.Errorf("invalid value for rfc3339milli: `%v`, expected format like '2006-01-02T15:04:05.000Z'", v)
}
return nil
default:
return fmt.Errorf("Unsupported field type %v:%v", t, v)
}
Expand Down
41 changes: 41 additions & 0 deletions external/epsearchast/v3/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"github.com/stretchr/testify/require"
"strings"
"testing"
"time"

Check failure on line 8 in external/epsearchast/v3/validate_test.go

View workflow job for this annotation

GitHub Actions / Build

"time" imported and not used
)

var logicOps = []string{"AND", "OR"}
Expand Down Expand Up @@ -1325,6 +1326,46 @@
require.ErrorContains(t, err, "value [457.42] does not satisfy requirement [lt]")
}

func TestValidateAstWithTypeValidationReturnsNoErrorWhenRequestIsValidAsRFC3339Milli(t *testing.T) {
// Fixture Setup
// language=JSON
jsonTxt := `
{
"type": "EQ",
"args": [ "updated_at", "2024-01-01T00:00:00.000Z"]
}
`
ast, err := GetAst(jsonTxt)
require.NoError(t, err)

// Execute SUT
err = ValidateAstFieldAndOperatorsWithFieldTypes(ast, map[string][]string{"updated_at": {"eq"}}, map[string]FieldType{"updated_at": RFC3339Milli})

// Verification
require.NoError(t, err)
}

func TestValidateAstWithTypeValidationReturnsErrorWhenRequestIsNotValidAsRFC3339Milli(t *testing.T) {
// Fixture Setup
// language=JSON
jsonTxt := `
{
"type": "EQ",
"args": [ "updated_at", "2024-01-01"]
}
`
ast, err := GetAst(jsonTxt)
require.NoError(t, err)

// Execute SUT
err = ValidateAstFieldAndOperatorsWithFieldTypes(ast, map[string][]string{"updated_at": {"eq"}}, map[string]FieldType{"updated_at": RFC3339Milli})

// Verification
require.ErrorContains(t, err, "could not validate [updated_at]")
require.ErrorContains(t, err, "the value [2024-01-01]")
require.ErrorContains(t, err, "invalid value for rfc3339milli")
}

func TestValidateAstWithDefaultRestrictionOnOrComplexityReturnsAValidationErrorWhenFilterIsToComplex(t *testing.T) {
// Fixture Setup
// language=JSON
Expand Down
Loading