forked from mtusi2026/mtusi2026lab6
35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package fiber
|
|
|
|
import (
|
|
"mime"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/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
|
|
}
|
|
|
|
// decodeMessage accepts the optional attachment list without weakening strict JSON validation.
|
|
func decodeMessage(ctx *fiber.Ctx, target any) error {
|
|
mediaType, _, err := mime.ParseMediaType(ctx.Get(fiber.HeaderContentType))
|
|
if err != nil || mediaType != fiber.MIMEApplicationJSON {
|
|
return errMedia
|
|
}
|
|
if err := strictjson.DecodeOptional(ctx.Body(), target, []string{"text"}, []string{"attachment_ids"}); err != nil {
|
|
return service.ErrInvalid
|
|
}
|
|
return nil
|
|
}
|