Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi3filter: Simplify ValidateRequest implementation #1041

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/docs/openapi3filter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func ValidateParameter(ctx context.Context, input *RequestValidationInput, param
defined. The function returns RequestError with a openapi3.SchemaError cause
when a value is invalid by JSON schema.

func ValidateRequest(ctx context.Context, input *RequestValidationInput) (err error)
func ValidateRequest(ctx context.Context, input *RequestValidationInput) error
ValidateRequest is used to validate the given input according to previous
loaded OpenAPIv3 spec. If the input does not match the OpenAPIv3 spec,
a non-nil error will be returned.
Expand Down
36 changes: 18 additions & 18 deletions openapi3filter/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var ErrInvalidEmptyValue = errors.New("empty value is not allowed")
//
// Note: One can tune the behavior of uniqueItems: true verification
// by registering a custom function with openapi3.RegisterArrayUniqueItemsChecker
func ValidateRequest(ctx context.Context, input *RequestValidationInput) (err error) {
func ValidateRequest(ctx context.Context, input *RequestValidationInput) error {
var me openapi3.MultiError

options := input.Options
Expand All @@ -49,10 +49,10 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) (err er
security = &route.Spec.Security
}
if security != nil {
if err = ValidateSecurityRequirements(ctx, input, *security); err != nil && !options.MultiError {
return
}
if err != nil {
if err := ValidateSecurityRequirements(ctx, input, *security); err != nil {
if !options.MultiError {
return err
}
me = append(me, err)
}
}
Expand All @@ -66,10 +66,10 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) (err er
}
}

if err = ValidateParameter(ctx, input, parameter); err != nil && !options.MultiError {
return
}
if err != nil {
if err := ValidateParameter(ctx, input, parameter); err != nil {
if !options.MultiError {
return err
}
me = append(me, err)
}
}
Expand All @@ -79,29 +79,29 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) (err er
if options.ExcludeRequestQueryParams && parameter.Value.In == openapi3.ParameterInQuery {
continue
}
if err = ValidateParameter(ctx, input, parameter.Value); err != nil && !options.MultiError {
return
}
if err != nil {
if err := ValidateParameter(ctx, input, parameter.Value); err != nil {
if !options.MultiError {
return err
}
me = append(me, err)
}
}

// RequestBody
requestBody := operation.RequestBody
if requestBody != nil && !options.ExcludeRequestBody {
if err = ValidateRequestBody(ctx, input, requestBody.Value); err != nil && !options.MultiError {
return
}
if err != nil {
if err := ValidateRequestBody(ctx, input, requestBody.Value); err != nil {
if !options.MultiError {
return err
}
me = append(me, err)
}
}

if len(me) > 0 {
return me
}
return
return nil
}

// appendToQueryValues adds to query parameters each value in the provided slice
Expand Down
Loading