23 lines
657 B
Go
23 lines
657 B
Go
package fiber
|
|
|
|
import (
|
|
"mime"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/strictjson"
|
|
)
|
|
|
|
// decode checks HTTP media type and maps strict JSON decoding errors to API input errors.
|
|
func decode(ctx *fiber.Ctx, target any, requiredFields ...string) error {
|
|
mediaType, _, err := mime.ParseMediaType(ctx.Get(fiber.HeaderContentType))
|
|
if err != nil || mediaType != fiber.MIMEApplicationJSON {
|
|
return errMedia
|
|
}
|
|
if err := strictjson.DecodeObject(ctx.Body(), target, requiredFields...); err != nil {
|
|
return service.ErrInvalid
|
|
}
|
|
return nil
|
|
}
|