Skip to content

Commit 82e6077

Browse files
committed
update go version to 1.18. Fix lint
Signed-off-by: Youyuan Wu <youyuanwu@outlook.com>
1 parent 625cad1 commit 82e6077

19 files changed

+105
-67
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141

4242
steps:
4343
- uses: actions/checkout@v2
44-
- uses: golangci/golangci-lint-action@v2
44+
- uses: golangci/golangci-lint-action@v3
4545
with:
46+
version: v1.53
4647
args: --timeout=5m

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ linters:
3939
- tparallel
4040
- paralleltest
4141
- cyclop # because we have gocyclo already
42+
- depguard # we do not add a config for this
4243
# TODO: review the linters below. We disabled them to make the CI pass first.
44+
- nonamedreturns
45+
- exhaustruct
46+
- nosnakecase
47+
- nolintlint
4348
- ireturn
4449
- varnamelen
4550
- forcetypeassert

default_validator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (d *defaultValidator) validateDefaultInResponse(resp *spec.Response, respon
170170

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

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

264264
// TODO: Temporary duplicated code. Need to refactor with examples
265+
265266
// nolint: dupl
266267
func (d *defaultValidator) validateDefaultValueItemsAgainstSchema(path, in string, root interface{}, items *spec.Items) *Result {
267268
res := new(Result)

doc.go

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ as well as tools to validate data against their schema.
1919
This package follows Swagger 2.0. specification (aka OpenAPI 2.0). Reference
2020
can be found here: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.
2121
22-
Validating a specification
22+
# Validating a specification
2323
2424
Validates a spec document (from JSON or YAML) against the JSON schema for swagger,
2525
then checks a number of extra rules that can't be expressed in JSON schema.
@@ -30,34 +30,36 @@ Entry points:
3030
- SpecValidator.Validate()
3131
3232
Reported as errors:
33-
[x] definition can't declare a property that's already defined by one of its ancestors
34-
[x] definition's ancestor can't be a descendant of the same model
35-
[x] path uniqueness: each api path should be non-verbatim (account for path param names) unique per method
36-
[x] each security reference should contain only unique scopes
37-
[x] each security scope in a security definition should be unique
38-
[x] parameters in path must be unique
39-
[x] each path parameter must correspond to a parameter placeholder and vice versa
40-
[x] each referenceable definition must have references
41-
[x] each definition property listed in the required array must be defined in the properties of the model
42-
[x] each parameter should have a unique `name` and `type` combination
43-
[x] each operation should have only 1 parameter of type body
44-
[x] each reference must point to a valid object
45-
[x] every default value that is specified must validate against the schema for that property
46-
[x] items property is required for all schemas/definitions of type `array`
47-
[x] path parameters must be declared a required
48-
[x] headers must not contain $ref
49-
[x] schema and property examples provided must validate against their respective object's schema
50-
[x] examples provided must validate their schema
33+
34+
[x] definition can't declare a property that's already defined by one of its ancestors
35+
[x] definition's ancestor can't be a descendant of the same model
36+
[x] path uniqueness: each api path should be non-verbatim (account for path param names) unique per method
37+
[x] each security reference should contain only unique scopes
38+
[x] each security scope in a security definition should be unique
39+
[x] parameters in path must be unique
40+
[x] each path parameter must correspond to a parameter placeholder and vice versa
41+
[x] each referenceable definition must have references
42+
[x] each definition property listed in the required array must be defined in the properties of the model
43+
[x] each parameter should have a unique `name` and `type` combination
44+
[x] each operation should have only 1 parameter of type body
45+
[x] each reference must point to a valid object
46+
[x] every default value that is specified must validate against the schema for that property
47+
[x] items property is required for all schemas/definitions of type `array`
48+
[x] path parameters must be declared a required
49+
[x] headers must not contain $ref
50+
[x] schema and property examples provided must validate against their respective object's schema
51+
[x] examples provided must validate their schema
5152
5253
Reported as warnings:
53-
[x] path parameters should not contain any of [{,},\w]
54-
[x] empty path
55-
[x] unused definitions
56-
[x] unsupported validation of examples on non-JSON media types
57-
[x] examples in response without schema
58-
[x] readOnly properties should not be required
5954
60-
Validating a schema
55+
[x] path parameters should not contain any of [{,},\w]
56+
[x] empty path
57+
[x] unused definitions
58+
[x] unsupported validation of examples on non-JSON media types
59+
[x] examples in response without schema
60+
[x] readOnly properties should not be required
61+
62+
# Validating a schema
6163
6264
The schema validation toolkit validates data against JSON-schema-draft 04 schema.
6365
@@ -70,16 +72,16 @@ Entry points:
7072
- AgainstSchema()
7173
- ...
7274
73-
Known limitations
75+
# Known limitations
7476
7577
With the current version of this package, the following aspects of swagger are not yet supported:
76-
[ ] errors and warnings are not reported with key/line number in spec
77-
[ ] default values and examples on responses only support application/json producer type
78-
[ ] invalid numeric constraints (such as Minimum, etc..) are not checked except for default and example values
79-
[ ] rules for collectionFormat are not implemented
80-
[ ] no validation rule for polymorphism support (discriminator) [not done here]
81-
[ ] valid js ECMA regexp not supported by Go regexp engine are considered invalid
82-
[ ] arbitrary large numbers are not supported: max is math.MaxFloat64
8378
79+
[ ] errors and warnings are not reported with key/line number in spec
80+
[ ] default values and examples on responses only support application/json producer type
81+
[ ] invalid numeric constraints (such as Minimum, etc..) are not checked except for default and example values
82+
[ ] rules for collectionFormat are not implemented
83+
[ ] no validation rule for polymorphism support (discriminator) [not done here]
84+
[ ] valid js ECMA regexp not supported by Go regexp engine are considered invalid
85+
[ ] arbitrary large numbers are not supported: max is math.MaxFloat64
8486
*/
8587
package validate

doc_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func ExampleSpec() {
4949
// Output: This spec is valid
5050
}
5151

52-
func ExampleSpec_url() {
52+
func ExampleSpec_second() {
5353
// Example with high level spec validation call, without showing warnings
5454

5555
// Example with online spec URL:
@@ -67,6 +67,9 @@ func ExampleSpec_url() {
6767
} else {
6868
fmt.Println("Could not load this spec")
6969
}
70+
71+
// Output:
72+
// This spec is valid
7073
}
7174

7275
func ExampleSpecValidator_Validate() {
@@ -116,6 +119,9 @@ func ExampleSpecValidator_Validate_url() {
116119
fmt.Println("This spec has some validation warnings")
117120
}
118121
}
122+
123+
// Output:
124+
// This spec is valid
119125
}
120126

121127
func ExampleAgainstSchema() {

example_validator.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func (ex *exampleValidator) isVisited(path string) bool {
4848
// - schemas
4949
// - individual property
5050
// - responses
51-
//
5251
func (ex *exampleValidator) Validate() (errs *Result) {
5352
errs = new(Result)
5453
if ex == nil || ex.SpecValidator == nil {

go.mod

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/go-openapi/validate
22

3-
go 1.14
3+
go 1.18
44

55
require (
66
github.com/go-openapi/analysis v0.21.2
@@ -13,3 +13,21 @@ require (
1313
github.com/stretchr/testify v1.7.0
1414
gopkg.in/yaml.v2 v2.4.0
1515
)
16+
17+
require (
18+
github.com/PuerkitoBio/purell v1.1.1 // indirect
19+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
20+
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
21+
github.com/davecgh/go-spew v1.1.1 // indirect
22+
github.com/go-openapi/jsonreference v0.19.6 // indirect
23+
github.com/go-stack/stack v1.8.0 // indirect
24+
github.com/josharian/intern v1.0.0 // indirect
25+
github.com/mailru/easyjson v0.7.6 // indirect
26+
github.com/mitchellh/mapstructure v1.4.1 // indirect
27+
github.com/oklog/ulid v1.3.1 // indirect
28+
github.com/pmezard/go-difflib v1.0.0 // indirect
29+
go.mongodb.org/mongo-driver v1.7.5 // indirect
30+
golang.org/x/net v0.0.0-20210421230115-4e50805a0758 // indirect
31+
golang.org/x/text v0.3.7 // indirect
32+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
33+
)

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ golang.org/x/tools v0.0.0-20190329151228-23e29df326fe/go.mod h1:LCzVGOaR6xXOjkQ3
151151
golang.org/x/tools v0.0.0-20190416151739-9c9e1878f421/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
152152
golang.org/x/tools v0.0.0-20190420181800-aa740d480789/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
153153
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
154-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
155154
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
156155
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
157156
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (h *paramHelper) resolveParam(path, method, operationID string, param *spec
250250

251251
}
252252
if err != nil { // Safeguard
253-
// NOTE: we may enter enter here when the whole parameter is an unresolved $ref
253+
// NOTE: we may enter here when the whole parameter is an unresolved $ref
254254
refPath := strings.Join([]string{"\"" + path + "\"", method}, ".")
255255
errorHelp.addPointerError(res, err, param.Ref.String(), refPath)
256256
return nil, res

jsonschema_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"strings"
2424
"testing"
25+
"time"
2526

2627
"github.com/go-openapi/spec"
2728
"github.com/go-openapi/strfmt"
@@ -107,7 +108,13 @@ func isExtendedEnabled(nm string) bool {
107108
func TestJSONSchemaSuite(t *testing.T) {
108109
// Internal local server to serve remote $ref
109110
go func() {
110-
err := http.ListenAndServe("localhost:1234", http.FileServer(http.Dir(jsonSchemaFixturesPath+"/remotes")))
111+
svr := &http.Server{
112+
ReadTimeout: 5 * time.Second,
113+
WriteTimeout: 10 * time.Second,
114+
Addr: "localhost:1234",
115+
Handler: http.FileServer(http.Dir(jsonSchemaFixturesPath + "/remotes")),
116+
}
117+
err := svr.ListenAndServe()
111118
if err != nil {
112119
panic(err.Error())
113120
}

0 commit comments

Comments
 (0)