Merge pull request #5 from foomo/feature/update-querry-validation

feat: Update query
This commit is contained in:
danielthomas74
2023-10-05 17:45:14 +02:00
committed by GitHub
3 changed files with 37 additions and 32 deletions
+7
View File
@@ -13,4 +13,11 @@ const (
HealthOk Health = "ok"
)
const (
MissingQueryFieldValues QueryError = "Missing field values"
QueryValueExpired QueryError = "Query field value is expired"
MissingQueryCondition QueryError = "Missing query condition"
MissingQueryField QueryError = "Query Field is empty"
)
const DateFormat = "02 Jan 2006"
+1
View File
@@ -2,3 +2,4 @@ package constants
type Severity string
type Health string
type QueryError string
+29 -32
View File
@@ -1,66 +1,63 @@
package validations
import (
"fmt"
catvo "github.com/bestbytes/catalogue/vo"
"github.com/foomo/contentfulvalidation/constants"
)
func IsAttributeExpired(query *catvo.Query, attributes catvo.Attributes) bool {
func ValidateQuery(query *catvo.Query, attributes catvo.Attributes) (constants.QueryError, bool) {
// TODO is this ok?
isValueExpired := func(value string, def catvo.AttributeDefinition) bool {
isValueExpired := func(value string, def catvo.AttributeDefinition) (constants.QueryError, bool) {
if len(value) < 1 {
return constants.MissingQueryFieldValues, true
}
if _, ok := def.EnumStrings[catvo.AttributeValueID(value)]; !ok {
// thow error
// provide contex on value with this id ....
fmt.Println("Attribute NOT found: ", ok)
return true
return constants.QueryValueExpired, true
} else {
fmt.Println("Attribute found: ", ok)
return false
return "", false
}
}
areValuesExpired := func(values []string, def catvo.AttributeDefinition) bool {
expired := false
areValuesExpired := func(values []string, def catvo.AttributeDefinition) (constants.QueryError, bool) {
if len(values) < 1 {
return constants.MissingQueryFieldValues, true
}
for _, v := range values {
if isValueExpired(v, def) {
expired = true
if res, ok := isValueExpired(v, def); ok {
return res, true
}
}
return expired
return "", false
}
for _, e := range query.Elements {
fmt.Println("THE Matcher: ", e.Matcher)
errorMessage := constants.QueryError("")
hasError := false
// @TODO validate if there is even an attribute set, or is empty string
if e.Matcher != nil {
if def, ok := attributes[e.Matcher.Attribute]; ok {
switch {
case e.Matcher.StringIn != nil:
return areValuesExpired(e.Matcher.StringIn.Values, def)
errorMessage, hasError = areValuesExpired(e.Matcher.StringIn.Values, def)
case e.Matcher.StringAllIn != nil:
return areValuesExpired(e.Matcher.StringAllIn.Values, def)
errorMessage, hasError = areValuesExpired(e.Matcher.StringAllIn.Values, def)
case e.Matcher.StringNotIn != nil:
return areValuesExpired(e.Matcher.StringNotIn.Values, def)
errorMessage, hasError = areValuesExpired(e.Matcher.StringNotIn.Values, def)
case e.Matcher.StringEquals != nil:
return isValueExpired(e.Matcher.StringEquals.Value, def)
errorMessage, hasError = isValueExpired(e.Matcher.StringEquals.Value, def)
case e.Matcher.StringNotEquals != nil:
return isValueExpired(e.Matcher.StringNotEquals.Value, def)
errorMessage, hasError = isValueExpired(e.Matcher.StringNotEquals.Value, def)
default:
errorMessage, hasError = constants.MissingQueryCondition, true
}
} else {
// throw error
fmt.Println("NO attribute within ALL ATTRIBUTES: ")
// TODO uncomment once catalogue attr are in, maybe a different validation for this
// return true
return constants.MissingQueryField, true
}
}
} else {
fmt.Println("MATCHER is NIL e.matcher: ")
return true
if hasError {
return errorMessage, hasError
}
}
return false
return "", false
}