Skip to content

ErrContext

go
type ErrContext struct {
    FieldName        string
    FieldLabel       string
    FieldValue       interface{}
    TemplateLanguage language.Tag
    // unexported template state
}

func (e *ErrContext) Error() string

func (e *ErrContext) SetTemplate(key string)
func (e *ErrContext) SetFieldLimitValue(v interface{})

The result type for every validation failure. *ErrContext implements the standard error interface.

Public fields

FieldDescription
FieldNameGo field name.
FieldLabelResolved label (locale-aware) used as the message prefix.
FieldValueThe original failing value.
TemplateLanguageThe locale used to render the message.

Error()

Returns the rendered message. The message is composed as FieldLabel + Template + FieldLimitValue, with hooks for {field} / {limit} placeholders and {{ … }} no-affix wrapping. See Error Messages for the full template grammar.

Mutators

These are typically used inside a custom checker to fine-tune the error before returning it.

SetTemplate(key string)

Swap the underlying template by key. Any previously set limit value is cleared so messages can never leak across templates.

go
ctx := govalid.NewErrorContext(c)
ctx.SetTemplate("alphadash") // borrow another rule's template
return ctx

SetFieldLimitValue(v interface{})

Set the {limit} value (and the implicit suffix). Built-ins do this for min/max/minlen/maxlen so the rendered message includes the threshold.

go
ctx := govalid.NewErrorContext(c)
ctx.SetFieldLimitValue(42)
return ctx

Constructors

You usually don't call these directly — return them from a custom checker.

go
func NewErrorContext(c CheckerContext) *ErrContext
func MakeUserDefinedError(msg string)  *ErrContext
func MakeValueTypeError(c CheckerContext) *ErrContext
func MakeCheckerParamError(c CheckerContext) *ErrContext
func MakeCheckerNotFoundError(c CheckerContext) *ErrContext
func MakeFieldNotFoundError(c CheckerContext) *ErrContext

Released under the MIT License.