CheckerContext
go
type CheckerContext struct {
StructValue reflect.Value
FieldName string
FieldType reflect.Type
FieldValue interface{}
FieldLabel string
TemplateLanguage language.Tag
Rule *rule
}The argument every checker receives. govalid populates it before calling your function — you don't construct it yourself in normal use.
Fields
| Field | Description |
|---|---|
StructValue | reflect.Value of the surrounding struct. Use this to access sibling fields by name. |
FieldName | Go field name (not the label). |
FieldType | reflect.Type of the field. |
FieldValue | The actual value, ready to type-assert. May be nil for nil pointer/interface fields. |
FieldLabel | Resolved label string (locale-aware). |
TemplateLanguage | Active locale for the current Check call. |
Rule | Pointer to the parsed rule. Rule.params is the []string of tag parameters. |
Recipes
Read a tag parameter
go
if len(c.Rule.params) == 0 {
return govalid.MakeCheckerParamError(c)
}
limit, err := strconv.Atoi(c.Rule.params[0])Look up a sibling field
go
sv := c.StructValue
for i := 0; i < sv.Type().NumField(); i++ {
if sv.Type().Field(i).Name == "Other" {
v := sv.Field(i).Interface()
// ...
}
}Type-assert with safety
go
v, ok := c.FieldValue.(string)
if !ok {
return govalid.MakeValueTypeError(c)
}Build a localized error
go
return govalid.NewErrorContext(c)NewErrorContext reads c.Rule.checker to look up the right template, applies c.TemplateLanguage, and prefixes c.FieldLabel. You only need to register the message via SetMessageTemplates once.