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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 [Question]: Validate request params #2873

Closed
3 tasks done
meldumm opened this issue Feb 22, 2024 · 5 comments
Closed
3 tasks done

馃 [Question]: Validate request params #2873

meldumm opened this issue Feb 22, 2024 · 5 comments

Comments

@meldumm
Copy link

meldumm commented Feb 22, 2024

Question Description

Request:

{
    "tokens": [{
        "address": "test",
        "decimals": "1"
    }]
}

If a user sends a request with a string instead of an int for decimals, the following error will be returned:

runtime error: invalid memory address or nil pointer dereference

Is there a way to validate the request body and return a proper error message to the user?

Code Snippet (optional)

type Token struct {
	Address  string `json:"address"`
	Decimals int    `json:"decimals"`
}

type tokenAddRequestParams struct {
	Tokens        []Token `json:"tokens"`
}

func AddTokens(c *fiber.Ctx) error {
	var req tokenAddRequestParams
	if err := c.BodyParser(&req); err != nil {
		return nil, c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse JSON"})
	}
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
Copy link

welcome bot commented Feb 22, 2024

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

is this a proposal for V2 or V3 ?

@meldumm
Copy link
Author

meldumm commented Feb 22, 2024

This is a question for V2

@sixcolors
Copy link
Member

Can you provide a minimal runnable example?

@meldumm
Copy link
Author

meldumm commented Feb 22, 2024

The issue was identified in a different section of the code. Thank you for your assistance

@meldumm meldumm closed this as completed Feb 22, 2024
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

3 participants