Merge pull request #7 from foomo/feat/query-slice-of-errors

feat: Refactor product query to return slice of errors
This commit is contained in:
danielthomas74 2023-10-10 14:43:07 +02:00 committed by GitHub
commit cf7478b80f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,59 +5,50 @@ import (
"github.com/foomo/contentfulvalidation/constants" "github.com/foomo/contentfulvalidation/constants"
) )
func ValidateQuery(query *catvo.Query, attributes catvo.Attributes) (constants.QueryError, bool) { func ValidateQuery(query *catvo.Query, attributes catvo.Attributes) []constants.QueryError {
errors := []constants.QueryError{}
isValueExpired := func(value string, def catvo.AttributeDefinition) (constants.QueryError, bool) { isValueExpired := func(value string, def catvo.AttributeDefinition) {
if len(value) < 1 { if len(value) < 1 {
return constants.MissingQueryFieldValues, true errors = append(errors, constants.MissingQueryFieldValues)
}
if _, ok := def.EnumStrings[catvo.AttributeValueID(value)]; !ok {
return constants.QueryValueExpired, true
} else { } else {
return "", false if _, ok := def.EnumStrings[catvo.AttributeValueID(value)]; !ok {
errors = append(errors, constants.QueryValueExpired)
}
} }
} }
areValuesExpired := func(values []string, def catvo.AttributeDefinition) (constants.QueryError, bool) { areValuesExpired := func(values []string, def catvo.AttributeDefinition) {
if len(values) < 1 { if len(values) < 1 {
return constants.MissingQueryFieldValues, true errors = append(errors, constants.MissingQueryFieldValues)
} }
for _, v := range values { for _, v := range values {
if res, ok := isValueExpired(v, def); ok { isValueExpired(v, def)
return res, true
}
} }
return "", false
} }
for _, e := range query.Elements { for _, e := range query.Elements {
errorMessage := constants.QueryError("")
hasError := false
if e.Matcher != nil { if e.Matcher != nil {
if def, ok := attributes[e.Matcher.Attribute]; ok { if def, ok := attributes[e.Matcher.Attribute]; ok {
switch { switch {
case e.Matcher.StringIn != nil: case e.Matcher.StringIn != nil:
errorMessage, hasError = areValuesExpired(e.Matcher.StringIn.Values, def) areValuesExpired(e.Matcher.StringIn.Values, def)
case e.Matcher.StringAllIn != nil: case e.Matcher.StringAllIn != nil:
errorMessage, hasError = areValuesExpired(e.Matcher.StringAllIn.Values, def) areValuesExpired(e.Matcher.StringAllIn.Values, def)
case e.Matcher.StringNotIn != nil: case e.Matcher.StringNotIn != nil:
errorMessage, hasError = areValuesExpired(e.Matcher.StringNotIn.Values, def) areValuesExpired(e.Matcher.StringNotIn.Values, def)
case e.Matcher.StringEquals != nil: case e.Matcher.StringEquals != nil:
errorMessage, hasError = isValueExpired(e.Matcher.StringEquals.Value, def) isValueExpired(e.Matcher.StringEquals.Value, def)
case e.Matcher.StringNotEquals != nil: case e.Matcher.StringNotEquals != nil:
errorMessage, hasError = isValueExpired(e.Matcher.StringNotEquals.Value, def) isValueExpired(e.Matcher.StringNotEquals.Value, def)
default: default:
errorMessage, hasError = constants.MissingQueryCondition, true errors = append(errors, constants.MissingQueryCondition)
} }
} else { } else {
return constants.MissingQueryField, true errors = append(errors, constants.MissingQueryField)
} }
} }
if hasError {
return errorMessage, hasError
}
} }
return "", false
return errors
} }