diff --git a/go.mod b/go.mod index 8957f7a..32fd20c 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/casbin/casbin/v2 v2.77.2 github.com/coreos/go-oidc/v3 v3.6.0 github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/go-playground/validator/v10 v10.15.5 github.com/gorilla/context v1.1.1 github.com/gorilla/mux v1.8.0 github.com/microcosm-cc/bluemonday v1.0.25 @@ -31,10 +32,10 @@ require ( github.com/BurntSushi/toml v0.3.1 // indirect github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect github.com/aymerick/douceur v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/go-jose/go-jose/v3 v3.0.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator v9.31.0+incompatible // indirect github.com/go-resty/resty/v2 v2.7.0 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect diff --git a/go.sum b/go.sum index 97b78e8..354ad64 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.100.2 h1:t9Iw5QH5v4XtlEQaCtUY7x6sCABps8sW0acw7e2WQ6Y= cloud.google.com/go v0.100.2/go.mod h1:4Xra9TjzAeYHrl5+oeLlzbM2k3mjVhZh4UqTZ//w99A= +cloud.google.com/go v0.110.4 h1:1JYyxKMN9hd5dR2MYTPWkGUgcoxVVhg0LKNKEo0qvmk= +cloud.google.com/go v0.110.4/go.mod h1:+EYjdK8e5RME/VY/qLCAtuyALQ9q67dvuum8i+H5xsI= cloud.google.com/go/compute v0.1.0/go.mod h1:GAesmwr110a34z04OlxYkATPBEfVhkymfTBXtfbBFow= cloud.google.com/go/compute v1.20.1 h1:6aKEtlUiwEpJzM001l0yFkpXmUVXaN8W+fbkb2AZNbg= cloud.google.com/go/compute v1.20.1/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM= @@ -56,15 +60,18 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.m github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-jose/go-jose/v3 v3.0.0 h1:s6rrhirfEP/CGIoc6p+PZAeogN2SxKav6Wp7+dyMWVo= github.com/go-jose/go-jose/v3 v3.0.0/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA= -github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig= +github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7NLylN+x8TTueE24= +github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= diff --git a/src/v2/handler/dataagreement/config_create_dataagreement.go b/src/v2/handler/dataagreement/config_create_dataagreement.go index 9246abd..f5b36ed 100644 --- a/src/v2/handler/dataagreement/config_create_dataagreement.go +++ b/src/v2/handler/dataagreement/config_create_dataagreement.go @@ -14,7 +14,7 @@ import ( "github.com/bb-consent/api/src/v2/dataagreement" "github.com/bb-consent/api/src/v2/revision" "github.com/bb-consent/api/src/v2/token" - "github.com/go-playground/validator" + "github.com/go-playground/validator/v10" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -67,28 +67,28 @@ var MethodOfUseMappings = []MethodOfUseMapping{ type policyForDataAgreement struct { policy.Policy Id string `json:"id"` - Name string `json:"name" validate:"requiredWhenActive"` - Url string `json:"url" validate:"requiredWhenActive"` + Name string `json:"name" validate:"required_if=Active true"` + Url string `json:"url" validate:"required_if=Active true"` } type dataAgreement struct { Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` Version string `json:"version"` ControllerId string `json:"controllerId"` - ControllerUrl string `json:"controllerUrl" validate:"requiredWhenActive"` - ControllerName string `json:"controllerName" validate:"requiredWhenActive"` - Policy policyForDataAgreement `json:"policy" validate:"requiredWhenActive"` - Purpose string `json:"purpose" validate:"requiredWhenActive"` - PurposeDescription string `json:"purposeDescription" validate:"requiredWhenActive,max=500"` - LawfulBasis string `json:"lawfulBasis" validate:"requiredWhenActive"` - MethodOfUse string `json:"methodOfUse" validate:"requiredWhenActive"` + ControllerUrl string `json:"controllerUrl" validate:"required_if=Active true"` + ControllerName string `json:"controllerName" validate:"required_if=Active true"` + Policy policyForDataAgreement `json:"policy" validate:"required_if=Active true"` + Purpose string `json:"purpose" validate:"required_if=Active true"` + PurposeDescription string `json:"purposeDescription" validate:"required_if=Active true,max=500"` + LawfulBasis string `json:"lawfulBasis" validate:"required_if=Active true"` + MethodOfUse string `json:"methodOfUse" validate:"required_if=Active true"` DpiaDate string `json:"dpiaDate"` DpiaSummaryUrl string `json:"dpiaSummaryUrl"` - Signature dataagreement.Signature `json:"signature" validate:"requiredWhenActive"` + Signature dataagreement.Signature `json:"signature" validate:"required_if=Active true"` Active bool `json:"active"` Forgettable bool `json:"forgettable"` CompatibleWithVersionId string `json:"compatibleWithVersionId"` - Lifecycle string `json:"lifecycle" validate:"requiredWhenActive"` + Lifecycle string `json:"lifecycle" validate:"required_if=Active true"` OrganisationId string `json:"-"` IsDeleted bool `json:"-"` } @@ -128,25 +128,9 @@ func isValidMethodOfUse(methodOfUse string) bool { return isFound } -func registerCustomValidation(validate *validator.Validate) { - if err := validate.RegisterValidation("requiredWhenActive", func(fl validator.FieldLevel) bool { - dataAgreement := fl.Top().Interface().(dataAgreement) - - // If "active" is true, require the field - if dataAgreement.Active { - return fl.Field().String() != "" - } - - return true - - }); err != nil { - panic(err) - } -} - func validateAddDataAgreementRequestBody(dataAgreementReq addDataAgreementReq) error { var validate = validator.New() - registerCustomValidation(validate) + if err := validate.Struct(dataAgreementReq.DataAgreement); err != nil { return err }