label
Sets the field name for the {label} parameter in error messages — like :attribute in Laravel.
Signature
ts
function label<T>(schema: Schema<T>, text: string): Schema<T>Parameters
| Parameter | Type | Description |
|---|---|---|
schema | Schema<T> | The field schema |
text | string | The field name |
Notes
- Built-in messages don't use
{label}— add it to your texts viasetMessagesor themessagesoption. - Without
label,{label}is the field name:email, or0for an array item. - The name applies only to errors of the field itself, not to nested fields.
- For a multilingual app it's easier to pass names with the
labelsoption ofvalidate— separately for each language.
Examples
Names in the schema
ts
const Signup = object({
email: label(pipe(string(), email()), 'Email'),
password: label(pipe(string(), minLength(8)), 'Password'),
});
setMessages({ required: '{label} is required', minLength: '{label} must be at least {min} characters' });
validate(Signup, { password: '123' }).errors;
// { email: 'Email is required', password: 'Password must be at least 8 characters' }Names for different languages
ts
const labels = {
ru: { email: 'Почта', 'address.city': 'Город' },
en: { email: 'Email', 'address.city': 'City' },
};
validate(Order, body, { messages: locales[lang], labels: labels[lang] });See also
- Messages & i18n
- validate — the
messagesandlabelsoptions