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

📝 [Proposal]: v3 auto binding #3005

Closed
3 tasks done
febelery opened this issue May 17, 2024 · 2 comments
Closed
3 tasks done

📝 [Proposal]: v3 auto binding #3005

febelery opened this issue May 17, 2024 · 2 comments

Comments

@febelery
Copy link

febelery commented May 17, 2024

Feature Proposal Description

I suggest binding to add an auto-bind method, like the golang echo framework's bind, to automatically parse based on content-type, Form, Body, JSON methods are too cumbersome

Alignment with Express API

N/a

HTTP RFC Standards Compliance

N/a

API Stability

N/a

Feature Examples

v2 code like:

type answerReq struct {
	UserId   int `json:"-" query:"-" params:"-"`
	QuizId   int `params:"id" json:"-" validate:"required"`
	RecordId int `json:"id" validate:"required"`
	Duration int        `json:"duration" validate:"required,min=3"`
	Answer   [][]string `json:"answer" validate:"required"`
}

func RequestParser[T any](c *fiber.Ctx) (*T, error) {
	req := new(T)

	if strings.ToLower(c.Method()) != "get" {
		if err := c.BodyParser(req); err != nil {
			if errors.Is(err, fiber.ErrUnprocessableEntity) {
				return nil, errors.New("errors")
			}
			return nil, err
		}
	}
	if err := c.QueryParser(req); err != nil {
		return nil, err
	}
	if err := c.ParamsParser(req); err != nil {
		return nil, err
	}

	if err := validator.Validate(req); err != nil {
		return nil, err
	}

	return req, nil
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have searched for existing issues that describe my proposal before opening this one.
  • I understand that a proposal that does not meet these guidelines may be closed without explanation.
@efectn
Copy link
Member

efectn commented May 17, 2024

@febelery the feature you requested already included in #2006 PR

It just has one Bind() method that covers other methods

@febelery
Copy link
Author

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants