Skip to content

Custom lang file ​

All error texts can be replaced with your own — just like lang files in Laravel: copy the file into your project, edit it and load it.

1. Copy a lang file ​

bash
npx tinyval lang ru

The command creates lang/tinyval/ru.ts with all texts. Available: en, ru, zh.

OptionWhat it does
--out <dir>Where to put the file. Default: lang/tinyval
--jsCreate .js instead of .ts
--forceOverwrite an existing file

2. Edit the texts ​

ts
// lang/tinyval/ru.ts
import type { Messages } from 'tinyval';

export default {
  required: 'Поле «{label}» обязательно',
  minLength: '{label}: минимум {min} символов',
  email: 'Проверьте адрес почты',
  // ...
} satisfies Messages;

satisfies Messages makes TypeScript check that the file has every error code and suggests their names. The $locale key ('ru', 'en'…) sets the language for plural forms like {min|character|characters}. To change just a few texts you don't need the whole file — setMessages with those keys is enough.

3. Load it ​

ts
import { setMessages } from 'tinyval';
import messages from './lang/tinyval/ru';

setMessages(messages);

Once on startup. If there are several languages and the language depends on the request, pass the file to the call: validate(schema, data, { messages }).

Texts for specific fields ​

Besides codes, a lang file can have "field.code" keys — like custom in Laravel. For array items use * instead of the index:

ts
export default {
  ...ru,
  'email.required': 'Укажите почту — на неё придёт письмо',
  'password.minLength': 'Пароль слишком простой',
  'items.*.qty.min': 'Количество — от {min}',
} satisfies Messages;

Text priority: the message in the schema → "field.code" → code. At each step the call's messages option is checked first, then setMessages.

Field names ​

Names for {label} are set for the whole app with setLabels — like attributes in Laravel:

ts
setLabels({ email: 'Почта', 'items.*.qty': 'Количество' });

See also ​