2021-07-18 05:28:24 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func OnlyIntegerAsID() fiber.Handler {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
regex, err := regexp.Compile(`([0-9]+)`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
loc := regex.FindStringIndex(c.Params("id"))
|
|
|
|
if loc[1] == len(c.Params("id")) {
|
|
|
|
return c.Next()
|
|
|
|
}
|
|
|
|
|
2021-08-04 10:14:33 +00:00
|
|
|
return c.
|
|
|
|
Status(fiber.StatusBadRequest).
|
2021-09-26 19:13:38 +00:00
|
|
|
JSON(Error{
|
2021-08-04 10:14:33 +00:00
|
|
|
Error: "only numbers are allowed as ID",
|
|
|
|
})
|
2021-07-18 05:28:24 +00:00
|
|
|
}
|
|
|
|
}
|