chore: validate shortcut name

This commit is contained in:
Steven 2023-06-29 22:05:32 +08:00
parent 1792ca1272
commit 774966f2eb

View File

@ -92,7 +92,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
shortcut, err := s.Store.CreateShortcut(ctx, &store.Shortcut{ shortcut, err := s.Store.CreateShortcut(ctx, &store.Shortcut{
CreatorID: userID, CreatorID: userID,
Name: create.Name, Name: strings.ToLower(create.Name),
Link: create.Link, Link: create.Link,
Description: create.Description, Description: create.Description,
Visibility: convertVisibilityToStore(create.Visibility), Visibility: convertVisibilityToStore(create.Visibility),
@ -147,6 +147,10 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
if err := json.NewDecoder(c.Request().Body).Decode(patch); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(patch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err)
} }
if patch.Name != nil {
name := strings.ToLower(*patch.Name)
patch.Name = &name
}
if patch.Link != nil && !validateLink(*patch.Link) { if patch.Link != nil && !validateLink(*patch.Link) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid link: %s", *patch.Link)) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid link: %s", *patch.Link))
} }