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 ¶
- func GetService(ctx context.Context) (datacontract.Validator, error)
- func MustGetService(ctx context.Context) datacontract.Validator
- func Validate(ctx context.Context, obj any) error
- func ValidateVar(ctx context.Context, field any, tag string) error
- type CustomRuleConfig
- type CustomValidateFunc
- type ValidationError
- type ValidationErrors
- type Validator
- type ValidatorConfig
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)
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 是校验器配置契约的顶层别名。