validate

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 3 Imported by: 0

README

validate

该目录只是对外公开的便捷转发层。

  • 面向业务代码提供 gorp/validate 入口
  • 真实实现仍在 framework/*
  • 不在这里承载独立校验实现或新语义

Documentation

Overview

Application scenarios: - Expose the unified validation capability through a top-level convenience package. - Re-export validator contracts so business code can depend on short package paths. - Provide context-based helper functions for object and field validation.

适用场景: - 通过顶层便捷包暴露统一校验能力。 - 重新导出校验器契约,让业务代码可以依赖更短的包路径。 - 提供基于 context 的对象校验和字段校验 helper。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetService added in v0.1.4

func GetService(ctx context.Context) (datacontract.Validator, error)

GetService 从容器获取统一校验器。 通过 context 解析容器后按 ValidatorKey 查找校验器实例, 找不到或容器不可用时返回 error。

Example:

validatorSvc, err := validate.GetService(ctx)

func MustGetService added in v0.1.4

func MustGetService(ctx context.Context) datacontract.Validator

MustGetService 从容器获取统一校验器,失败时 panic。 适用于启动阶段或明确期望校验器一定已注册的场景。

Example:

validatorSvc := validate.MustGetService(ctx)

func Validate

func Validate(ctx context.Context, obj any) error

Validate 使用容器中的校验器校验对象。 通过 context 解析容器获取校验器,然后调用其 Validate 方法完成结构体校验。 如果容器不可用或校验器未注册,返回 error。

Example:

err := validate.Validate(ctx, &CreateUserRequest{Name: "alice"})

func ValidateVar

func ValidateVar(ctx context.Context, field any, tag string) error

ValidateVar 使用容器中的校验器校验单个字段值。 通过 context 解析容器获取校验器,然后调用其 ValidateVar 方法完成字段校验。 tag 参数为校验规则标签,如 "required,email"。

Example:

err := validate.ValidateVar(ctx, "alice@example.com", "required,email")

Types

type CustomRuleConfig

type CustomRuleConfig = datacontract.CustomRuleConfig

CustomRuleConfig is the top-level alias of the custom rule config contract.

CustomRuleConfig 是自定义规则配置契约的顶层别名。

type CustomValidateFunc

type CustomValidateFunc = datacontract.CustomValidateFunc

CustomValidateFunc is the top-level alias of the custom validate callback contract.

CustomValidateFunc 是自定义校验回调契约的顶层别名。

type ValidationError

type ValidationError = datacontract.ValidationError

ValidationError is the top-level alias of the validation error contract.

ValidationError 是校验错误契约的顶层别名。

type ValidationErrors

type ValidationErrors = datacontract.ValidationErrors

ValidationErrors is the top-level alias of the validation errors contract.

ValidationErrors 是校验错误集合契约的顶层别名。

type Validator

type Validator = datacontract.Validator

Validator is the top-level alias of the unified validator contract.

Validator 是统一校验器契约的顶层别名。

type ValidatorConfig

type ValidatorConfig = datacontract.ValidatorConfig

ValidatorConfig is the top-level alias of the validator config contract.

ValidatorConfig 是校验器配置契约的顶层别名。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL