Skip to content

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 ​

FieldDescription
StructValuereflect.Value of the surrounding struct. Use this to access sibling fields by name.
FieldNameGo field name (not the label).
FieldTypereflect.Type of the field.
FieldValueThe actual value, ready to type-assert. May be nil for nil pointer/interface fields.
FieldLabelResolved label string (locale-aware).
TemplateLanguageActive locale for the current Check call.
RulePointer 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.

Released under the MIT License.