create_role.go 1.1 KB
package command

import (
	"fmt"
	"reflect"
	"strings"

	"github.com/beego/beego/v2/core/validation"
)

type CreateRoleCommand struct {
	// 用户ID
	UserId int64 `cname:"用户ID" json:"userId" valid:"Required`
	// 组织ID
	OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required`
	// 角色名称
	RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
	// 描述
	Desc string `cname:"描述" json:"desc,omitempty"`
}

func (createRoleCommand *CreateRoleCommand) Valid(validation *validation.Validation) {
	//validation.SetError("CustomValid", "未实现的自定义认证")
}

func (createRoleCommand *CreateRoleCommand) ValidateCommand() error {
	valid := validation.Validation{}
	b, err := valid.Valid(createRoleCommand)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(createRoleCommand).Elem()
		for _, validErr := range valid.Errors {
			field, isExist := elem.FieldByName(validErr.Field)
			if isExist {
				return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
			} else {
				return fmt.Errorf(validErr.Message)
			}
		}
	}
	return nil
}