Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Add function to validate uuid strings #127

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion internals/api/uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ package uuid

import (
gid "github.com/satori/go.uuid"

"github.com/secrethub/secrethub-go/internals/errio"
)

// Errors
var (
errInvalidUUID = errio.Namespace("uuid").Code("invalid")
ErrInvalidUUIDErr = errInvalidUUID.ErrorPref("invalid uuid: %s")
)

// UUID is a wrapper around go.uuid.UUID
Expand All @@ -20,7 +28,7 @@ func New() *UUID {
func FromString(str string) (*UUID, error) {
id, err := gid.FromString(str)
if err != nil {
return nil, err
return nil, ErrInvalidUUIDErr(err)
}
return &UUID{id}, nil
}
Expand All @@ -39,3 +47,18 @@ func (u *UUID) IsZero() bool {
func Equal(a *UUID, b *UUID) bool {
return gid.Equal(a.UUID, b.UUID)
}

// Validate validates a uuid string is a valid UUID.
func Validate(str string) error {
_, err := FromString(str)
return err
}

// IsErrInvalidUUID returns whether the given error is returned for an invalid uuid.
func IsErrInvalidUUID(err error) bool {
SimonBarendse marked this conversation as resolved.
Show resolved Hide resolved
publicErr, ok := err.(errio.PublicError)
if !ok {
return false
}
return publicErr.Namespace == errInvalidUUID.Namespace && publicErr.Code == errInvalidUUID.Code
}