use async_trait::async_trait;
use crate::errors::FieldErrors;
#[async_trait]
pub trait FormValidator<F: Form, C: Send + 'static> {
type Error: Send + 'static;
async fn validate(&self, form: &mut F, context: &mut C) -> Result<(), Self::Error>;
}
#[async_trait]
pub trait FieldValidator<C: Send + 'static> {
type Error: Send + 'static;
async fn validate(
&self,
value: &str,
errors: &mut FieldErrors,
context: &mut C,
) -> Result<(), Self::Error>;
}
pub trait Form: Sized + 'static {
type Partial: FormPartial<Form = Self>;
}
#[async_trait]
pub trait FormPartial {
type Form: Form<Partial = Self>;
type Validation: FormValidation<Partial = Self>;
type Error: Send + 'static;
fn form(&self) -> Result<Self::Form, &'static str>
where
Self: Sized;
async fn validate(&self) -> Result<Self::Validation, Self::Error>
where
Self: Sized;
const INFO: &'static FormPartialInfo;
fn validator_info(&self) -> &'static FormPartialInfo {
Self::INFO
}
}
pub trait FormValidation {
type Partial: FormPartial<Validation = Self>;
fn is_valid(&self) -> bool;
}
pub struct FormPartialInfo {
pub form_validators: &'static [&'static str],
pub fields: &'static [FieldInfo],
}
pub struct FieldInfo {
pub name: &'static str,
pub validators: &'static [FieldValidatorInfo],
}
pub enum FieldValidatorInfo {
MinLength(u32),
MaxLength(u32),
MinValue(i64),
MaxValue(i64),
Required,
Email,
Regex(&'static str),
Custom(&'static str),
}