diff --git a/versionware/validator.go b/versionware/validator.go index 100ffee5..e0ba51c6 100644 --- a/versionware/validator.go +++ b/versionware/validator.go @@ -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 } diff --git a/versionware/validator_test.go b/versionware/validator_test.go index b86dd102..f2a265ed 100644 --- a/versionware/validator_test.go +++ b/versionware/validator_test.go @@ -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)) @@ -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")