1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5// Package validate provides input validation helpers.
6package validate
7
8import (
9 "errors"
10 "fmt"
11
12 "git.secluded.site/go-lunatask"
13)
14
15// ErrInvalidReference indicates the input is not a valid UUID or deep link.
16var ErrInvalidReference = errors.New("invalid reference: expected UUID or lunatask:// deep link")
17
18// Reference parses a UUID or Lunatask deep link and returns the normalised UUID.
19// Accepts formats:
20// - UUID: "3bbf1923-64ae-4bcf-96a9-9bb86c799dab"
21// - Deep link: "lunatask://areas/3bbf1923-64ae-4bcf-96a9-9bb86c799dab"
22func Reference(input string) (string, error) {
23 _, id, err := lunatask.ParseDeepLink(input)
24 if err != nil {
25 return "", fmt.Errorf("%w: %s", ErrInvalidReference, input)
26 }
27
28 return id, nil
29}
30
31// ErrInvalidStatus indicates the status value is not recognized.
32var ErrInvalidStatus = errors.New("invalid status")
33
34// TaskStatus validates and normalizes a task status string.
35// Returns the corresponding lunatask.TaskStatus or an error if invalid.
36func TaskStatus(input string) (lunatask.TaskStatus, error) {
37 status, err := lunatask.ParseTaskStatus(input)
38 if err != nil {
39 return "", fmt.Errorf("%w: %s", ErrInvalidStatus, input)
40 }
41
42 return status, nil
43}
44
45// ErrInvalidMotivation indicates the motivation value is not recognized.
46var ErrInvalidMotivation = errors.New("invalid motivation")
47
48// Motivation validates and normalizes a motivation string.
49// Returns the corresponding lunatask.Motivation or an error if invalid.
50func Motivation(input string) (lunatask.Motivation, error) {
51 motivation, err := lunatask.ParseMotivation(input)
52 if err != nil {
53 return "", fmt.Errorf("%w: %s", ErrInvalidMotivation, input)
54 }
55
56 return motivation, nil
57}
58
59// ErrInvalidRelationship indicates the relationship strength is not recognized.
60var ErrInvalidRelationship = errors.New("invalid relationship strength")
61
62// RelationshipStrength validates and normalizes a relationship strength string.
63// Returns the corresponding lunatask.RelationshipStrength or an error if invalid.
64func RelationshipStrength(input string) (lunatask.RelationshipStrength, error) {
65 rel, err := lunatask.ParseRelationshipStrength(input)
66 if err != nil {
67 return "", fmt.Errorf("%w: %s", ErrInvalidRelationship, input)
68 }
69
70 return rel, nil
71}