UI frameworks
Validate Tauri command arguments, events, state and channel messages, without a tauri dependency.

vld_app::tauri validates JSON crossing the Tauri IPC boundary and returns a serializable error the frontend can read. It has no tauri dependency: add tauri to your app separately.

Install

Cargo.toml
[dependencies]
vld = "0.8"
vld-app = "0.8"

Module vld_app::tauri, always compiled (no feature). It has no tauri dependency: add tauri to your app separately.

Example

use vld_app::tauri::prelude::*;

vld::schema! {
    #[derive(Debug, Clone)]
    pub struct CreateUser {
        pub name: String  => vld::string().min(2).max(50),
        pub email: String => vld::string().email(),
    }
}

// Explicit validation: the frontend receives `VldTauriError` as JSON.
#[tauri::command]
fn create_user(payload: serde_json::Value) -> Result<String, VldTauriError> {
    let user = validate::<CreateUser>(payload)?;
    Ok(format!("Created {}", user.name))
}

// Validation during deserialization of the argument.
#[tauri::command]
fn create_user_auto(payload: VldPayload<CreateUser>) -> Result<String, VldTauriError> {
    Ok(format!("Created {}", payload.name))
}

The error serializes as:

{
  "error": "Validation failed",
  "issues": [{
    "path": "name",
    "message": "String must be at least 2 characters",
    "code": "too_small",
    "params": { "inclusive": "true", "minimum": "2", "origin": "string" }
  }]
}
import { invoke } from '@tauri-apps/api/core'

try {
  await invoke('create_user', { payload: { name: 'A', email: 'x' } })
} catch (err) {
  for (const issue of (err as { issues: { path: string; message: string }[] }).issues) {
    console.error(`${issue.path}: ${issue.message}`)
  }
}

code and params let the frontend translate on its own, with the same catalogs vld translates with (feature i18n, 29 languages, keys vld.<code>.<sub-kind> / vld.<code>). With tauri-plugin-i18n-next, build the Rust catalog with .extend(vld::i18n::files()): the WebView gets the vld.* keys with the app's own, and the app's locales/{locale}.json can override any of them or add a language.

// messages: vld's catalogs (vld::i18n::files()) under the app's own
const text = (i: { code: string; params: Record<string, string>; message: string }) => {
  const sub = i.params.origin ?? i.params.validation ?? i.params.format ?? i.params.kind
  const key = [sub && `vld.${i.code}.${sub}`, `vld.${i.code}`].find((k) => k && te(k))
  return key ? t(key, i.params) : i.message
}

Size messages are plurals (tc(key, Number(i.params.minimum), i.params)), type names translate through vld.type.<name>, exclusive bounds use <code>_exclusive: see Translations → Frontend. On the Rust side, vld::i18n::Translator::new(i18n.page("index")) translates a VldError before it becomes TauriIssues.

code is the issue code (a rule key for custom rules); invalid_json / custom for json_parse_error / custom. params are the template parameters (minimum, maximum, origin, expected, …), all strings.

API

  • validate::<T>(value) — validate a serde_json::Value; use it for commands, events, state, plugin config and channel messages.
  • validate_args::<T>(json_str) — parse a JSON string, then validate ("Invalid JSON" error on a parse failure).
  • VldPayload<T> — Deserialize wrapper that validates; Deref to T. VldEvent<T> is a type alias of it.
  • TauriIssue { path, message, code, params }.
  • VldTauriError { error, issues: Vec<TauriIssue> } — from_vld, json_parse_error, custom, has_issues, issue_count.

Gotchas

  • Issue paths have no leading dot: name, user.email, items[0].
  • With VldPayload<T> a validation failure surfaces as Tauri's argument deserialization error (a string "Validation failed: name: ...; email: ..."), not as VldTauriError. Take serde_json::Value and call validate when the frontend needs structured issues.
  • VldEvent<T> is VldPayload<T>: construct and match it as VldPayload(x).