Skip to main content

validate_args

Macro validate_args 

Source
macro_rules! validate_args {
    ($($field:ident => $schema:expr),* $(,)?) => { ... };
}
Expand description

Validate server function arguments inline.

Each argument is validated against its schema. All errors are accumulated and returned as a VldServerError.

§Example

#[server]
async fn create_user(name: String, email: String, age: i64) -> Result<(), ServerFnError> {
    vld_dioxus::validate_args! {
        name  => vld::string().min(2).max(50),
        email => vld::string().email(),
        age   => vld::number().int().min(0).max(150),
    }.map_err(|e| ServerFnError::new(e.to_string()))?;

    // ... all arguments are valid
    Ok(())
}

§Shared Schemas

Define schema factories to share between server and client:

// shared.rs — compiles for both server and WASM
pub fn name_schema() -> impl vld::schema::VldSchema<Output = String> {
    vld::string().min(2).max(50)
}

// server function
vld_dioxus::validate_args! {
    name => shared::name_schema(),
}.map_err(|e| ServerFnError::new(e.to_string()))?;

// client component (use_memo)
let err = use_memo(move || vld_dioxus::check_field(&name(), &shared::name_schema()));