diff --git a/default_validator.go b/default_validator.go index 215dc29..e0dd938 100644 --- a/default_validator.go +++ b/default_validator.go @@ -168,7 +168,7 @@ func (d *defaultValidator) validateDefaultValueValidAgainstSchema() *Result { // reset explored schemas to get depth-first recursive-proof exploration d.resetVisited() for nm, sch := range s.spec.Spec().Definitions { - res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("definitions.%s", nm), "body", &sch)) //#nosec + res.Merge(d.validateDefaultValueSchemaAgainstSchema("definitions."+nm, "body", &sch)) //#nosec } } return res @@ -263,7 +263,7 @@ func (d *defaultValidator) validateDefaultValueSchemaAgainstSchema(path, in stri } if schema.AdditionalItems != nil && schema.AdditionalItems.Schema != nil { // NOTE: we keep validating values, even though additionalItems is not supported by Swagger 2.0 (and 3.0 as well) - res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalItems", path), in, schema.AdditionalItems.Schema)) + res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+".additionalItems", in, schema.AdditionalItems.Schema)) } for propName, prop := range schema.Properties { res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+"."+propName, in, &prop)) //#nosec @@ -272,7 +272,7 @@ func (d *defaultValidator) validateDefaultValueSchemaAgainstSchema(path, in stri res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+"."+propName, in, &prop)) //#nosec } if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil { - res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalProperties", path), in, schema.AdditionalProperties.Schema)) + res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+".additionalProperties", in, schema.AdditionalProperties.Schema)) } if schema.AllOf != nil { for i, aoSch := range schema.AllOf { diff --git a/example_validator.go b/example_validator.go index 7f202c9..d089569 100644 --- a/example_validator.go +++ b/example_validator.go @@ -146,7 +146,7 @@ func (ex *exampleValidator) validateExampleValueValidAgainstSchema() *Result { // reset explored schemas to get depth-first recursive-proof exploration ex.resetVisited() for nm, sch := range s.spec.Spec().Definitions { - res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("definitions.%s", nm), "body", &sch)) //#nosec + res.Merge(ex.validateExampleValueSchemaAgainstSchema("definitions."+nm, "body", &sch)) //#nosec } } return res @@ -256,7 +256,7 @@ func (ex *exampleValidator) validateExampleValueSchemaAgainstSchema(path, in str } if schema.AdditionalItems != nil && schema.AdditionalItems.Schema != nil { // NOTE: we keep validating values, even though additionalItems is unsupported in Swagger 2.0 (and 3.0 as well) - res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalItems", path), in, schema.AdditionalItems.Schema)) + res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+".additionalItems", in, schema.AdditionalItems.Schema)) } for propName, prop := range schema.Properties { res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+"."+propName, in, &prop)) //#nosec @@ -265,7 +265,7 @@ func (ex *exampleValidator) validateExampleValueSchemaAgainstSchema(path, in str res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+"."+propName, in, &prop)) //#nosec } if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil { - res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalProperties", path), in, schema.AdditionalProperties.Schema)) + res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+".additionalProperties", in, schema.AdditionalProperties.Schema)) } if schema.AllOf != nil { for i, aoSch := range schema.AllOf { diff --git a/helpers.go b/helpers.go index 5c3dd76..757e403 100644 --- a/helpers.go +++ b/helpers.go @@ -233,7 +233,7 @@ func (h *paramHelper) safeExpandedParamsFor(path, method, operationID string, re operation.Parameters = resolvedParams for _, ppr := range s.expandedAnalyzer().SafeParamsFor(method, path, - func(p spec.Parameter, err error) bool { + func(_ spec.Parameter, err error) bool { // since params have already been expanded, there are few causes for error res.AddErrors(someParametersBrokenMsg(path, method, operationID)) // original error from analyzer diff --git a/helpers_test.go b/helpers_test.go index ec10cca..33caa38 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -15,7 +15,7 @@ package validate import ( - "fmt" + "errors" "testing" "github.com/stretchr/testify/assert" @@ -24,7 +24,7 @@ import ( func TestHelpers_addPointerError(t *testing.T) { res := new(Result) - r := errorHelp.addPointerError(res, fmt.Errorf("my error"), "my ref", "path") + r := errorHelp.addPointerError(res, errors.New("my error"), "my ref", "path") require.NotEmpty(t, r.Errors) msg := r.Errors[0].Error() assert.Contains(t, msg, "could not resolve reference in path to $ref my ref: my error") diff --git a/messages_test.go b/messages_test.go index eb73b3f..2ec5375 100644 --- a/messages_test.go +++ b/messages_test.go @@ -211,7 +211,7 @@ func checkMustHalt(t *testing.T, haltOnErrors bool) { } func testWalkSpecs(t *testing.T, tested ExpectedMap, haltOnErrors, continueOnErrors bool) filepath.WalkFunc { - return func(path string, info os.FileInfo, err error) error { + return func(path string, info os.FileInfo, _ error) error { thisTest, found := tested.Get(info.Name()) if info.IsDir() || !found { // skip @@ -255,6 +255,7 @@ func recapTest(t *testing.T, config ExpectedMap) { } } func reportTest(t *testing.T, path string, res *Result, expectedMessages []ExpectedMessage, msgtype string, continueOnErrors bool) { + const expected = "Expected " // Prints out a recap of error messages. To be enabled during development / test iterations verifiedErrors := make([]string, 0, 50) lines := make([]string, 0, 50) @@ -263,17 +264,17 @@ func reportTest(t *testing.T, path string, res *Result, expectedMessages []Expec } t.Logf("DEVMODE:Recap of returned %s messages while validating %s ", msgtype, path) for _, v := range verifiedErrors { - status := fmt.Sprintf("Unexpected %s", msgtype) + status := "Unexpected " + msgtype for _, s := range expectedMessages { if (s.WithContinueOnErrors && continueOnErrors) || !s.WithContinueOnErrors { if s.IsRegexp { if matched, _ := regexp.MatchString(s.Message, v); matched { - status = fmt.Sprintf("Expected %s", msgtype) + status = expected + msgtype break } } else { if strings.Contains(v, s.Message) { - status = fmt.Sprintf("Expected %s", msgtype) + status = expected + msgtype break } } @@ -284,21 +285,21 @@ func reportTest(t *testing.T, path string, res *Result, expectedMessages []Expec for _, s := range expectedMessages { if (s.WithContinueOnErrors && continueOnErrors) || !s.WithContinueOnErrors { - status := fmt.Sprintf("Missing %s", msgtype) + status := "Missing " + msgtype for _, v := range verifiedErrors { if s.IsRegexp { if matched, _ := regexp.MatchString(s.Message, v); matched { - status = fmt.Sprintf("Expected %s", msgtype) + status = expected + msgtype break } } else { if strings.Contains(v, s.Message) { - status = fmt.Sprintf("Expected %s", msgtype) + status = expected + msgtype break } } } - if status != fmt.Sprintf("Expected %s", msgtype) { + if status != expected+msgtype { lines = append(lines, fmt.Sprintf("[%s]%s", status, s.Message)) } } diff --git a/result.go b/result.go index 7e09182..ca45b00 100644 --- a/result.go +++ b/result.go @@ -15,7 +15,7 @@ package validate import ( - "fmt" + stderrors "errors" "reflect" "strings" @@ -379,13 +379,13 @@ func (r *Result) keepRelevantErrors() *Result { strippedErrors := []error{} for _, e := range r.Errors { if strings.HasPrefix(e.Error(), "IMPORTANT!") { - strippedErrors = append(strippedErrors, fmt.Errorf(strings.TrimPrefix(e.Error(), "IMPORTANT!"))) + strippedErrors = append(strippedErrors, stderrors.New(strings.TrimPrefix(e.Error(), "IMPORTANT!"))) } } strippedWarnings := []error{} for _, e := range r.Warnings { if strings.HasPrefix(e.Error(), "IMPORTANT!") { - strippedWarnings = append(strippedWarnings, fmt.Errorf(strings.TrimPrefix(e.Error(), "IMPORTANT!"))) + strippedWarnings = append(strippedWarnings, stderrors.New(strings.TrimPrefix(e.Error(), "IMPORTANT!"))) } } var strippedResult *Result diff --git a/result_test.go b/result_test.go index 9bdccab..2a7f61d 100644 --- a/result_test.go +++ b/result_test.go @@ -15,7 +15,7 @@ package validate import ( - "fmt" + "errors" "testing" "github.com/stretchr/testify/assert" @@ -25,16 +25,16 @@ import ( // Test AddError() uniqueness func TestResult_AddError(t *testing.T) { r := Result{} - r.AddErrors(fmt.Errorf("one error")) - r.AddErrors(fmt.Errorf("another error")) - r.AddErrors(fmt.Errorf("one error")) - r.AddErrors(fmt.Errorf("one error")) - r.AddErrors(fmt.Errorf("one error")) - r.AddErrors(fmt.Errorf("one error"), fmt.Errorf("another error")) + r.AddErrors(errors.New("one error")) + r.AddErrors(errors.New("another error")) + r.AddErrors(errors.New("one error")) + r.AddErrors(errors.New("one error")) + r.AddErrors(errors.New("one error")) + r.AddErrors(errors.New("one error"), errors.New("another error")) assert.Len(t, r.Errors, 2) - assert.Contains(t, r.Errors, fmt.Errorf("one error")) - assert.Contains(t, r.Errors, fmt.Errorf("another error")) + assert.Contains(t, r.Errors, errors.New("one error")) + assert.Contains(t, r.Errors, errors.New("another error")) } func TestResult_AddNilError(t *testing.T) { @@ -42,26 +42,26 @@ func TestResult_AddNilError(t *testing.T) { r.AddErrors(nil) assert.Empty(t, r.Errors) - errArray := []error{fmt.Errorf("one Error"), nil, fmt.Errorf("another error")} + errArray := []error{errors.New("one Error"), nil, errors.New("another error")} r.AddErrors(errArray...) assert.Len(t, r.Errors, 2) } func TestResult_AddWarnings(t *testing.T) { r := Result{} - r.AddErrors(fmt.Errorf("one Error")) + r.AddErrors(errors.New("one Error")) assert.Len(t, r.Errors, 1) assert.Empty(t, r.Warnings) - r.AddWarnings(fmt.Errorf("one Warning")) + r.AddWarnings(errors.New("one Warning")) assert.Len(t, r.Errors, 1) assert.Len(t, r.Warnings, 1) } func TestResult_Merge(t *testing.T) { r := Result{} - r.AddErrors(fmt.Errorf("one Error")) - r.AddWarnings(fmt.Errorf("one Warning")) + r.AddErrors(errors.New("one Error")) + r.AddWarnings(errors.New("one Warning")) r.Inc() assert.Len(t, r.Errors, 1) assert.Len(t, r.Warnings, 1) @@ -69,8 +69,8 @@ func TestResult_Merge(t *testing.T) { // Merge with same r2 := Result{} - r2.AddErrors(fmt.Errorf("one Error")) - r2.AddWarnings(fmt.Errorf("one Warning")) + r2.AddErrors(errors.New("one Error")) + r2.AddWarnings(errors.New("one Warning")) r2.Inc() r.Merge(&r2) @@ -81,8 +81,8 @@ func TestResult_Merge(t *testing.T) { // Merge with new r3 := Result{} - r3.AddErrors(fmt.Errorf("new Error")) - r3.AddWarnings(fmt.Errorf("new Warning")) + r3.AddErrors(errors.New("new Error")) + r3.AddWarnings(errors.New("new Warning")) r3.Inc() r.Merge(&r3) @@ -94,20 +94,20 @@ func TestResult_Merge(t *testing.T) { func errorFixture() (Result, Result, Result) { r := Result{} - r.AddErrors(fmt.Errorf("one Error")) - r.AddWarnings(fmt.Errorf("one Warning")) + r.AddErrors(errors.New("one Error")) + r.AddWarnings(errors.New("one Warning")) r.Inc() // same r2 := Result{} - r2.AddErrors(fmt.Errorf("one Error")) - r2.AddWarnings(fmt.Errorf("one Warning")) + r2.AddErrors(errors.New("one Error")) + r2.AddWarnings(errors.New("one Warning")) r2.Inc() // new r3 := Result{} - r3.AddErrors(fmt.Errorf("new Error")) - r3.AddWarnings(fmt.Errorf("new Warning")) + r3.AddErrors(errors.New("new Error")) + r3.AddWarnings(errors.New("new Warning")) r3.Inc() return r, r2, r3 } @@ -144,11 +144,11 @@ func TestResult_IsValid(t *testing.T) { assert.True(t, r.IsValid()) assert.False(t, r.HasErrors()) - r.AddWarnings(fmt.Errorf("one Warning")) + r.AddWarnings(errors.New("one Warning")) assert.True(t, r.IsValid()) assert.False(t, r.HasErrors()) - r.AddErrors(fmt.Errorf("one Error")) + r.AddErrors(errors.New("one Error")) assert.False(t, r.IsValid()) assert.True(t, r.HasErrors()) } @@ -158,10 +158,10 @@ func TestResult_HasWarnings(t *testing.T) { assert.False(t, r.HasWarnings()) - r.AddErrors(fmt.Errorf("one Error")) + r.AddErrors(errors.New("one Error")) assert.False(t, r.HasWarnings()) - r.AddWarnings(fmt.Errorf("one Warning")) + r.AddWarnings(errors.New("one Warning")) assert.True(t, r.HasWarnings()) } @@ -171,10 +171,10 @@ func TestResult_HasErrorsOrWarnings(t *testing.T) { assert.False(t, r.HasErrorsOrWarnings()) - r.AddErrors(fmt.Errorf("one Error")) + r.AddErrors(errors.New("one Error")) assert.True(t, r.HasErrorsOrWarnings()) - r2.AddWarnings(fmt.Errorf("one Warning")) + r2.AddWarnings(errors.New("one Warning")) assert.True(t, r2.HasErrorsOrWarnings()) r.Merge(&r2) @@ -183,10 +183,10 @@ func TestResult_HasErrorsOrWarnings(t *testing.T) { func TestResult_keepRelevantErrors(t *testing.T) { r := Result{} - r.AddErrors(fmt.Errorf("one Error")) - r.AddErrors(fmt.Errorf("IMPORTANT!Another Error")) - r.AddWarnings(fmt.Errorf("one warning")) - r.AddWarnings(fmt.Errorf("IMPORTANT!Another warning")) + r.AddErrors(errors.New("one Error")) + r.AddErrors(errors.New("IMPORTANT!Another Error")) + r.AddWarnings(errors.New("one warning")) + r.AddWarnings(errors.New("IMPORTANT!Another warning")) assert.Len(t, r.keepRelevantErrors().Errors, 1) assert.Len(t, r.keepRelevantErrors().Warnings, 1) } @@ -194,8 +194,8 @@ func TestResult_keepRelevantErrors(t *testing.T) { func TestResult_AsError(t *testing.T) { r := Result{} require.NoError(t, r.AsError()) - r.AddErrors(fmt.Errorf("one Error")) - r.AddErrors(fmt.Errorf("additional Error")) + r.AddErrors(errors.New("one Error")) + r.AddErrors(errors.New("additional Error")) res := r.AsError() require.Error(t, res) diff --git a/swagger_test.go b/swagger_test.go index 3c9908d..7bbe61c 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -128,7 +128,7 @@ func wantSwaggerTest(info os.FileInfo) bool { // Just validates all fixtures in ./fixtures/go-swagger (excluded codegen cases) func testGoSwaggerSpecs(t *testing.T, path string, expectToFail, expectToFailOnLoad map[string]bool, haltOnErrors bool) { err := filepath.Walk(path, - func(path string, info os.FileInfo, err error) error { + func(path string, info os.FileInfo, _ error) error { t.Run(path, func(t *testing.T) { if !DebugTest { // when running in dev mode, run serially t.Parallel()