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.

Builders with a _msg twin

  • Strings: min_msg, max_msg, len_msg, non_empty_msg, starts_with_msg, ends_with_msg, contains_msg, regex_msg and every format (email_msg, uuid_msg, url_strict_msg, …).
  • Dates: past_msg, future_msg, timezone_offset_only_msg.

with_messages keys

  • Strings: too_small, too_big, invalid_length, non_empty, invalid_regex, invalid_starts_with, invalid_ends_with, invalid_contains, invalid_<format> (invalid_email, invalid_url, …).
  • Numbers (number(), integers, f32()): too_small, too_big, not_positive, not_negative, not_non_negative, not_non_positive, not_multiple_of, not_safe; integers also not_int.

Integer-specific

vld::number().int().int_error("Whole numbers only") sets the message for a number with a fraction.

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 translation tables.