Skip to content

Commit 928e85c

Browse files
authored
refactor: add newValidationErrorf() function (#285)
1 parent 976ad40 commit 928e85c

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

errors.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ func (e ValidationError) MarshalJSON() ([]byte, error) {
4444
})
4545
}
4646

47-
func newValidationError(key string, description string, args ...interface{}) ValidationError {
48-
return ValidationError{Key: key, Description: fmt.Sprintf(description, args...)}
47+
func newValidationError(key string, description string) ValidationError {
48+
return ValidationError{Key: key, Description: description}
49+
}
50+
51+
func newValidationErrorf(key string, description string, args ...any) ValidationError {
52+
return newValidationError(key, fmt.Sprintf(description, args...))
4953
}
5054

5155
type ValidationWarning ValidationError

fields.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
2222

2323
if publiccodev0.URL != nil && network {
2424
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.URL), network); !reachable {
25-
vr = append(vr, newValidationError("url", "'%s' not reachable: %s", publiccodev0.URL, err.Error()))
25+
vr = append(vr, newValidationErrorf("url", "'%s' not reachable: %s", publiccodev0.URL, err.Error()))
2626
}
2727

2828
if !vcsurl.IsRepo((*url.URL)(publiccodev0.URL)) {
@@ -32,7 +32,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
3232

3333
if publiccodev0.LandingURL != nil {
3434
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.LandingURL), network); !reachable {
35-
vr = append(vr, newValidationError(
35+
vr = append(vr, newValidationErrorf(
3636
"landingURL",
3737
"'%s' not reachable: %s", publiccodev0.LandingURL, err.Error(),
3838
))
@@ -41,7 +41,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
4141

4242
if publiccodev0.Roadmap != nil {
4343
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.Roadmap), network); !reachable {
44-
vr = append(vr, newValidationError(
44+
vr = append(vr, newValidationErrorf(
4545
"roadmap",
4646
"'%s' not reachable: %s", publiccodev0.Roadmap, err.Error(),
4747
))
@@ -113,7 +113,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
113113
if !exists {
114114
u := toCodeHostingURL(*publiccodev0.Legal.AuthorsFile, parser.currentBaseURL)
115115

116-
vr = append(vr, newValidationError("legal.authorsFile", "'%s' does not exist: %s", urlutil.DisplayURL(&u), err.Error()))
116+
vr = append(vr, newValidationErrorf("legal.authorsFile", "'%s' does not exist: %s", urlutil.DisplayURL(&u), err.Error()))
117117
}
118118
}
119119
}
@@ -144,7 +144,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
144144

145145
if !parser.disableExternalChecks && network && desc.Documentation != nil {
146146
if reachable, err := parser.isReachable(*(*url.URL)(desc.Documentation), network); !reachable {
147-
vr = append(vr, newValidationError(
147+
vr = append(vr, newValidationErrorf(
148148
fmt.Sprintf("description.%s.documentation", lang),
149149
"'%s' not reachable: %s", desc.Documentation, err.Error(),
150150
))
@@ -153,7 +153,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
153153

154154
if !parser.disableExternalChecks && network && desc.APIDocumentation != nil {
155155
if reachable, err := parser.isReachable(*(*url.URL)(desc.APIDocumentation), network); !reachable {
156-
vr = append(vr, newValidationError(
156+
vr = append(vr, newValidationErrorf(
157157
fmt.Sprintf("description.%s.apiDocumentation", lang),
158158
"'%s' not reachable: %s", desc.APIDocumentation, err.Error(),
159159
))
@@ -167,7 +167,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
167167
} else if !parser.disableExternalChecks {
168168
isImage, err := parser.isImageFile(toCodeHostingURL(v, parser.currentBaseURL), network)
169169
if !isImage {
170-
vr = append(vr, newValidationError(
170+
vr = append(vr, newValidationErrorf(
171171
keyName,
172172
"'%s' is not an image: %s", v, err.Error(),
173173
))
@@ -178,7 +178,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
178178
for i, v := range desc.Videos {
179179
_, err := parser.isOEmbedURL((*url.URL)(v))
180180
if err != nil {
181-
vr = append(vr, newValidationError(
181+
vr = append(vr, newValidationErrorf(
182182
fmt.Sprintf("description.%s.videos[%d]", lang, i),
183183
"'%s' is not a valid video URL supporting oEmbed: %s", v, err.Error(),
184184
))

parser.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func NewDefaultParser() (*Parser, error) {
106106
func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) { //nolint:maintidx
107107
b, err := io.ReadAll(in)
108108
if err != nil {
109-
return nil, ValidationResults{newValidationError("", fmt.Sprintf("Can't read the stream: %v", err))}
109+
return nil, ValidationResults{newValidationErrorf("", "Can't read the stream: %v", err)}
110110
}
111111

112112
if !utf8.Valid(b) {
@@ -146,11 +146,10 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) { //nolint:mainti
146146

147147
if !slices.Contains(SupportedVersions, version.Value) {
148148
return nil, ValidationResults{
149-
newValidationError("publiccodeYmlVersion", fmt.Sprintf(
149+
newValidationErrorf("publiccodeYmlVersion",
150150
"unsupported version: '%s'. Supported versions: %s",
151151
version.Value,
152-
strings.Join(SupportedVersions, ", "),
153-
)),
152+
strings.Join(SupportedVersions, ", ")),
154153
}
155154
}
156155

@@ -280,7 +279,7 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) { //nolint:mainti
280279
if p.currentBaseURL == nil {
281280
cwd, err := os.Getwd()
282281
if err != nil {
283-
ve = append(ve, newValidationError("", fmt.Sprintf("no baseURL set and failed to get working directory: %s", err)))
282+
ve = append(ve, newValidationErrorf("", "no baseURL set and failed to get working directory: %s", err))
284283

285284
return asPublicCode(publiccode), ve
286285
}

0 commit comments

Comments
 (0)