Ai
1 Star 0 Fork 0

ryancartoon/sensu-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
validators.go 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
package v2
import (
"errors"
"regexp"
)
// ConstrainedResource defines a resources that has contraints on it's attributes
type ConstrainedResource interface {
Validate() error
}
// NameRegex is used to validate the name of a resource
var NameRegex = regexp.MustCompile(`\A[\w\.\-]+\z`)
// StrictNameRegex is used to validate names of resources using a strict subset
// of charset.
var StrictNameRegex = regexp.MustCompile(`\A[a-z0-9\_\.\-]+\z`)
// SubscriptionNameRegex is used to validate the name of a subscription, which
// can contain a single ":" character in case of an entity subscription (e.g.
// entity:foo)
var SubscriptionNameRegex = regexp.MustCompile(`\A[\w\.\-]+\:?[\w\.\-]+\z`)
// ValidateName validates the name of an element so it's not empty and it does
// not contains specical characters. Compatible with Sensu 1.0.
func ValidateName(name string) error {
return validateNameWithPattern(name, NameRegex)
}
// ValidateNameStrict validates the name of an element so it's not empty and it
// does not contains specical characters. Not compatible with Sensu 1.0
// resources.
func ValidateNameStrict(name string) error {
return validateNameWithPattern(name, StrictNameRegex)
}
// ValidateSubscriptionName validates the name of a subscription so it's not
// empty and it does not contains special characters except for an optional ":"
func ValidateSubscriptionName(name string) error {
return validateNameWithPattern(name, SubscriptionNameRegex)
}
// validateName validates the name of an element so it's not empty and it does
// not contains specical characters
func validateNameWithPattern(name string, rexp *regexp.Regexp) error {
if name == "" {
return errors.New("must not be empty")
}
if match := rexp.MatchString(name); !match {
return errors.New("cannot contain spaces or special characters")
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ryancartoon/sensu-go.git
git@gitee.com:ryancartoon/sensu-go.git
ryancartoon
sensu-go
sensu-go
v5.10.1

搜索帮助