Skip to content

Commit

Permalink
Examples validation (#592)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Fenoll <[email protected]>
  • Loading branch information
danicc097 and fenollp authored Sep 16, 2022
1 parent 46603c3 commit 68016e0
Show file tree
Hide file tree
Showing 8 changed files with 505 additions and 7 deletions.
2 changes: 1 addition & 1 deletion openapi3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (components *Components) Validate(ctx context.Context) (err error) {
return
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("%s: %s", k, err)
}
}

Expand Down
10 changes: 9 additions & 1 deletion openapi3/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi3

import (
"context"
"errors"
"fmt"

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

// Validate returns an error if Example does not comply with the OpenAPI spec.
func (example *Example) Validate(ctx context.Context) error {
return nil // TODO
if example.Value != nil && example.ExternalValue != "" {
return errors.New("value and externalValue are mutually exclusive")
}
if example.Value == nil && example.ExternalValue == "" {
return errors.New("example has no value or externalValue field")
}

return nil
}
5 changes: 5 additions & 0 deletions openapi3/example_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package openapi3

func validateExampleValue(input interface{}, schema *Schema) error {
return schema.VisitJSON(input, MultiErrors())
}
Loading

0 comments on commit 68016e0

Please sign in to comment.