-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,64 @@ | ||
pub mod fields; | ||
|
||
use std::collections::HashMap; | ||
use std::future::Future; | ||
|
||
use crate::core::request::Request; | ||
|
||
use self::fields::AbstractFields; | ||
|
||
pub mod fields; | ||
|
||
pub type FormFields = Vec<Box<dyn AbstractFields>>; | ||
|
||
pub type ValidationError = HashMap<String, Vec<String>>; | ||
|
||
pub trait FormValidator { | ||
fn new() -> Self; | ||
fn form_fields(&mut self) -> FormFields; | ||
fn validate(&mut self, request: &Request) { | ||
fn validate( | ||
&mut self, | ||
request: &Request, | ||
) -> Box<dyn Future<Output = Result<(), ValidationError>> + '_> { | ||
let request = request.clone(); | ||
// let (form_data, files) = request.parse().await; | ||
// | ||
// for field in self.form_fields() { | ||
// | ||
// // field.validate(, files); | ||
// } | ||
|
||
Box::new(Box::pin(async move { | ||
let (mut form_data, mut files) = request.parse().await; | ||
|
||
let mut errors = HashMap::new(); | ||
|
||
for mut field in self.form_fields() { | ||
let field_name = field.field_name().await; | ||
|
||
let result; | ||
if let Some(custom_validate_result) = | ||
self.custom_validate(&request, &field_name, &field).await | ||
{ | ||
result = custom_validate_result; | ||
} else { | ||
result = field.validate(&mut form_data, &mut files).await; | ||
} | ||
|
||
match result { | ||
Ok(()) => {} | ||
Err(error) => { | ||
errors.insert(field_name, error); | ||
} | ||
} | ||
} | ||
|
||
if errors.len() > 0 { | ||
return Err(errors); | ||
} | ||
|
||
Ok(()) | ||
})) | ||
} | ||
|
||
fn custom_validate( | ||
&mut self, | ||
_: &Request, | ||
_: &String, | ||
_: &Box<dyn AbstractFields>, | ||
) -> Box<dyn Future<Output = Option<Result<(), Vec<String>>>> + Sync + Unpin + 'static> { | ||
Box::new(Box::pin(async move { None })) | ||
} | ||
fn custom_validate(&mut self, request: &Request) {} | ||
} |