version.go 953 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package manifest_entities
  2. import (
  3. "fmt"
  4. "regexp"
  5. "github.com/go-playground/validator/v10"
  6. "github.com/langgenius/dify-plugin-daemon/pkg/validators"
  7. )
  8. type Version string
  9. func NewVersion(version string) (Version, error) {
  10. if !PluginDeclarationVersionRegex.MatchString(version) {
  11. return "", fmt.Errorf("invalid version")
  12. }
  13. return Version(version), nil
  14. }
  15. func (v Version) String() string {
  16. return string(v)
  17. }
  18. const (
  19. VERSION_PATTERN = `\d{1,4}(\.\d{1,4}){2}(-\w{1,16})?`
  20. VERSION_X_PATTERN = `(\d{1,4}|[xX])`
  21. )
  22. var PluginDeclarationVersionRegex = regexp.MustCompile("^" + VERSION_PATTERN + "$")
  23. func isVersion(fl validator.FieldLevel) bool {
  24. // version format must be like x.x.x, at least 2 digits and most 5 digits, and it can be ends with a letter
  25. value := fl.Field().String()
  26. return PluginDeclarationVersionRegex.MatchString(value)
  27. }
  28. func init() {
  29. validators.GlobalEntitiesValidator.RegisterValidation("version", isVersion)
  30. }