Skip to content

Latest commit

 

History

History
58 lines (45 loc) · 1.45 KB

File metadata and controls

58 lines (45 loc) · 1.45 KB
title Localized Go Validation Messages
description Localize Valgo validation messages, override templates, and reuse validation options with a ValidationFactory in Go.

Valgo ships with localized messages (English default; also Spanish, German, Hungarian) and lets you provide your own locale entries.

Set a locale by code

val := v.New(v.Options{LocaleCode: "es"}).
  Check(v.String(" ", "nombre").Not().Blank())

Override locale entries

val := v.New(v.Options{
  Locale: &v.Locale{
    v.ErrorKeyNotBlank: "{{title}} should not be blank",
  },
}).Check(v.String(" ", "name").Not().Blank())

Add a new locale

val := v.New(v.Options{
  LocaleCode: "ee",
  Locale: &v.Locale{
    v.ErrorKeyNotBlank: "{{title}} ei tohi olla tuhi",
  },
}).Is(v.String(" ", "name").Not().Blank())

Custom validator fallback messages

Custom validators can provide default messages for their own error keys with ValidatorContext.WithLocaleFallback(...). Fallback entries are merged only when the active validation locale does not already define the key, so Options{Locale: ...} overrides still take precedence.

Factory()

Use a factory to set defaults (locale, custom marshaling) once.

factory := v.Factory(v.FactoryOptions{
  LocaleCodeDefault: "es",
  Locales: map[string]*v.Locale{
    "ee": {
      v.ErrorKeyNotBlank: "{{title}} ei tohi olla tuhi",
    },
  },
})

val := factory.Is(v.String(" ", "nombre").Not().Blank())