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

feat: Add ParamsStruct in middleware.Request #1204

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

akkuman
Copy link

@akkuman akkuman commented Mar 26, 2024

I wish to support custom validate(like https://github.com/go-ozzo/ozzo-validation), but it only support validate on body currently

example middleware:

package middleware

import "github.com/ogen-go/ogen/middleware"

type validatable interface {
	Validate() error
}

// Validator 一个适用于 ogen 的校验中间件
func Validator() middleware.Middleware {
	return func(req middleware.Request, next middleware.Next) (middleware.Response, error) {
		if params, ok := req.ParamsStruct.(validatable); ok {
			err := params.Validate()
			if err != nil {
				return middleware.Response{}, err
			}
		}
		if body, ok := req.Body.(validatable); ok {
			err := body.Validate()
			if err != nil {
				return middleware.Response{}, err
			}
		}
		return next(req)
	}
}

Copy link

codecov bot commented Mar 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.87%. Comparing base (b1ddda2) to head (c94a623).
Report is 25 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1204   +/-   ##
=======================================
  Coverage   73.87%   73.87%           
=======================================
  Files         189      189           
  Lines       12772    12772           
=======================================
  Hits         9435     9435           
  Misses       2797     2797           
  Partials      540      540           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@akkuman akkuman force-pushed the feat-params-struct branch from cca52cf to 1608df8 Compare April 29, 2024 06:38
@akkuman akkuman force-pushed the feat-params-struct branch from 1608df8 to c94a623 Compare May 14, 2024 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant