diff --git a/constants/constants.go b/constants/constants.go index 2a5648b..6718a87 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -13,4 +13,11 @@ const ( 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" diff --git a/constants/vo.go b/constants/vo.go index 7846ec9..7c63a42 100644 --- a/constants/vo.go +++ b/constants/vo.go @@ -2,3 +2,4 @@ package constants type Severity string type Health string +type QueryError string diff --git a/validations/query.go b/validations/query.go index ee9747e..2782e30 100644 --- a/validations/query.go +++ b/validations/query.go @@ -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.MissingQuerryFieldValues, 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.QuerryValueExpired, 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.MissingQuerryFieldValues, 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.MissingQuerryCondition, 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.MissingQuerryField, true } + } - } else { - fmt.Println("MATCHER is NIL e.matcher: ") - return true + if hasError { + return errorMessage, hasError } } - return false + return "", false }