pub struct Binder { /* private fields */ }Expand description
Accumulates field-level binding failures into one 400 response.
Binding is best-effort: a bad field records an error and yields None, the
remaining fields still bind, and the request is rejected once at the end
with every failure listed — matching ASP.NET Core’s ModelState.
Implementations§
Source§impl Binder
impl Binder
Sourcepub fn has_errors(&self) -> bool
pub fn has_errors(&self) -> bool
Whether any field failed.
Sourcepub fn errors(&self) -> &[FieldError]
pub fn errors(&self) -> &[FieldError]
The recorded failures, in binding order.
Sourcepub fn into_error(self) -> Error
pub fn into_error(self) -> Error
Consume the binder, yielding the 400 ProblemDetails error.
The response body carries errors as { "field": ["message"] }.
Sourcepub fn invalid(&mut self, field: &str, kind: &str, value: &str)
pub fn invalid(&mut self, field: &str, kind: &str, value: &str)
Record an unparsable value for field.
Sourcepub fn unknown_field(&mut self, name: &str)
pub fn unknown_field(&mut self, name: &str)
Record a JSON key that no field claimed (deny_unknown_fields).
Sourcepub fn required<T: FromStr>(
&mut self,
raw: Option<&str>,
field: &str,
kind: &str,
) -> Option<T>
pub fn required<T: FromStr>( &mut self, raw: Option<&str>, field: &str, kind: &str, ) -> Option<T>
Parse a required scalar.
Returns None (and records an error) when the value is absent or
unparsable; the caller checks Binder::has_errors before building the
request.
Sourcepub fn optional<T: FromStr>(
&mut self,
raw: Option<&str>,
field: &str,
kind: &str,
) -> Option<T>
pub fn optional<T: FromStr>( &mut self, raw: Option<&str>, field: &str, kind: &str, ) -> Option<T>
Parse an optional scalar: absent is None, present-but-invalid is an
error (never silently None).
Sourcepub fn list<T: FromStr>(
&mut self,
raw: &[&str],
field: &str,
kind: &str,
) -> Vec<T>
pub fn list<T: FromStr>( &mut self, raw: &[&str], field: &str, kind: &str, ) -> Vec<T>
Parse every occurrence of a repeated value into a list.
An empty list is valid (ASP.NET Core binds missing collections as empty); unparsable elements are errors.
Sourcepub fn take<T>(&mut self, value: Option<T>, field: &str) -> Option<T>
pub fn take<T>(&mut self, value: Option<T>, field: &str) -> Option<T>
Record a “required” failure for an already-extracted optional value.
Used for body fields: serde has already validated the body, so a
missing value here means the field was skipped by the shadow struct.
Sourcepub fn required_file(
&mut self,
file: Option<&FormFile>,
field: &str,
) -> Option<FormFile>
pub fn required_file( &mut self, file: Option<&FormFile>, field: &str, ) -> Option<FormFile>
The first uploaded file, required.