Explorar o código

feat: verion regexr

Yeuoly hai 1 ano
pai
achega
600a7cec69

+ 17 - 12
internal/types/entities/plugin_entities/plugin_declaration.go

@@ -1,9 +1,11 @@
 package plugin_entities
 
 import (
+	"regexp"
 	"time"
 
 	"github.com/go-playground/validator/v10"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/validators"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
 )
 
@@ -73,7 +75,7 @@ type PluginDeclarationExecution struct {
 }
 
 type PluginDeclaration struct {
-	Version   string                     `json:"version" yaml:"version" validate:"required"`
+	Version   string                     `json:"version" yaml:"version" validate:"required,version"`
 	Type      DifyManifestType           `json:"type" yaml:"type" validate:"required,eq=plugin"`
 	Author    string                     `json:"author" yaml:"author" validate:"required"`
 	Name      string                     `json:"name" yaml:"name" validate:"required" enum:"plugin"`
@@ -83,21 +85,24 @@ type PluginDeclaration struct {
 	Execution PluginDeclarationExecution `json:"execution" yaml:"execution" validate:"required"`
 }
 
+var (
+	plugin_declaration_version_regex = regexp.MustCompile(`^\d{1,4}(\.\d{1,4}){1,3}(-\w{1,16})?$`)
+)
+
+func isVersion(fl validator.FieldLevel) bool {
+	// version format must be like x.x.x, at least 2 digits and most 5 digits, can be ends with a letter
+	value := fl.Field().String()
+	return plugin_declaration_version_regex.MatchString(value)
+}
+
 func (p *PluginDeclaration) Identity() string {
 	return parser.MarshalPluginIdentity(p.Name, p.Version)
 }
 
-var (
-	plugin_declaration_validator = validator.New()
-)
-
 func init() {
 	// init validator
-	plugin_declaration_validator.RegisterValidation("plugin_declaration_platform_arch", isPluginDeclarationPlatformArch)
-}
-
-func (p *PluginDeclaration) Validate() error {
-	return plugin_declaration_validator.Struct(p)
+	validators.GlobalEntitiesValidator.RegisterValidation("plugin_declaration_platform_arch", isPluginDeclarationPlatformArch)
+	validators.GlobalEntitiesValidator.RegisterValidation("version", isVersion)
 }
 
 func UnmarshalPluginDeclarationFromYaml(data []byte) (*PluginDeclaration, error) {
@@ -106,7 +111,7 @@ func UnmarshalPluginDeclarationFromYaml(data []byte) (*PluginDeclaration, error)
 		return nil, err
 	}
 
-	if err := obj.Validate(); err != nil {
+	if err := validators.GlobalEntitiesValidator.Struct(obj); err != nil {
 		return nil, err
 	}
 
@@ -119,7 +124,7 @@ func UnmarshalPluginDeclarationFromJSON(data []byte) (*PluginDeclaration, error)
 		return nil, err
 	}
 
-	if err := obj.Validate(); err != nil {
+	if err := validators.GlobalEntitiesValidator.Struct(obj); err != nil {
 		return nil, err
 	}
 

+ 127 - 0
internal/types/entities/plugin_entities/plugin_declaration_test.go

@@ -0,0 +1,127 @@
+package plugin_entities
+
+import (
+	"testing"
+	"time"
+
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
+)
+
+func preparePluginDeclaration() PluginDeclaration {
+	return PluginDeclaration{
+		Version:   "0.0.1",
+		Type:      PluginType,
+		Name:      "test",
+		Author:    "test",
+		CreatedAt: time.Now(),
+		Resource: PluginResourceRequirement{
+			Memory:  1,
+			Storage: 1,
+			Permission: &PluginPermissionRequirement{
+				Tool: &PluginPermissionToolRequirement{
+					Enabled: true,
+				},
+				Model: &PluginPermissionModelRequirement{
+					Enabled: true,
+				},
+				Node: &PluginPermissionNodeRequirement{
+					Enabled: true,
+				},
+			},
+		},
+		Plugins: []string{},
+		Execution: PluginDeclarationExecution{
+			Install: "echo 'hello'",
+			Launch:  "echo 'hello'",
+		},
+	}
+}
+
+func TestPluginDeclarationFullTest(t *testing.T) {
+	declaration := preparePluginDeclaration()
+	declaration_bytes := parser.MarshalJsonBytes(declaration)
+
+	// unmarshal
+	new_declaration, err := parser.UnmarshalJsonBytes[PluginDeclaration](declaration_bytes)
+	if err != nil {
+		t.Errorf("failed to unmarshal declaration: %s", err.Error())
+		return
+	}
+
+	if new_declaration.Version != declaration.Version {
+		t.Errorf("version not equal")
+		return
+	}
+	if new_declaration.Type != declaration.Type {
+		t.Errorf("type not equal")
+		return
+	}
+	if new_declaration.Name != declaration.Name {
+		t.Errorf("name not equal")
+		return
+	}
+	if new_declaration.Author != declaration.Author {
+		t.Errorf("author not equal")
+		return
+	}
+	if new_declaration.CreatedAt.GoString() != declaration.CreatedAt.GoString() {
+		t.Errorf("created_at not equal")
+		return
+	}
+	if new_declaration.Resource.Memory != declaration.Resource.Memory {
+		t.Errorf("memory not equal")
+		return
+	}
+
+	if new_declaration.Resource.Permission == nil {
+		t.Errorf("permission is nil")
+		return
+	}
+
+	if new_declaration.Resource.Permission.Tool == nil {
+		t.Errorf("tool permission is nil")
+		return
+	}
+
+	if new_declaration.Resource.Permission.Node == nil {
+		t.Errorf("node permission is nil")
+		return
+	}
+
+}
+
+func TestPluginDeclarationIncorrectVersion(t *testing.T) {
+	declaration := preparePluginDeclaration()
+	declaration.Version = "1"
+	declaration_bytes := parser.MarshalJsonBytes(declaration)
+
+	_, err := parser.UnmarshalJsonBytes[PluginDeclaration](declaration_bytes)
+	if err == nil {
+		t.Errorf("failed to validate version")
+		return
+	}
+}
+
+func TestPluginDeclarationIncorrectType(t *testing.T) {
+	declaration := preparePluginDeclaration()
+	declaration.Type = "test"
+	declaration_bytes := parser.MarshalJsonBytes(declaration)
+
+	_, err := parser.UnmarshalJsonBytes[PluginDeclaration](declaration_bytes)
+	if err == nil {
+		t.Errorf("failed to validate type")
+		return
+	}
+}
+
+func TestPluginDeclarationIncorrectName(t *testing.T) {
+	declaration := preparePluginDeclaration()
+	declaration.Name = ""
+	declaration_bytes := parser.MarshalJsonBytes(declaration)
+
+	_, err := parser.UnmarshalJsonBytes[PluginDeclaration](declaration_bytes)
+	if err == nil {
+		t.Errorf("failed to validate name")
+		return
+	}
+}