pub struct Accumulator { /* private fields */ }Expand description
Type used to build up a list of validation failures.
When validating fields of a struct while validating that struct,
push the field’s name (or index in a sequence)
onto the prefix member of the accumulator
before passing it in to the validator.
This allows nested fields to report where the failure happened.
use validatrix::Accumulator;
fn accumulate(accum: &mut Accumulator) {
accum.with_key("some_field", |a| if true == false { a.add_failure("pigs have flown") });
// equivalent to the above
if true == false {
accum.add_failure_at("some_field", "hell has frozen over");
}
}Implementations§
Source§impl Accumulator
impl Accumulator
Sourcepub fn add_failure(&mut self, message: impl Into<String>)
pub fn add_failure(&mut self, message: impl Into<String>)
Add an extra failure to this accumulator.
Sourcepub fn add_failure_at(
&mut self,
prefix: impl Into<Key>,
message: impl Into<String>,
)
pub fn add_failure_at( &mut self, prefix: impl Into<Key>, message: impl Into<String>, )
Accumulate an extra failure at the given key.
Sourcepub fn validate_member_at(
&mut self,
field: impl Into<Key>,
member: &impl Validate,
)
pub fn validate_member_at( &mut self, field: impl Into<Key>, member: &impl Validate, )
Accumulate any validation errors for a Validate field with key field.
Sourcepub fn validate_member_at_ctx<T: ValidateContext>(
&mut self,
field: impl Into<Key>,
member: &T,
context: &T::Context,
)
pub fn validate_member_at_ctx<T: ValidateContext>( &mut self, field: impl Into<Key>, member: &T, context: &T::Context, )
Like Self::validate_member_at, but for a crate::ValidateContext field with the given context.
Sourcepub fn with_key(&mut self, prefix: impl Into<Key>, f: impl FnOnce(&mut Self))
pub fn with_key(&mut self, prefix: impl Into<Key>, f: impl FnOnce(&mut Self))
Perform manual validation inside the given closure for a member with the given prefix.
The closure takes an accumulator as an argument, which will be this accumulator with the added prefix.
Sourcepub fn with_keys(&mut self, prefixes: &[Key], f: impl FnOnce(&mut Self))
pub fn with_keys(&mut self, prefixes: &[Key], f: impl FnOnce(&mut Self))
Convenience method for Accumulator::with_key-like behaviour at multiple keys’ depth.
Sourcepub fn validate_iter<'a, V: Validate + 'a, I: IntoIterator<Item = &'a V>>(
&mut self,
items: I,
)
pub fn validate_iter<'a, V: Validate + 'a, I: IntoIterator<Item = &'a V>>( &mut self, items: I, )
Iterate over a collection of Validate-able items, validating them all in turn. As this tracks the items’ index in the iterable, the whole collection should be passed rather than a filtered version.
Sourcepub fn validate_iter_ctx<'a, V: ValidateContext + 'a, I: IntoIterator<Item = &'a V>>(
&mut self,
items: I,
context: &V::Context,
)
pub fn validate_iter_ctx<'a, V: ValidateContext + 'a, I: IntoIterator<Item = &'a V>>( &mut self, items: I, context: &V::Context, )
Like Self::validate_iter, but for a collection of crate::ValidateContext items with the given context.
Sourcepub fn validate_iter_at<'a, V: Validate + 'a, I: IntoIterator<Item = &'a V>>(
&mut self,
prefix: impl Into<Key>,
items: I,
)
pub fn validate_iter_at<'a, V: Validate + 'a, I: IntoIterator<Item = &'a V>>( &mut self, prefix: impl Into<Key>, items: I, )
Convenience method to do Self::validate_iter for a given key.
Sourcepub fn validate_iter_at_ctx<'a, V: ValidateContext + 'a, I: IntoIterator<Item = &'a V>>(
&mut self,
prefix: impl Into<Key>,
items: I,
context: &V::Context,
)
pub fn validate_iter_at_ctx<'a, V: ValidateContext + 'a, I: IntoIterator<Item = &'a V>>( &mut self, prefix: impl Into<Key>, items: I, context: &V::Context, )
Like Self::validate_iter_at, but for a collection of crate::ValidateContext items with the given context.