Skip to content

Commit

Permalink
feat(Component): validate Type
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Jul 17, 2024
1 parent c9ff8ad commit ba3c214
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions component.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,41 @@ import (
"fmt"
"net/url"
"regexp"
"slices"
"strings"

"gopkg.in/yaml.v3"
)

const (
ComponentTypeSecurity = "Security"
ComponentTypePortfolio = "Portfolio"
ComponentTypeEquity = "Equity"
ComponentTypeETF = "ETF"
ComponentTypeFactor = "Factor"
ComponentTypeMutualFund = "Mutual Fund"
)

func ComponentTypes() []string {
return []string{
ComponentTypeSecurity,
ComponentTypePortfolio,
ComponentTypeEquity,
ComponentTypeETF,
ComponentTypeFactor,
ComponentTypeMutualFund,
}
}

type Component struct {
Type string `yaml:"type,omitempty" json:"type,omitempty" bson:"type"`
ID string `yaml:"id,omitempty" json:"id,omitempty" bson:"id"`
Label string `yaml:"label,omitempty" json:"label,omitempty" bson:"label"`
}

var componentExpression = regexp.MustCompile(`^[a-zA-Z0-9.:s]{1,24}$`)
var componentExpression = regexp.MustCompile(`^[a-zA-Z0-9.:]{1,24}$`)

func (component Component) Validate() error {
func (component *Component) Validate() error {
if component.ID == "" {
return fmt.Errorf(`component ID must be set`)
}
Expand All @@ -27,6 +48,9 @@ func (component Component) Validate() error {
if !componentExpression.MatchString(component.ID) {
return fmt.Errorf("component id %q does not match the component ID pattern %q", component.ID, componentExpression.String())
}
if component.Type != "" && !slices.Contains(ComponentTypes(), component.Type) {
return fmt.Errorf("component type %q is not a known component type", component.Type)
}
return nil
}

Expand Down

0 comments on commit ba3c214

Please sign in to comment.