generated from mitchell/rust_template
validation builder additions
This commit is contained in:
parent
bf04957370
commit
e7fd8ec7e1
@ -1,4 +1,4 @@
|
|||||||
use crate::controls::ValidationFn;
|
use crate::{controls::ValidationFn, FormToolData};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
/// A helper builder that allows you to specify a validation function
|
/// A helper builder that allows you to specify a validation function
|
||||||
@ -8,23 +8,25 @@ use std::fmt::Display;
|
|||||||
/// closures, but for simple validation function this builder can be helpful
|
/// closures, but for simple validation function this builder can be helpful
|
||||||
///
|
///
|
||||||
/// Validations are run in the order that they are called in the builder.
|
/// Validations are run in the order that they are called in the builder.
|
||||||
pub struct ValidationBuilder<T: 'static> {
|
pub struct ValidationBuilder<FD: FormToolData, T: 'static> {
|
||||||
/// The name of the field, for error messages.
|
/// The name of the field, for error messages.
|
||||||
name: String,
|
name: String,
|
||||||
|
/// The getter function for the field to validate.
|
||||||
|
field_fn: Box<dyn Fn(&FD) -> &T + 'static>,
|
||||||
/// The functions to be called when validating.
|
/// The functions to be called when validating.
|
||||||
functions: Vec<Box<dyn Fn(&str, &T) -> Result<(), String> + 'static>>,
|
functions: Vec<Box<dyn Fn(&str, &T) -> Result<(), String> + 'static>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> Default for ValidationBuilder<T> {
|
impl<FD: FormToolData, T: 'static> ValidationBuilder<FD, T> {
|
||||||
fn default() -> Self {
|
/// Creates a new empty [`ValidationBuilder`] on the given field.
|
||||||
|
pub fn for_field(field_fn: impl Fn(&FD) -> &T + 'static) -> Self {
|
||||||
ValidationBuilder {
|
ValidationBuilder {
|
||||||
name: String::from("Field"),
|
name: String::from("Field"),
|
||||||
|
field_fn: Box::new(field_fn),
|
||||||
functions: Vec::new(),
|
functions: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: 'static> ValidationBuilder<T> {
|
|
||||||
/// The name of the field that is being validated.
|
/// The name of the field that is being validated.
|
||||||
///
|
///
|
||||||
/// This is the name that will be used for error messages.
|
/// This is the name that will be used for error messages.
|
||||||
@ -33,9 +35,19 @@ impl<T: 'static> ValidationBuilder<T> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a custom validation function.
|
||||||
|
///
|
||||||
|
/// The function should take the value as an argument and return
|
||||||
|
/// a [`Result<(), String>`], just like any other validation function.
|
||||||
|
pub fn custom(mut self, f: impl ValidationFn<T>) -> Self {
|
||||||
|
self.functions.push(Box::new(move |_name, value| f(value)));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Builds the action validation function.
|
/// Builds the action validation function.
|
||||||
pub fn build(self) -> impl ValidationFn<T> {
|
pub fn build(self) -> impl ValidationFn<FD> {
|
||||||
move |value| {
|
move |form_data| {
|
||||||
|
let value = (self.field_fn)(form_data);
|
||||||
for f in self.functions.iter() {
|
for f in self.functions.iter() {
|
||||||
match f(self.name.as_str(), value) {
|
match f(self.name.as_str(), value) {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
@ -47,7 +59,7 @@ impl<T: 'static> ValidationBuilder<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ValidationBuilder<String> {
|
impl<FD: FormToolData> ValidationBuilder<FD, String> {
|
||||||
/// Requires the field to not be empty.
|
/// Requires the field to not be empty.
|
||||||
pub fn required(mut self) -> Self {
|
pub fn required(mut self) -> Self {
|
||||||
self.functions.push(Box::new(move |name, value| {
|
self.functions.push(Box::new(move |name, value| {
|
||||||
@ -75,7 +87,7 @@ impl ValidationBuilder<String> {
|
|||||||
/// Requires the field's length to be less than or equal to `min_len`.
|
/// Requires the field's length to be less than or equal to `min_len`.
|
||||||
pub fn max_len(mut self, max_len: usize) -> Self {
|
pub fn max_len(mut self, max_len: usize) -> Self {
|
||||||
self.functions.push(Box::new(move |name, value| {
|
self.functions.push(Box::new(move |name, value| {
|
||||||
if value.len() < max_len {
|
if value.len() > max_len {
|
||||||
Err(format!("{} must be <= {} characters", name, max_len))
|
Err(format!("{} must be <= {} characters", name, max_len))
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -85,7 +97,7 @@ impl ValidationBuilder<String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: PartialOrd<T> + Display + 'static> ValidationBuilder<T> {
|
impl<FD: FormToolData, T: PartialOrd<T> + Display + 'static> ValidationBuilder<FD, T> {
|
||||||
/// Requires the value to be at least `min_value` according to
|
/// Requires the value to be at least `min_value` according to
|
||||||
/// `PartialOrd`.
|
/// `PartialOrd`.
|
||||||
pub fn min_value(mut self, min_value: T) -> Self {
|
pub fn min_value(mut self, min_value: T) -> Self {
|
||||||
@ -113,7 +125,7 @@ impl<T: PartialOrd<T> + Display + 'static> ValidationBuilder<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: PartialEq<T> + Display + 'static> ValidationBuilder<T> {
|
impl<FD: FormToolData, T: PartialEq<T> + Display + 'static> ValidationBuilder<FD, T> {
|
||||||
/// Requires the field to be in the provided whitelist.
|
/// Requires the field to be in the provided whitelist.
|
||||||
pub fn whitelist(mut self, whitelist: Vec<T>) -> Self {
|
pub fn whitelist(mut self, whitelist: Vec<T>) -> Self {
|
||||||
self.functions.push(Box::new(move |name, value| {
|
self.functions.push(Box::new(move |name, value| {
|
||||||
|
|||||||
Reference in New Issue
Block a user