jokes-bapak2/api/app/v1/middleware/validation.go

29 lines
495 B
Go
Raw Normal View History

package middleware
import (
"jokes-bapak2-api/app/v1/models"
"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).
JSON(models.Error{
Error: "only numbers are allowed as ID",
})
}
}