Skip to content

Commit

Permalink
Additional error information (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
danicc097 authored Oct 7, 2022
1 parent d4c06af commit 59dbc9a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
36 changes: 18 additions & 18 deletions openapi3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range schemas {
v := components.Schemas[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("schema %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("schema %q: %w", k, err)
}
}

Expand All @@ -64,10 +64,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range parameters {
v := components.Parameters[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("parameter %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("parameter %q: %w", k, err)
}
}

Expand All @@ -79,10 +79,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range requestBodies {
v := components.RequestBodies[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("request body %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("request body %q: %w", k, err)
}
}

Expand All @@ -94,10 +94,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range responses {
v := components.Responses[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("response %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("response %q: %w", k, err)
}
}

Expand All @@ -109,10 +109,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range headers {
v := components.Headers[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("header %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("header %q: %w", k, err)
}
}

Expand All @@ -124,10 +124,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range securitySchemes {
v := components.SecuritySchemes[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("security scheme %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("security scheme %q: %w", k, err)
}
}

Expand All @@ -139,10 +139,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range examples {
v := components.Examples[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("example %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return fmt.Errorf("%s: %w", k, err)
return fmt.Errorf("example %q: %w", k, err)
}
}

Expand All @@ -154,10 +154,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range links {
v := components.Links[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("link %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("link %q: %w", k, err)
}
}

Expand All @@ -169,10 +169,10 @@ func (components *Components) Validate(ctx context.Context) (err error) {
for _, k := range callbacks {
v := components.Callbacks[k]
if err = ValidateIdentifier(k); err != nil {
return
return fmt.Errorf("callback %q: %w", k, err)
}
if err = v.Validate(ctx); err != nil {
return
return fmt.Errorf("callback %q: %w", k, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion openapi3/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (example *Example) Validate(ctx context.Context) error {
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 errors.New("no value or externalValue field")
}

return nil
Expand Down
16 changes: 8 additions & 8 deletions openapi3/example_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestExamplesSchemaValidation(t *testing.T) {
parametersExample: `
example: abcd
`,
errContains: "invalid paths",
errContains: "invalid path /user: invalid operation POST: invalid example",
},
{
name: "valid_parameter_example",
Expand All @@ -64,7 +64,7 @@ func TestExamplesSchemaValidation(t *testing.T) {
email: bad
password: short
`,
errContains: "invalid paths: invalid path /user: invalid operation POST: BadUser",
errContains: "invalid paths: invalid path /user: invalid operation POST: example BadUser",
},
{
name: "valid_component_examples",
Expand All @@ -90,7 +90,7 @@ func TestExamplesSchemaValidation(t *testing.T) {
email: bad
password: short
`,
errContains: "invalid paths",
errContains: "invalid path /user: invalid operation POST: invalid example",
},
{
name: "valid_mediatype_examples",
Expand All @@ -109,7 +109,7 @@ func TestExamplesSchemaValidation(t *testing.T) {
email: good@email.com
# missing password
`,
errContains: "invalid schema example",
errContains: "schema \"CreateUserRequest\": invalid example",
},
{
name: "valid_schema_request_example",
Expand All @@ -127,7 +127,7 @@ func TestExamplesSchemaValidation(t *testing.T) {
user_id: 1
# missing access_token
`,
errContains: "invalid schema example",
errContains: "schema \"CreateUserResponse\": invalid example",
},
{
name: "valid_schema_response_example",
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestExampleObjectValidation(t *testing.T) {
email: real@email.com
password: validpassword
`,
errContains: "example and examples are mutually exclusive",
errContains: "invalid path /user: invalid operation POST: example and examples are mutually exclusive",
componentExamples: `
examples:
BadUser:
Expand All @@ -295,7 +295,7 @@ func TestExampleObjectValidation(t *testing.T) {
BadUser:
description: empty user example
`,
errContains: "example has no value or externalValue field",
errContains: "invalid components: example \"BadUser\": no value or externalValue field",
},
{
name: "value_externalValue_mutual_exclusion",
Expand All @@ -308,7 +308,7 @@ func TestExampleObjectValidation(t *testing.T) {
password: validpassword
externalValue: 'http://example.com/examples/example'
`,
errContains: "value and externalValue are mutually exclusive",
errContains: "invalid components: example \"BadUser\": value and externalValue are mutually exclusive",
},
}

Expand Down
2 changes: 1 addition & 1 deletion openapi3/issue601_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestIssue601(t *testing.T) {
require.NoError(t, err)

err = doc.Validate(sl.Context)
require.Contains(t, err.Error(), `invalid components: invalid schema example: Error at "/type": property "type" is missing`)
require.Contains(t, err.Error(), `invalid components: schema "DiscoveryResult": invalid example: Error at "/type": property "type" is missing`)
require.Contains(t, err.Error(), `| Error at "/nsid": property "nsid" is missing`)

err = doc.Validate(sl.Context, DisableExamplesValidation())
Expand Down
6 changes: 3 additions & 3 deletions openapi3/media_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (mediaType *MediaType) Validate(ctx context.Context) error {

if example := mediaType.Example; example != nil {
if err := validateExampleValue(example, schema.Value); err != nil {
return err
return fmt.Errorf("invalid example: %w", err)
}
}

Expand All @@ -107,10 +107,10 @@ func (mediaType *MediaType) Validate(ctx context.Context) error {
for _, k := range names {
v := examples[k]
if err := v.Validate(ctx); err != nil {
return fmt.Errorf("%s: %w", k, err)
return fmt.Errorf("example %s: %w", k, err)
}
if err := validateExampleValue(v.Value.Value, schema.Value); err != nil {
return fmt.Errorf("%s: %w", k, err)
return fmt.Errorf("example %s: %w", k, err)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion openapi3/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (parameter *Parameter) Validate(ctx context.Context) error {
}
if example := parameter.Example; example != nil {
if err := validateExampleValue(example, schema.Value); err != nil {
return err
return fmt.Errorf("invalid example: %w", err)
}
} else if examples := parameter.Examples; examples != nil {
names := make([]string, 0, len(examples))
Expand Down
4 changes: 2 additions & 2 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,13 +763,13 @@ func (schema *Schema) validate(ctx context.Context, stack []*Schema) (err error)

if v := schema.Default; v != nil {
if err := schema.VisitJSON(v); err != nil {
return fmt.Errorf("invalid schema default: %w", err)
return fmt.Errorf("invalid default: %w", err)
}
}

if x := schema.Example; x != nil && !validationOpts.ExamplesValidationDisabled {
if err := validateExampleValue(x, schema); err != nil {
return fmt.Errorf("invalid schema example: %w", err)
return fmt.Errorf("invalid example: %w", err)
}
}

Expand Down

0 comments on commit 59dbc9a

Please sign in to comment.