diff --git a/errors.go b/errors.go index 75ba087..12a1b5c 100644 --- a/errors.go +++ b/errors.go @@ -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 @@ -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. diff --git a/errors_test.go b/errors_test.go index 92a818a..1a0ba82 100644 --- a/errors_test.go +++ b/errors_test.go @@ -3,6 +3,7 @@ package publiccode import ( "encoding/json" "errors" + "fmt" "net/url" "testing" ) @@ -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) + } + }) + } +} diff --git a/example_test.go b/example_test.go index fb80457..bd21899 100644 --- a/example_test.go +++ b/example_test.go @@ -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 diff --git a/parser.go b/parser.go index 98f4af3..d2afda8 100644 --- a/parser.go +++ b/parser.go @@ -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. @@ -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) { diff --git a/publiccode-parser/publiccode_parser.go b/publiccode-parser/publiccode_parser.go index 1131e19..4c1805c 100644 --- a/publiccode-parser/publiccode_parser.go +++ b/publiccode-parser/publiccode_parser.go @@ -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