Skip to content

Commit

Permalink
openapi3filter: Simplify ValidateRequest implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Dec 11, 2024
1 parent 793b28d commit ecbc224
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
3 changes: 1 addition & 2 deletions .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 Expand Up @@ -433,4 +433,3 @@ func Strict(strict bool) ValidatorOption

func ValidationOptions(options Options) ValidatorOption
ValidationOptions sets request/response validation options on the validator.

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

0 comments on commit ecbc224

Please sign in to comment.