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:
| Tool | Scope | Example |
|---|---|---|
*_msg(...) builders | one check | vld::string().min_msg(3, "Too short") |
with_messages(...) | many checks of one schema, by key | strings, numbers |
type_error("...") | wrong JSON type | most primitives |
.message("...") | every issue of the wrapped schema | any 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_msgand 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 alsonot_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.