From de1f3a908ee8c58aa63563beb8e9cb4f3eb8878e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Wed, 8 Jun 2022 14:04:21 +0200 Subject: [PATCH] Make value validators properly translate error messages --- compose/service/values/validator.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/compose/service/values/validator.go b/compose/service/values/validator.go index 97dc33644..7169be250 100644 --- a/compose/service/values/validator.go +++ b/compose/service/values/validator.go @@ -3,13 +3,15 @@ package values import ( "context" "fmt" - "github.com/cortezaproject/corteza-server/pkg/locale" "math/big" "net/mail" "net/url" + "strconv" "strings" "time" + "github.com/cortezaproject/corteza-server/pkg/locale" + "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/expr" "github.com/cortezaproject/corteza-server/pkg/slice" @@ -29,6 +31,7 @@ type ( localeService interface { T(ctx context.Context, ns, key string, rr ...string) string + TResource(ctx context.Context, ns, key string, rr ...string) string } validator struct { @@ -215,7 +218,7 @@ fields: } if len(f.Expressions.Validators) > 0 { - for _, cv := range f.Expressions.Validators { + for i, cv := range f.Expressions.Validators { eval, err := valParser.NewEvaluable(cv.Test) if err != nil { out.Push(makeInternalErr(f, err)) @@ -234,9 +237,23 @@ fields: } if invalid { + message := cv.Error + + // In case tests mit this for easier testing; falling back to whatever + // the field validator has stored. + if vldtr.localeSvc != nil { + validatorID := locale.ContentID(cv.ValidatorID, i) + rpl := strings.NewReplacer( + "{{validatorID}}", strconv.FormatUint(validatorID, 10), + ) + + k := rpl.Replace(types.LocaleKeyModuleFieldExpressionValidatorValidatorIDError.Path) + message = vldtr.localeSvc.TResource(ctx, f.ResourceTranslation(), k) + } + out.Push(types.RecordValueError{ Kind: "error", - Message: cv.Error, + Message: message, Meta: map[string]interface{}{"field": f.Name}}, )