UI frameworks
Server functions (Leptos, Dioxus)
vld::form — check server-function arguments on the server and form fields in the browser with the same schemas, for Leptos, Dioxus or any other framework.
vld::form lets the same vld schemas check server-function arguments on the server and form fields in the browser. It is part of vld itself (no feature) and has no Leptos or Dioxus dependency, so it works with any version of either, and builds for native and WASM targets.
Cargo.toml
[dependencies]
vld = "0.8"
Example
Leptos:
use leptos::prelude::*;
// Shared rules, compiled for the server and the client.
fn name_schema() -> vld::primitives::ZString { vld::string().min(2).max(50) }
fn email_schema() -> vld::primitives::ZString { vld::string().email() }
#[server]
async fn create_user(name: String, email: String) -> Result<(), ServerFnError> {
vld::form::validate_args! {
name => name_schema(),
email => email_schema(),
}
.map_err(|e| ServerFnError::new(e.to_string()))?;
Ok(())
}
#[component]
fn CreateUserForm() -> impl IntoView {
let (name, set_name) = signal(String::new());
let name_error = Memo::new(move |_| vld::form::check_field(&name.get(), &name_schema()));
// ... render the input and `name_error`
}
Dioxus is the same with its own signals:
#[component]
fn CreateUserForm() -> Element {
let name = use_signal(String::new);
let name_error = use_memo(move || vld::form::check_field(&name(), &name_schema()));
rsx! {}
}
On the client, recover the field errors from the server-function error:
if let Some(err) = vld::form::VldServerError::from_server_message(&e.to_string()) {
let name_msg = err.field_error("name");
}
Whole-struct validation with a schema! type:
vld::schema! {
struct CreateUserSchema {
name: String => vld::string().min(2).max(50),
email: String => vld::string().email(),
}
}
#[derive(serde::Serialize)]
struct CreateUserArgs { name: String, email: String }
let args = CreateUserArgs { name: "X".into(), email: "bad".into() };
let errors = vld::form::check_all_fields::<CreateUserSchema, _>(&args);
assert_eq!(errors.len(), 2);
assert_eq!(errors[0].field, "name");
The runnable example is crates/vld/examples/form_basic.rs (cargo run -p vld --example form_basic).
API
| Item | Description |
|---|---|
validate_args! { arg => schema, .. } | Validate local values; Result<(), VldServerError> with one FieldError per issue (field = argument name, nested issues as arg.sub / arg[0]) |
validate::<S, T>(&value) / validate_value::<S>(&json) | Validate a serializable value / a JSON value against schema type S |
check_field(&value, &schema) / check_field_all | First error message or None / all messages, for reactive UI |
check_all_fields::<S, T>(&value) | Vec<FieldError> for a whole form |
VldServerError { message, fields } | validation, internal, field_error, field_errors, has_field_error, error_fields, from_json, from_server_message; Display is its JSON |
FieldError { field, message } | One field's error |
Gotchas
- Field paths have no leading dot:
name,user.email,items[0]. ServerFnError'sDisplayadds a prefix before the JSON, sofrom_json(&e.to_string())does not match; usefrom_server_message, which reads from the first{to the last}.- A value that fails to serialize is reported as an error, never as valid.
validate_args!is a module-path macro: writevld::form::validate_args!(it is not at the crate root).