validator crate for constraint checking. The #[dto] macro derives both automatically; ValidationPipe or ValidatedBody<T> runs the validation before your handler receives the data. Invalid payloads return 422 Unprocessable Entity with a structured error body.
#[dto]
Derives serde::Deserialize, validator::Validate, and NestDto on a struct. By default it also emits #[serde(deny_unknown_fields)] so any JSON key not in the struct definition causes a 422 error.
#[dto(allow_unknown_fields)]
Opts out of deny_unknown_fields. Use this when you intentionally accept JSON payloads from forward-compatible clients that may include extra fields.
NestDto trait
NestDto is a marker trait generated by #[dto]. It is used by ValidationPipe and the ValidatedBody<T>, ValidatedQuery<T>, and ValidatedPath<T> extractors to enforce that only DTOs marked with #[dto] are passed through the validation pipeline.
NestDto manually; #[dto] handles it.
Using DTOs in handlers
- ValidatedBody extractor
- #[param::body] + ValidationPipe
- ValidatedQuery extractor
Field validation attributes
All field attributes are applied inside a#[dto] struct. They expand to validator crate constraint annotations.
String constraints
Validates that the field is a non-empty string. Useful as a presence check when used with
String.Validates that the field is a well-formed email address.
Validates that the field is a well-formed URL.
Validates string length. Both
min and max are optional.Numeric constraints
Validates that the field is an integer type (
i8–i128, u8–u128, isize, usize).Validates that the field is a numeric type (also accepts
f32, f64).Validates that the numeric field is greater than or equal to
N.Validates that the numeric field is less than or equal to
N.Boolean and optional
Validates that the field is a
bool.Marks the field as optional in the validation pipeline. Wrap the field type in
Option<T> and add this attribute so that a missing JSON key is accepted without triggering other validators on the field.Nested DTOs
Runs validation recursively on a nested struct field. The nested type must also derive
Validate (which #[dto] provides).Comprehensive example
The following DTO covers string, numeric, boolean, optional, and nested validation in a single type:deny_unknown_fields default behavior
#[dto] applies #[serde(deny_unknown_fields)] by default. This means any JSON key in the request body that is not a field on the struct causes deserialization to fail with a 422 response before validation even runs.
ValidationPipe behavior with whitelist: true. Use #[dto(allow_unknown_fields)] when you need to accept extra fields from clients on a different schema version.