feat(api): validate custom memo_id format in CreateMemo

Add early validation for custom memo IDs to provide clear error
messages when an invalid format is provided.

Validation ensures memo_id matches the required pattern:
- 1-32 characters long
- Alphanumeric and hyphens only
- Cannot start or end with a hyphen
- Pattern: ^[a-zA-Z0-9]([a-zA-Z0-9-]{0,30}[a-zA-Z0-9])?$

This provides better UX by catching format errors early with a
descriptive error message rather than a generic "invalid uid"
error from the store layer.
This commit is contained in:
Claude 2025-11-08 01:28:53 +00:00
parent 94483ea2a4
commit c8900acc4e
No known key found for this signature in database
1 changed files with 6 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/usememos/memos/internal/base"
"github.com/usememos/memos/plugin/webhook"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
@ -33,6 +34,11 @@ func (s *APIV1Service) CreateMemo(ctx context.Context, request *v1pb.CreateMemoR
memoUID := strings.TrimSpace(request.MemoId)
if memoUID == "" {
memoUID = shortuuid.New()
} else {
// Validate custom memo ID format
if !base.UIDMatcher.MatchString(memoUID) {
return nil, status.Errorf(codes.InvalidArgument, "invalid memo_id format: must be 1-32 characters, alphanumeric and hyphens only, cannot start or end with hyphen")
}
}
create := &store.Memo{