Skip to content

Commit

Permalink
Merge pull request #131 from cmars/fix/error-on-empty-specs
Browse files Browse the repository at this point in the history
fix: fail fast when creating a validator that has no specs.
  • Loading branch information
cmars authored Jan 25, 2022
2 parents 5743bd3 + 5759dbd commit c27db8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions versionware/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func today() time.Time {
// requests according to the given OpenAPI spec versions. For configuration
// defaults, a nil config may be used.
func NewValidator(config *ValidatorConfig, docs ...*openapi3.T) (*Validator, error) {
if len(docs) == 0 {
return nil, fmt.Errorf("no OpenAPI versions provided")
}
if config == nil {
config = &defaultValidatorConfig
}
Expand Down
14 changes: 14 additions & 0 deletions versionware/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,19 @@ func TestValidator(t *testing.T) {

func TestValidatorConfig(t *testing.T) {
c := qt.New(t)

// No specs provided
_, err := versionware.NewValidator(&versionware.ValidatorConfig{ServerURL: "://"})
c.Assert(err, qt.ErrorMatches, `no OpenAPI versions provided`)

// Invalid server URL
_, err = versionware.NewValidator(&versionware.ValidatorConfig{ServerURL: "://"}, &openapi3.T{})
c.Assert(err, qt.ErrorMatches, `invalid ServerURL: parse "://": missing protocol scheme`)

// Missing version in OpenAPI spec
_, err = versionware.NewValidator(&versionware.ValidatorConfig{ServerURL: "http://example.com"}, &openapi3.T{})
c.Assert(err, qt.ErrorMatches, `extension "x-snyk-api-version" not found`)

docs := make([]*openapi3.T, 2)
for i, specStr := range []string{v20210820, v20210916} {
doc, err := openapi3.NewLoader().LoadFromData([]byte(specStr))
Expand All @@ -482,8 +492,12 @@ func TestValidatorConfig(t *testing.T) {
c.Assert(err, qt.IsNil)
docs[i] = doc
}

// Invalid server URL
_, err = versionware.NewValidator(&versionware.ValidatorConfig{ServerURL: "localhost:8080"}, docs...)
c.Assert(err, qt.ErrorMatches, `invalid ServerURL: unsupported scheme "localhost" \(did you forget to specify the scheme://\?\)`)

// Valid
_, err = versionware.NewValidator(&versionware.ValidatorConfig{ServerURL: "http://localhost:8080"}, docs...)
c.Assert(err, qt.IsNil)
c.Assert(docs[0].Servers[0].URL, qt.Equals, "http://localhost:8080")
Expand Down

0 comments on commit c27db8c

Please sign in to comment.