All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references核心组件数据校验数据校验-校验对象.md

校验对象

数据校验组件提供了数据校验对象,用于数据校验的统一的配置管理、便捷的链式操作。

接口文档: https://pkg.go.dev/github.com/gogf/gf/v2/util/gvalid

type Validator
    func New() *Validator
    func (v *Validator) Assoc(assoc interface{}) *Validator
    func (v *Validator) Bail() *Validator
    func (v *Validator) Ci() *Validator
    func (v *Validator) Clone() *Validator
    func (v *Validator) Data(data interface{}) *Validator
    func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator
    func (v *Validator) Messages(messages interface{}) *Validator
    func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator
    func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator
    func (v *Validator) Rules(rules interface{}) *Validator
    func (v *Validator) Run(ctx context.Context) Error

简要说明:

  1. New方法用于创建一个新的校验对象。
  2. Assoc用于关联数据校验,具体请查看后续章节介绍。
  3. Bail方法用于设定只要后续的多个校验中有一个规则校验失败则停止校验立即返回错误结果。
  4. Ci方法用于设置需要比较数值的规则时,不区分字段的大小写。
  5. Run方法对给定规则和信息的数据进行校验操作。
  6. I18n方法用于设置当前校验对象的I18N国际化组件。默认情况下,校验组件使用的是框架全局默认的i18n组件对象。
  7. Data方法用于传递需要联合校验的数据集合,往往传递的是map类型或者struct类型。
  8. Rules方法用于传递当前链式操作校验的自定义校验规则,往往使用[]string类型或者map类型。
  9. Messages方法用于传递当前链式操作校验的自定义错误提示信息,往往使用map类型传递,具体看后续代码示例。 :::tip 由于数据校验对象也是一个非常常用的对象,因此g模块中定义了Validator方法来快捷创建校验对象。大部分场景下我们推荐使用g模块的g.Validator()方式来快捷创建一个校验对象,关于g模块的介绍请参考章节:对象管理 :::

使用示例

单数据校验

var (
    err error
    ctx = gctx.New()
)
err = g.Validator().
    Rules("min:18").
    Data(16).
    Messages("未成年人不允许注册哟").
    Run(ctx)
fmt.Println(err.Error())

// Output:
// 未成年人不允许注册哟
var (
    err  error
    ctx  = gctx.New()
    data = g.Map{
        "password": "123",
    }
)

err = g.Validator().Data("").Assoc(data).
    Rules("required-with:password").
    Messages("请输入确认密码").
    Run(ctx)

fmt.Println(err.Error())

Struct 数据校验

type User struct {
    Name string `v:"required#请输入用户姓名"`
    Type int    `v:"required#请选择用户类型"`
}
var (
    err  error
    ctx  = gctx.New()
    user = User{}
    data = g.Map{
        "name": "john",
    }
)
if err = gconv.Scan(data, &user); err != nil {
    panic(err)
}
err = g.Validator().Assoc(data).Data(user).Run(ctx)
if err != nil {
    fmt.Println(err.(gvalid.Error).Items())
}

// Output:
// [map[Type:map[required:请选择用户类型]]]

Map 数据校验

params := map[string]interface{}{
    "passport":  "",
    "password":  "123456",
    "password2": "1234567",
}
rules := map[string]string{
    "passport":  "required|length:6,16",
    "password":  "required|length:6,16|same:password2",
    "password2": "required|length:6,16",
}
messages := map[string]interface{}{
    "passport": "账号不能为空|账号长度应当在{min}到{max}之间",
    "password": map[string]string{
        "required": "密码不能为空",
        "same":     "两次密码输入不相等",
    },
}
err := g.Validator().Messages(messages).Rules(rules).Data(params).Run(gctx.New())
if err != nil {
    g.Dump(err.Maps())
}

执行后,终端输出:

{
    "passport": {
        "length": "账号长度应当在6到16之间",
        "required": "账号不能为空"
    },
    "password": {
        "same": "两次密码输入不相等"
    }
}

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.