Errors

Custom messages

Per-check messages, bulk overrides, type errors and whole-schema messages.

There are four ways to change a message. From the most to the least specific:

ToolScopeExample
*_msg(...) buildersone checkvld::string().min_msg(3, "Too short")
with_messages(...)many checks of one schema, by keystrings, numbers
type_error("...")wrong JSON typemost primitives
.message("...")every issue of the wrapped schemaany schema
use vld::prelude::*;

let username = vld::string()
    .type_error("Username must be text")
    .min_msg(3, "At least 3 characters")
    .max_msg(20, "At most 20 characters")
    .alphanumeric_msg("Letters and digits only");

let pin = vld::string().len(4).pattern(r"^\d+$")
    .message("PIN must be 4 digits");

A per-check _msg wins over with_messages. with_messages is consulted when an issue is built, so it also covers checks added after it; a later call replaces it.

// Override many checks by key at once:
let code = vld::string().min(3).max(8).with_messages(|key| match key {
    "too_small" => Some("Too short".into()),
    "too_big" => Some("Too long".into()),
    _ => None,
});

Builders with a _msg twin

  • Strings: min_msg, max_msg, len_msg, non_empty_msg, starts_with_msg, ends_with_msg, contains_msg, pattern_msg, every format (email_msg, uuid_msg, url_strict_msg, iban_msg, …) and every password rule (require_digit_msg, max_repeat_msg(n, ..), password_strength_msg(score, ..), …).
  • Dates: past_msg, future_msg, timezone_offset_only_msg.
  • Any schema: rule_msg(rule, "...") (see rule message precedence).

with_messages keys

  • Strings: too_small, too_big (also min_bytes / max_bytes), invalid_length, non_empty, invalid_regex (for pattern), invalid_starts_with, invalid_ends_with, invalid_contains, invalid_<format> (invalid_email, invalid_url, invalid_country, invalid_country_alpha3, invalid_currency (for currency_code()), invalid_iban, invalid_bic, invalid_isbn, invalid_ean, invalid_vin, invalid_timezone, invalid_mime, invalid_object_id, invalid_regex_syntax, invalid_single_script, …), the password rules (invalid_password_lowercase, invalid_password_uppercase, invalid_password_digit, invalid_password_symbol, invalid_password_repeat, invalid_password_strength), and a rule's key.
  • Numbers (number(), integers, f32()): too_small, too_big (also the type-range failure of sized integers and f32), not_positive, not_negative, not_non_negative, not_non_positive, not_multiple_of, not_safe, invalid_enum_value (one_of); integers also not_int.

Integer-specific

vld::number().int().int_error("Whole numbers only") sets the message for a number with a fraction (with_messages key not_int does the same).

Refinements

refine(check, "message"), custom(|v| Err("message".into())) and super_refine take the message directly.

Messages and translations

Custom messages are kept by translate_error: only default English messages are translated. Write custom messages in the target language, or leave them out and use the catalogs (feature i18n: translator(locale), 29 languages). To change a default message in every schema at once, override its key in a catalog instead of a _msg on each check: "vld.too_small.string" in your app's locales/{locale}.json with Translator::new, or a MapResolver built from english(). A custom code (refine, a rule) is translated through vld.<code>.