Skip to content

Commit b4b41f3

Browse files
authored
Add variadic options to Validate method (#692)
1 parent 7f75486 commit b4b41f3

31 files changed

+284
-154
lines changed

.github/workflows/go.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ jobs:
3636
- run: echo ${{ steps.go-cache-paths.outputs.go-mod }}
3737

3838
- name: Go Build Cache
39-
uses: actions/cache@v2
39+
uses: actions/cache@v3
4040
with:
4141
path: ${{ steps.go-cache-paths.outputs.go-build }}
4242
key: ${{ runner.os }}-go-${{ matrix.go }}-build-${{ hashFiles('**/go.sum') }}
4343

4444
- name: Go Mod Cache (go>=1.15)
45-
uses: actions/cache@v2
45+
uses: actions/cache@v3
4646
with:
4747
path: ${{ steps.go-cache-paths.outputs.go-mod }}
4848
key: ${{ runner.os }}-go-${{ matrix.go }}-mod-${{ hashFiles('**/go.sum') }}
@@ -61,6 +61,7 @@ jobs:
6161
- run: go fmt ./...
6262
- run: git --no-pager diff --exit-code
6363

64+
- run: go test ./...
6465
- if: runner.os == 'Linux'
6566
run: go test -count=10 ./...
6667
env:
@@ -116,7 +117,7 @@ jobs:
116117
fi
117118
118119
# Ensure impl Validate()
119-
if ! git grep -InE 'func [(].+Schema[)] Validate[(]ctx context.Context[)].+error.+[{]'; then
120+
if ! git grep -InE 'func [(].+'"$ty"'[)] Validate[(]ctx context.Context, opts [.][.][.]ValidationOption[)].+error.+[{]'; then
120121
echo "OAI type $ty does not implement Validate()" && exit 1
121122
fi
122123

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func arrayUniqueItemsChecker(items []interface{}) bool {
196196

197197
## Sub-v0 breaking API changes
198198

199+
### v0.111.0
200+
* Changed `func (*_) Validate(ctx context.Context) error` to `func (*_) Validate(ctx context.Context, opts ...ValidationOption) error`.
201+
* `openapi3.WithValidationOptions(ctx context.Context, opts *ValidationOptions) context.Context` prototype changed to `openapi3.WithValidationOptions(ctx context.Context, opts ...ValidationOption) context.Context`.
202+
199203
### v0.101.0
200204
* `openapi3.SchemaFormatValidationDisabled` has been removed in favour of an option `openapi3.EnableSchemaFormatValidation()` passed to `openapi3.T.Validate`. The default behaviour is also now to not validate formats, as the OpenAPI spec mentions the `format` is an open value.
201205

openapi3/callback.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ func (c Callbacks) JSONLookup(token string) (interface{}, error) {
3030
type Callback map[string]*PathItem
3131

3232
// Validate returns an error if Callback does not comply with the OpenAPI spec.
33-
func (callback Callback) Validate(ctx context.Context) error {
33+
func (callback Callback) Validate(ctx context.Context, opts ...ValidationOption) error {
34+
ctx = WithValidationOptions(ctx, opts...)
35+
3436
keys := make([]string, 0, len(callback))
3537
for key := range callback {
3638
keys = append(keys, key)

openapi3/components.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ func (components *Components) UnmarshalJSON(data []byte) error {
4040
}
4141

4242
// Validate returns an error if Components does not comply with the OpenAPI spec.
43-
func (components *Components) Validate(ctx context.Context) (err error) {
43+
func (components *Components) Validate(ctx context.Context, opts ...ValidationOption) (err error) {
44+
ctx = WithValidationOptions(ctx, opts...)
45+
4446
schemas := make([]string, 0, len(components.Schemas))
4547
for name := range components.Schemas {
4648
schemas = append(schemas, name)

openapi3/content.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ func (content Content) Get(mime string) *MediaType {
106106
}
107107

108108
// Validate returns an error if Content does not comply with the OpenAPI spec.
109-
func (content Content) Validate(ctx context.Context) error {
109+
func (content Content) Validate(ctx context.Context, opts ...ValidationOption) error {
110+
ctx = WithValidationOptions(ctx, opts...)
111+
110112
keys := make([]string, 0, len(content))
111113
for key := range content {
112114
keys = append(keys, key)

openapi3/discriminator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func (discriminator *Discriminator) UnmarshalJSON(data []byte) error {
2626
}
2727

2828
// Validate returns an error if Discriminator does not comply with the OpenAPI spec.
29-
func (discriminator *Discriminator) Validate(ctx context.Context) error {
29+
func (discriminator *Discriminator) Validate(ctx context.Context, opts ...ValidationOption) error {
30+
// ctx = WithValidationOptions(ctx, opts...)
31+
3032
return nil
3133
}

openapi3/encoding.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ func (encoding *Encoding) SerializationMethod() *SerializationMethod {
6666
}
6767

6868
// Validate returns an error if Encoding does not comply with the OpenAPI spec.
69-
func (encoding *Encoding) Validate(ctx context.Context) error {
69+
func (encoding *Encoding) Validate(ctx context.Context, opts ...ValidationOption) error {
70+
ctx = WithValidationOptions(ctx, opts...)
71+
7072
if encoding == nil {
7173
return nil
7274
}

openapi3/example.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func (example *Example) UnmarshalJSON(data []byte) error {
5555
}
5656

5757
// Validate returns an error if Example does not comply with the OpenAPI spec.
58-
func (example *Example) Validate(ctx context.Context) error {
58+
func (example *Example) Validate(ctx context.Context, opts ...ValidationOption) error {
59+
// ctx = WithValidationOptions(ctx, opts...)
60+
5961
if example.Value != nil && example.ExternalValue != "" {
6062
return errors.New("value and externalValue are mutually exclusive")
6163
}

openapi3/example_validation_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ func TestExamplesSchemaValidation(t *testing.T) {
221221
t.Parallel()
222222
for _, tc := range testCases {
223223
t.Run(tc.name, func(t *testing.T) {
224-
loader := NewLoader()
225-
226224
spec := bytes.Buffer{}
227225
spec.WriteString(`
228226
openapi: 3.0.3
@@ -339,13 +337,14 @@ components:
339337
`)
340338
spec.WriteString(tc.componentExamples)
341339

340+
loader := NewLoader()
342341
doc, err := loader.LoadFromData(spec.Bytes())
343342
require.NoError(t, err)
344343

345344
if testOption.disableExamplesValidation {
346345
err = doc.Validate(loader.Context, DisableExamplesValidation())
347346
} else {
348-
err = doc.Validate(loader.Context)
347+
err = doc.Validate(loader.Context, EnableExamplesValidation())
349348
}
350349

351350
if tc.errContains != "" && !testOption.disableExamplesValidation {
@@ -436,8 +435,6 @@ func TestExampleObjectValidation(t *testing.T) {
436435
t.Parallel()
437436
for _, tc := range testCases {
438437
t.Run(tc.name, func(t *testing.T) {
439-
loader := NewLoader()
440-
441438
spec := bytes.Buffer{}
442439
spec.WriteString(`
443440
openapi: 3.0.3
@@ -506,6 +503,7 @@ components:
506503
`)
507504
spec.WriteString(tc.componentExamples)
508505

506+
loader := NewLoader()
509507
doc, err := loader.LoadFromData(spec.Bytes())
510508
require.NoError(t, err)
511509

openapi3/external_docs.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ func (e *ExternalDocs) UnmarshalJSON(data []byte) error {
2929
}
3030

3131
// Validate returns an error if ExternalDocs does not comply with the OpenAPI spec.
32-
func (e *ExternalDocs) Validate(ctx context.Context) error {
32+
func (e *ExternalDocs) Validate(ctx context.Context, opts ...ValidationOption) error {
33+
// ctx = WithValidationOptions(ctx, opts...)
34+
3335
if e.URL == "" {
3436
return errors.New("url is required")
3537
}

0 commit comments

Comments
 (0)