Skip to content

Commit de09535

Browse files
authored
refactor: use go-validator's facility to customize messages (italia#209)
Use go-validator's translations, which is its preferred way to customize error messages and allows us to make them more user friendly and extensible.
1 parent b138ab8 commit de09535

File tree

4 files changed

+253
-113
lines changed

4 files changed

+253
-113
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module github.com/italia/publiccode-parser-go/v4
33
require (
44
github.com/alranel/go-vcsurl/v2 v2.0.2
55
github.com/dyatlov/go-oembed v0.0.0-20191103150536-a57c85b3b37c
6+
github.com/go-playground/locales v0.14.1
7+
github.com/go-playground/universal-translator v0.18.1
68
github.com/go-playground/validator/v10 v10.18.0
79
github.com/italia/httpclient-lib-go v0.0.2
810
github.com/kyoh86/go-spdx v0.0.5-0.20220518012447-4d195d3a5da1
@@ -13,8 +15,6 @@ require (
1315
require (
1416
github.com/Jeffail/gabs v1.4.0 // indirect
1517
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
16-
github.com/go-playground/locales v0.14.1 // indirect
17-
github.com/go-playground/universal-translator v0.18.1 // indirect
1818
github.com/leodido/go-urn v1.4.0 // indirect
1919
github.com/sirupsen/logrus v1.9.0 // indirect
2020
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect

parser.go

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import (
1515
"unicode/utf8"
1616

1717
"github.com/alranel/go-vcsurl/v2"
18+
"github.com/go-playground/locales/en"
19+
ut "github.com/go-playground/universal-translator"
1820
"github.com/go-playground/validator/v10"
21+
en_translations "github.com/go-playground/validator/v10/translations/en"
1922
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
2023
publiccodeValidator "github.com/italia/publiccode-parser-go/v4/validators"
2124
"gopkg.in/yaml.v3"
@@ -106,7 +109,7 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
106109

107110
_, version := getNodes("publiccodeYmlVersion", &node)
108111
if version == nil {
109-
return nil, ValidationResults{newValidationError("publiccodeYmlVersion", "required")}
112+
return nil, ValidationResults{newValidationError("publiccodeYmlVersion", "publiccodeYmlVersion is a required field")}
110113
}
111114
if version.ShortTag() != "!!str" {
112115
line, column := getPositionInFile("publiccodeYmlVersion", node)
@@ -176,38 +179,19 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
176179

177180
validate := publiccodeValidator.New()
178181

182+
en := en.New()
183+
uni := ut.New(en, en)
184+
185+
trans, _ := uni.GetTranslator("en")
186+
_ = en_translations.RegisterDefaultTranslations(validate, trans)
187+
_ = publiccodeValidator.RegisterLocalErrorMessages(validate, trans)
188+
179189
err = validate.Struct(publiccode)
180190
if err != nil {
181-
tagMap := map[string]string{
182-
"gt": "must be more than",
183-
"oneof": "must be one of the following:",
184-
"email": "must be a valid email",
185-
"date": "must be a date with format 'YYYY-MM-DD'",
186-
"umax": "must be less or equal than",
187-
"umin": "must be more or equal than",
188-
"url_http_url": "must be an HTTP URL",
189-
"url_url": "must be a valid URL",
190-
"is_category_v0": "must be a valid category",
191-
"is_scope_v0": "must be a valid scope",
192-
"is_italian_ipa_code": "must be a valid Italian Public Administration Code (iPA)",
193-
"iso3166_1_alpha2_lowercase": "must be a valid lowercase ISO 3166-1 alpha-2 two-letter country code",
194-
"bcp47_language_tag": "must be a valid BCP 47 language",
195-
"bcp47_keys": "must use a valid BCP 47 language",
196-
}
197191
for _, err := range err.(validator.ValidationErrors) {
198192
var sb strings.Builder
199193

200-
tag, ok := tagMap[err.ActualTag()]
201-
if !ok {
202-
tag = err.ActualTag()
203-
}
204-
205-
sb.WriteString(tag)
206-
207-
// condition parameters, e.g. oneof=red blue -> red blue
208-
if err.Param() != "" {
209-
sb.WriteString(" " + err.Param())
210-
}
194+
sb.WriteString(err.Translate(trans))
211195

212196
// TODO: find a cleaner way
213197
key := strings.Replace(

0 commit comments

Comments
 (0)