Skip to content

Commit

Permalink
chore: relinted, updated (#162)
Browse files Browse the repository at this point in the history
* chore: relinted, updated

* updated go version to go1.19
* updated linting rules
* updated dependencies, upgraded yaml.v2 to yaml.v3
* relinted code
* doc: updated links and badges in README
* ci: updated github actions

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

* removed empty comment lines

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

---------

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
  • Loading branch information
fredbi committed Dec 7, 2023
1 parent b2d5112 commit 7bda92f
Show file tree
Hide file tree
Showing 33 changed files with 449 additions and 497 deletions.
47 changes: 0 additions & 47 deletions .github/workflows/ci.yaml

This file was deleted.

50 changes: 50 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: go test

on: [push, pull_request]

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: stable
check-latest: true
cache: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
only-new-issues: true

test:
name: Unit tests
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
go_version: ['oldstable', 'stable' ]

steps:
- name: Run unit tests
uses: actions/setup-go@v4
with:
go-version: '${{ matrix.go_version }}'
check-latest: true
cache: true

- uses: actions/checkout@v3

- run: go test -v -race -coverprofile="coverage-${{ matrix.os }}.${{ matrix.go_version }}.out" -covermode=atomic ./...

- name: Upload coverage to codecov
uses: codecov/codecov-action@v3
with:
files: './coverage-${{ matrix.os }}.${{ matrix.go_version }}.out'
flags: '${{ matrix.go_version }}'
os: '${{ matrix.os }}'
fail_ci_if_error: false
verbose: true
56 changes: 31 additions & 25 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
gocyclo:
min-complexity: 50
min-complexity: 45
maligned:
suggest-new: true
dupl:
threshold: 100
threshold: 200
goconst:
min-len: 2
min-occurrences: 3
Expand All @@ -15,41 +17,45 @@ linters:
enable-all: true
disable:
- maligned
- unparam
- lll
- gochecknoinits
- gochecknoglobals
- funlen
- godox
- gocognit
- whitespace
- wsl
- funlen
- gochecknoglobals
- gochecknoinits
- scopelint
- wrapcheck
- exhaustivestruct
- exhaustive
- nlreturn
- testpackage
- gci
- gofumpt
- goerr113
- nlreturn
- gomnd
- tparallel
- exhaustivestruct
- goerr113
- errorlint
- nestif
- godot
- tparallel
- gofumpt
- paralleltest
- cyclop # because we have gocyclo already
- depguard # we do not add a config for this
# TODO: review the linters below. We disabled them to make the CI pass first.
- nonamedreturns
- tparallel
- thelper
- ifshort
- exhaustruct
- nosnakecase
- nolintlint
- ireturn
- varnamelen
- gci
- depguard
- errchkjson
- inamedparam
- nonamedreturns
- musttag
- ireturn
- forcetypeassert
- thelper
# Disable deprecated linters.
# They will be removed from golangci-lint in future.
- cyclop
# deprecated linters
- deadcode
- interfacer
- golint
- scopelint
- varcheck
- structcheck
- golint
- nosnakecase
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Validation helpers
[![Build Status](https://travis-ci.org/go-openapi/validate.svg?branch=master)](https://travis-ci.org/go-openapi/validate)
[![Build status](https://ci.appveyor.com/api/projects/status/d6epy6vipueyh5fs/branch/master?svg=true)](https://ci.appveyor.com/project/fredbi/validate/branch/master)
[![codecov](https://codecov.io/gh/go-openapi/validate/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/validate)
# Validation helpers [![Build Status](https://github.com/go-openapi/validate/actions/workflows/go-test.yml/badge.svg)](https://github.com/go-openapi/validate/actions?query=workflow%3A"go+test") [![codecov](https://codecov.io/gh/go-openapi/validate/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/validate)

[![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io)
[![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/validate/master/LICENSE)
[![Go Reference](https://pkg.go.dev/badge/github.com/go-openapi/validate.svg)](https://pkg.go.dev/github.com/go-openapi/validate)
Expand All @@ -24,7 +22,7 @@ Reference can be found here: https://github.com/OAI/OpenAPI-Specification/blob/m
* Minimum, Maximum, MultipleOf
* FormatOf

[Documentation](https://godoc.org/github.com/go-openapi/validate)
[Documentation](https://pkg.go.dev/github.com/go-openapi/validate)

## FAQ

Expand Down
3 changes: 1 addition & 2 deletions debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package validate

import (
"io/ioutil"
"os"
"sync"
"testing"
Expand All @@ -32,7 +31,7 @@ func TestDebug(t *testing.T) {
skipNotify(t)
t.SkipNow()
}
tmpFile, _ := ioutil.TempFile("", "debug-test")
tmpFile, _ := os.CreateTemp("", "debug-test")
tmpName := tmpFile.Name()
defer func() {
Debug = false
Expand Down
2 changes: 0 additions & 2 deletions default_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func (d *defaultValidator) validateDefaultInResponse(resp *spec.Response, respon

responseName, responseCodeAsStr := responseHelp.responseMsgVariants(responseType, responseCode)

//nolint: dupl
if response.Headers != nil { // Safeguard
for nm, h := range response.Headers {
// reset explored schemas to get depth-first recursive-proof exploration
Expand Down Expand Up @@ -263,7 +262,6 @@ func (d *defaultValidator) validateDefaultValueSchemaAgainstSchema(path, in stri

// TODO: Temporary duplicated code. Need to refactor with examples

// nolint: dupl
func (d *defaultValidator) validateDefaultValueItemsAgainstSchema(path, in string, root interface{}, items *spec.Items) *Result {
res := new(Result)
s := d.SpecValidator
Expand Down
17 changes: 12 additions & 5 deletions default_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import (
"github.com/stretchr/testify/require"
)

const (
jsonExt = ".json"
hasErrorMsg = " should have errors"
noErrorMsg = " should not have errors"
hasWarningMsg = " should have warnings"
)

func TestDefault_ValidatePetStore(t *testing.T) {
doc, _ := loads.Analyzed(PetStoreJSONMessage, "")
validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
Expand Down Expand Up @@ -72,36 +79,36 @@ func TestDefault_ValidateDefaults(t *testing.T) {
}

for _, tt := range tests {
path := filepath.Join("fixtures", "validation", "default", "valid-default-value-"+tt+".json")
path := filepath.Join("fixtures", "validation", "default", "valid-default-value-"+tt+jsonExt)
if DebugTest {
t.Logf("Testing valid default values for: %s", path)
}
validator := makeSpecValidator(t, path)
myDefaultValidator := &defaultValidator{SpecValidator: validator}
res := myDefaultValidator.Validate()
assert.Empty(t, res.Errors, tt+" should not have errors")
assert.Empty(t, res.Errors, tt+noErrorMsg)

// Special case: warning only
if tt == "parameter-required" {
warns := verifiedTestWarnings(res)
assert.Contains(t, warns, "limit in query has a default value and is required as parameter")
}

path = filepath.Join("fixtures", "validation", "default", "invalid-default-value-"+tt+".json")
path = filepath.Join("fixtures", "validation", "default", "invalid-default-value-"+tt+jsonExt)
if DebugTest {
t.Logf("Testing invalid default values for: %s", path)
}

validator = makeSpecValidator(t, path)
myDefaultValidator = &defaultValidator{SpecValidator: validator}
res = myDefaultValidator.Validate()
assert.NotEmpty(t, res.Errors, tt+" should have errors")
assert.NotEmpty(t, res.Errors, tt+hasErrorMsg)

// Update: now we have an additional message to explain it's all about a default value
// Example:
// - default value for limit in query does not validate its Schema
// - limit in query must be of type integer: "string"]
assert.True(t, len(res.Errors) >= 1, tt+" should have at least 1 error")
assert.NotEmptyf(t, res.Errors, tt+" should have at least 1 error")
}
}

Expand Down
6 changes: 3 additions & 3 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package validate_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -196,7 +196,7 @@ func Test_Issue102_Circular(t *testing.T) {
filepath.Join("fixtures", "bugs", "123-validate", "fixture-123.json"),
} {
t.Run(fixture, func(t *testing.T) {
filebytes, err := ioutil.ReadFile(fixture)
filebytes, err := os.ReadFile(fixture)
require.NoError(t, err)

openAPIv2Doc := json.RawMessage(filebytes)
Expand All @@ -207,7 +207,7 @@ func Test_Issue102_Circular(t *testing.T) {
validator := validate.NewSpecValidator(doc.Schema(), strfmt.Default)
validator.SetContinueOnErrors(true)
res, _ := validator.Validate(doc)
require.Lenf(t, res.Errors, 0, "unexpected validation erorrs: %v", res.Errors)
require.Emptyf(t, res.Errors, "unexpected validation erorrs: %v", res.Errors)
})
}
}
4 changes: 2 additions & 2 deletions example_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ func (ex *exampleValidator) validateExampleInResponse(resp *spec.Response, respo

responseName, responseCodeAsStr := responseHelp.responseMsgVariants(responseType, responseCode)

// nolint: dupl
if response.Headers != nil { // Safeguard
for nm, h := range response.Headers {
// reset explored schemas to get depth-first recursive-proof exploration
Expand Down Expand Up @@ -250,7 +249,8 @@ func (ex *exampleValidator) validateExampleValueSchemaAgainstSchema(path, in str
}

// TODO: Temporary duplicated code. Need to refactor with examples
// nolint: dupl
//

func (ex *exampleValidator) validateExampleValueItemsAgainstSchema(path, in string, root interface{}, items *spec.Items) *Result {
res := new(Result)
s := ex.SpecValidator
Expand Down
12 changes: 6 additions & 6 deletions example_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestExample_ValidateExamples(t *testing.T) {
validator := makeSpecValidator(t, path)
myExampleValidator := &exampleValidator{SpecValidator: validator}
res := myExampleValidator.Validate()
assert.Empty(t, res.Errors, tt+" should not have errors")
assert.Empty(t, res.Errors, tt+noErrorMsg)
/*
// Special case: warning only
if tt == "parameter-required" {
Expand All @@ -90,17 +90,17 @@ func TestExample_ValidateExamples(t *testing.T) {
switch tt {
case "header-badpattern":
// This fixture exhibits real errors besides example values
assert.NotEmpty(t, res.Errors, tt+" should have errors")
assert.NotEmpty(t, res.Warnings, tt+" should have warnings")
assert.NotEmpty(t, res.Errors, tt+hasErrorMsg)
assert.NotEmpty(t, res.Warnings, tt+hasWarningMsg)
default:
assert.Empty(t, res.Errors, tt+" should not have errors")
assert.NotEmpty(t, res.Warnings, tt+" should have warnings")
assert.Empty(t, res.Errors, tt+noErrorMsg)
assert.NotEmpty(t, res.Warnings, tt+hasWarningMsg)
}
// Update: now we have an additional message to explain it's all about a default value
// Example:
// - default value for limit in query does not validate its Schema
// - limit in query must be of type integer: "string"]
assert.True(t, len(res.Warnings) >= 1, tt+" should have at least 1 warning")
assert.NotEmptyf(t, res.Warnings, tt+" should have at least 1 warning")

debugTest(t, path, res)
if DebugTest && t.Failed() {
Expand Down

0 comments on commit 7bda92f

Please sign in to comment.