Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func (e ValidationWarning) MarshalJSON() ([]byte, error) {

// ValidationResults collects [ValidationError] and [ValidationWarning] values.
// [Parser.Parse] and [Parser.ParseStream] return it as a non-nil error even when
// it contains only warnings. Inspect each element to determine its severity.
// it contains only warnings. Use [ValidationResults.HasErrors] to tell whether the
// file is valid, or inspect each element to determine its severity.
// ValidationResults does not unwrap its elements for errors.Is or errors.As.
type ValidationResults []error //nolint:errname // intentionally named as a collection, not an error type

Expand All @@ -124,6 +125,16 @@ func (vr ValidationResults) Error() string {
return strings.Join(s, "\n")
}

// HasErrors reports whether vr contains a [ValidationError]. A file whose
// results hold only [ValidationWarning] values is valid.
func (vr ValidationResults) HasErrors() bool {
return slices.ContainsFunc(vr, func(err error) bool {
_, ok := errors.AsType[ValidationError](err)

return ok
})
}

// sortByPosition orders diagnostics as they appear in the file. Some checks
// walk maps such as description, whose iteration order changes from one run
// to the next. Diagnostics on the same position keep their relative order.
Expand Down
27 changes: 27 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package publiccode
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"testing"
)
Expand Down Expand Up @@ -154,3 +155,29 @@ func TestNewNotImageErrorNamesURLOnce(t *testing.T) {
}
}
}

func TestValidationResultsHasErrors(t *testing.T) {
warning := ValidationWarning{Key: "foo", Description: "minor warning"}
validationError := ValidationError{Key: "bar", Description: "bad field"}

tests := []struct {
name string
results ValidationResults
want bool
}{
{"nil", nil, false},
{"only warnings", ValidationResults{warning, warning}, false},
{"one error", ValidationResults{validationError}, true},
{"error after a warning", ValidationResults{warning, validationError}, true},
{"wrapped error", ValidationResults{fmt.Errorf("wrapped: %w", validationError)}, true},
{"other error type", ValidationResults{errors.New("plain")}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.results.HasErrors(); got != tt.want {
t.Errorf("HasErrors() = %v, want %v", got, tt.want)
}
})
}
}
33 changes: 13 additions & 20 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,24 @@ func ExampleParser_ParseStream_warnings() {
}

pc, err := parser.ParseStream(strings.NewReader(input))
valid := pc != nil
if err != nil {
results, ok := errors.AsType[publiccode.ValidationResults](err)
if !ok {
fmt.Println(err)
results, ok := errors.AsType[publiccode.ValidationResults](err)
if err != nil && !ok {
fmt.Println(err)

return
}
return
}

for _, result := range results {
var diagnostic publiccode.ValidationWarning
var validationError publiccode.ValidationError
switch {
case errors.As(result, &diagnostic):
fmt.Println("warning:", diagnostic.Key)
case errors.As(result, &validationError):
fmt.Println("error:", validationError.Key)
valid = false
default:
fmt.Println(result)
valid = false
}
for _, result := range results {
if warning, ok := errors.AsType[publiccode.ValidationWarning](result); ok {
fmt.Println("warning:", warning.Key)
} else {
fmt.Println(result)
}
}

// Warnings alone leave the file valid.
valid := pc != nil && !results.HasErrors()

fmt.Println("valid:", valid)
// Output:
// warning: publiccodeYmlVersion
Expand Down
10 changes: 6 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ func NewDefaultParser() (*Parser, error) {

// ParseStream reads and validates a publiccode.yml file from in.
// It returns a non-nil [ValidationResults] error when validation produces errors
// or warnings, including when there are only warnings. Inspect each result for
// [ValidationError] or [ValidationWarning] to distinguish them.
// or warnings, including when there are only warnings. Call
// [ValidationResults.HasErrors] to tell whether the file is valid, or inspect
// each result for [ValidationError] or [ValidationWarning] to distinguish them.
//
// The returned [PublicCode] may be nil if parsing fails before decoding.
// A non-nil PublicCode does not imply that validation succeeded.
Expand All @@ -188,8 +189,9 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
// Use [Parser.ParseStream] to parse data from an io.Reader.
//
// Like ParseStream, Parse returns a non-nil [ValidationResults] error for
// warnings as well as validation errors. Inspect each result for [ValidationError]
// or [ValidationWarning] before deciding whether to accept the returned [PublicCode].
// warnings as well as validation errors. Call [ValidationResults.HasErrors], or
// inspect each result for [ValidationError] or [ValidationWarning], before deciding
// whether to accept the returned [PublicCode].
// A non-nil PublicCode does not imply that validation succeeded. Errors opening
// or fetching the file return a nil PublicCode and an error outside ValidationResults.
func (p *Parser) Parse(uri string) (PublicCode, error) {
Expand Down
8 changes: 1 addition & 7 deletions publiccode-parser/publiccode_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,7 @@ func hasValidationErrors(results error) bool {
}

if vr, ok := errors.AsType[publiccode.ValidationResults](results); ok {
for _, res := range vr {
if _, ok := errors.AsType[publiccode.ValidationError](res); ok {
return true
}
}

return false
return vr.HasErrors()
}

return true
Expand Down