diff --git a/model/labelname.go b/model/labelname.go index a283132..e56fe41 100644 --- a/model/labelname.go +++ b/model/labelname.go @@ -14,6 +14,8 @@ package model import ( + "fmt" + "regexp" "strings" ) @@ -63,6 +65,19 @@ const ( // therewith. type LabelName string +// UnmarshalYAML implements the yaml.Unmarshaler interface. +func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error { + var s string + if err := unmarshal(&s); err != nil { + return err + } + if !labelNameRE.MatchString(s) { + return fmt.Errorf("%q is not a valid label name", s) + } + *ln = LabelName(s) + return nil +} + // LabelNames is a sortable LabelName slice. In implements sort.Interface. type LabelNames []LabelName