Skip to content

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 ​

ParameterTypeDescription
schemaSchema<T>The field schema
textstringThe field name

Notes ​

  • Built-in messages don't use {label} — add it to your texts via setMessages or the messages option.
  • Without label, {label} is the field name: email, or 0 for 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 labels option of validate — 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 ​