Skip to content

Commit 68016e0

Browse files
danicc097fenollp
andauthored
Examples validation (#592)
Co-authored-by: Pierre Fenoll <[email protected]>
1 parent 46603c3 commit 68016e0

File tree

8 files changed

+505
-7
lines changed

8 files changed

+505
-7
lines changed

openapi3/components.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (components *Components) Validate(ctx context.Context) (err error) {
9999
return
100100
}
101101
if err = v.Validate(ctx); err != nil {
102-
return
102+
return fmt.Errorf("%s: %s", k, err)
103103
}
104104
}
105105

openapi3/example.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openapi3
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/go-openapi/jsonpointer"
@@ -55,5 +56,12 @@ func (example *Example) UnmarshalJSON(data []byte) error {
5556

5657
// Validate returns an error if Example does not comply with the OpenAPI spec.
5758
func (example *Example) Validate(ctx context.Context) error {
58-
return nil // TODO
59+
if example.Value != nil && example.ExternalValue != "" {
60+
return errors.New("value and externalValue are mutually exclusive")
61+
}
62+
if example.Value == nil && example.ExternalValue == "" {
63+
return errors.New("example has no value or externalValue field")
64+
}
65+
66+
return nil
5967
}

openapi3/example_validation.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package openapi3
2+
3+
func validateExampleValue(input interface{}, schema *Schema) error {
4+
return schema.VisitJSON(input, MultiErrors())
5+
}

0 commit comments

Comments
 (0)