mirror of
https://github.com/foomo/contentfulvalidation.git
synced 2025-10-16 12:25:37 +00:00
feat: Update querry
This commit is contained in:
parent
18fb246f6b
commit
291f49393c
@ -13,4 +13,11 @@ const (
|
|||||||
HealthOk Health = "ok"
|
HealthOk Health = "ok"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MissingQuerryFieldValues QueryError = "Missing field values"
|
||||||
|
QuerryValueExpired QueryError = "Querry field value is expired"
|
||||||
|
MissingQuerryCondition QueryError = "Missing querry condition"
|
||||||
|
MissingQuerryField QueryError = "Querry Field is empty"
|
||||||
|
)
|
||||||
|
|
||||||
const DateFormat = "02 Jan 2006"
|
const DateFormat = "02 Jan 2006"
|
||||||
|
|||||||
@ -2,3 +2,4 @@ package constants
|
|||||||
|
|
||||||
type Severity string
|
type Severity string
|
||||||
type Health string
|
type Health string
|
||||||
|
type QueryError string
|
||||||
|
|||||||
@ -1,66 +1,63 @@
|
|||||||
package validations
|
package validations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
catvo "github.com/bestbytes/catalogue/vo"
|
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) (constants.QueryError, bool) {
|
||||||
isValueExpired := func(value string, def catvo.AttributeDefinition) bool {
|
if len(value) < 1 {
|
||||||
|
return constants.MissingQuerryFieldValues, true
|
||||||
|
}
|
||||||
if _, ok := def.EnumStrings[catvo.AttributeValueID(value)]; !ok {
|
if _, ok := def.EnumStrings[catvo.AttributeValueID(value)]; !ok {
|
||||||
// thow error
|
return constants.QuerryValueExpired, true
|
||||||
// provide contex on value with this id ....
|
|
||||||
fmt.Println("Attribute NOT found: ", ok)
|
|
||||||
return true
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Attribute found: ", ok)
|
return "", false
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
areValuesExpired := func(values []string, def catvo.AttributeDefinition) bool {
|
areValuesExpired := func(values []string, def catvo.AttributeDefinition) (constants.QueryError, bool) {
|
||||||
expired := false
|
if len(values) < 1 {
|
||||||
|
return constants.MissingQuerryFieldValues, true
|
||||||
|
}
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
if isValueExpired(v, def) {
|
if res, ok := isValueExpired(v, def); ok {
|
||||||
expired = true
|
return res, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return expired
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, e := range query.Elements {
|
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 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:
|
||||||
return areValuesExpired(e.Matcher.StringIn.Values, def)
|
errorMessage, hasError = areValuesExpired(e.Matcher.StringIn.Values, def)
|
||||||
case e.Matcher.StringAllIn != nil:
|
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:
|
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:
|
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:
|
case e.Matcher.StringNotEquals != nil:
|
||||||
return isValueExpired(e.Matcher.StringNotEquals.Value, def)
|
errorMessage, hasError = isValueExpired(e.Matcher.StringNotEquals.Value, def)
|
||||||
|
default:
|
||||||
|
errorMessage, hasError = constants.MissingQuerryCondition, true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// throw error
|
return constants.MissingQuerryField, true
|
||||||
fmt.Println("NO attribute within ALL ATTRIBUTES: ")
|
|
||||||
// TODO uncomment once catalogue attr are in, maybe a different validation for this
|
|
||||||
// return true
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
if hasError {
|
||||||
fmt.Println("MATCHER is NIL e.matcher: ")
|
return errorMessage, hasError
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user