vld-dioxus 0.4.0

Dioxus integration for the vld validation library — shared validation for server functions and WASM clients
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! # vld-dioxus — Dioxus integration for the `vld` validation library
//!
//! Shared validation for Dioxus server functions and WASM clients.
//! Define validation rules once, use them on both the server and the browser.
//!
//! **Zero dependency on `dioxus`** — works with any Dioxus version (0.5, 0.6, 0.7+).
//! Compatible with WASM and native targets.
//!
//! ## Key Features
//!
//! | Feature | Description |
//! |---|---|
//! | [`validate_args!`] | Inline validation of server function arguments |
//! | [`validate`] / [`validate_value`] | Validate a serializable value against a schema |
//! | [`check_field`] | Single-field validation for reactive UI |
//! | [`check_all_fields`] | Multi-field validation returning per-field errors |
//! | [`VldServerError`] | Serializable error type for server→client error transport |
//!
//! ## Quick Example
//!
//! ```ignore
//! // Shared validation rules (used on server AND client)
//! fn name_schema() -> vld::primitives::ZString { vld::string().min(2).max(50) }
//! fn email_schema() -> vld::primitives::ZString { vld::string().email() }
//!
//! // Server function
//! #[server]
//! async fn create_user(name: String, email: String) -> Result<(), ServerFnError> {
//!     vld_dioxus::validate_args! {
//!         name  => name_schema(),
//!         email => email_schema(),
//!     }.map_err(|e| ServerFnError::new(e.to_string()))?;
//!     Ok(())
//! }
//!
//! // Client component (reactive validation)
//! #[component]
//! fn CreateUserForm() -> Element {
//!     let mut name = use_signal(String::new);
//!     let name_err = use_memo(move || {
//!         vld_dioxus::check_field(&name(), &name_schema())
//!     });
//!     // ... render form with error display
//! }
//! ```

use serde::{Deserialize, Serialize};
use std::fmt;

// ========================= Error types =======================================

/// A single field validation error.
///
/// Designed to be serialized across the server→client boundary
/// and displayed in form UIs.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FieldError {
    /// Field name (matches the server function argument name or struct field).
    pub field: String,
    /// Human-readable error message.
    pub message: String,
}

/// Structured validation error for Dioxus server functions.
///
/// Serializable and deserializable — can be transmitted from server to client
/// as part of a `ServerFnError` message or custom error type.
///
/// # With `ServerFnError`
///
/// ```ignore
/// #[server]
/// async fn my_fn(name: String) -> Result<(), ServerFnError> {
///     vld_dioxus::validate_args! {
///         name => vld::string().min(2),
///     }.map_err(|e| ServerFnError::new(e.to_string()))?;
///     Ok(())
/// }
/// ```
///
/// # With custom error type
///
/// ```ignore
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// enum AppError {
///     Validation(VldServerError),
///     Server(String),
/// }
///
/// impl FromServerFnError for AppError { /* ... */ }
///
/// #[server]
/// async fn my_fn(name: String) -> Result<(), AppError> {
///     vld_dioxus::validate_args! {
///         name => vld::string().min(2),
///     }.map_err(AppError::Validation)?;
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VldServerError {
    /// Summary message.
    pub message: String,
    /// Per-field validation errors.
    pub fields: Vec<FieldError>,
}

impl VldServerError {
    /// Create a validation error from a list of field errors.
    pub fn validation(fields: Vec<FieldError>) -> Self {
        let count = fields.len();
        Self {
            message: format!(
                "Validation failed: {} field{} invalid",
                count,
                if count == 1 { "" } else { "s" }
            ),
            fields,
        }
    }

    /// Create an internal/serialization error.
    pub fn internal(msg: impl Into<String>) -> Self {
        Self {
            message: msg.into(),
            fields: Vec::new(),
        }
    }

    /// Get the error message for a specific field.
    pub fn field_error(&self, field: &str) -> Option<&str> {
        self.fields
            .iter()
            .find(|f| f.field == field)
            .map(|f| f.message.as_str())
    }

    /// Get all error messages for a specific field.
    pub fn field_errors(&self, field: &str) -> Vec<&str> {
        self.fields
            .iter()
            .filter(|f| f.field == field)
            .map(|f| f.message.as_str())
            .collect()
    }

    /// Check if a specific field has errors.
    pub fn has_field_error(&self, field: &str) -> bool {
        self.fields.iter().any(|f| f.field == field)
    }

    /// List all field names that have errors.
    pub fn error_fields(&self) -> Vec<&str> {
        let mut names: Vec<&str> = self.fields.iter().map(|f| f.field.as_str()).collect();
        names.dedup();
        names
    }

    /// Parse a `VldServerError` from a JSON string (e.g. from `ServerFnError` message).
    ///
    /// Returns `None` if the string is not a valid `VldServerError` JSON.
    pub fn from_json(s: &str) -> Option<Self> {
        serde_json::from_str(s).ok()
    }
}

impl fmt::Display for VldServerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match serde_json::to_string(self) {
            Ok(json) => write!(f, "{}", json),
            Err(_) => write!(f, "{}", self.message),
        }
    }
}

impl std::error::Error for VldServerError {}

impl From<vld::error::VldError> for VldServerError {
    fn from(error: vld::error::VldError) -> Self {
        let fields: Vec<FieldError> = error
            .issues
            .iter()
            .map(|issue| {
                let field = issue
                    .path
                    .iter()
                    .map(|p| p.to_string())
                    .collect::<Vec<_>>()
                    .join(".");
                FieldError {
                    field,
                    message: issue.message.clone(),
                }
            })
            .collect();
        Self::validation(fields)
    }
}

// ========================= Schema-based validation ===========================

/// Validate a serializable value against a vld schema type.
///
/// Serializes `data` to JSON, then validates using `S::vld_parse_value()`.
///
/// # Example
///
/// ```ignore
/// vld::schema! {
///     struct UserInput {
///         name: String => vld::string().min(2),
///         email: String => vld::string().email(),
///     }
/// }
///
/// #[derive(Serialize)]
/// struct Args { name: String, email: String }
///
/// vld_dioxus::validate::<UserInput, _>(&Args { name, email })?;
/// ```
pub fn validate<S, T>(data: &T) -> Result<(), VldServerError>
where
    S: vld::schema::VldParse,
    T: serde::Serialize,
{
    let json = serde_json::to_value(data)
        .map_err(|e| VldServerError::internal(format!("Serialization error: {}", e)))?;
    S::vld_parse_value(&json).map_err(VldServerError::from)?;
    Ok(())
}

/// Validate a `serde_json::Value` against a vld schema type.
pub fn validate_value<S>(json: &serde_json::Value) -> Result<(), VldServerError>
where
    S: vld::schema::VldParse,
{
    S::vld_parse_value(json).map_err(VldServerError::from)?;
    Ok(())
}

// ========================= Field-level validation ============================

/// Check a single value against a schema, returning an error message if invalid.
///
/// Designed for reactive client-side validation in Dioxus components.
/// Returns `None` if valid, `Some(message)` if invalid.
///
/// # Example
///
/// ```
/// let error = vld_dioxus::check_field(&"A".to_string(), &vld::string().min(2));
/// assert!(error.is_some());
///
/// let error = vld_dioxus::check_field(&"Alice".to_string(), &vld::string().min(2));
/// assert!(error.is_none());
/// ```
pub fn check_field<V, S>(value: &V, schema: &S) -> Option<String>
where
    V: serde::Serialize,
    S: vld::schema::VldSchema,
{
    let json = serde_json::to_value(value).ok()?;
    match schema.parse_value(&json) {
        Ok(_) => None,
        Err(e) => e.issues.first().map(|i| i.message.clone()),
    }
}

/// Check a single value, returning all error messages (not just the first).
///
/// # Example
///
/// ```
/// let errors = vld_dioxus::check_field_all(&"".to_string(), &vld::string().min(2).email());
/// assert!(errors.len() >= 1);
/// ```
pub fn check_field_all<V, S>(value: &V, schema: &S) -> Vec<String>
where
    V: serde::Serialize,
    S: vld::schema::VldSchema,
{
    let json = match serde_json::to_value(value) {
        Ok(j) => j,
        Err(_) => return vec![],
    };
    match schema.parse_value(&json) {
        Ok(_) => vec![],
        Err(e) => e.issues.iter().map(|i| i.message.clone()).collect(),
    }
}

/// Validate all fields of a serializable struct against a vld schema type.
///
/// Returns a list of [`FieldError`]s (empty if all fields are valid).
/// Designed for validating entire form state at once.
///
/// # Example
///
/// ```
/// use vld_dioxus::FieldError;
///
/// vld::schema! {
///     struct UserSchema {
///         name: String => vld::string().min(2),
///         email: String => vld::string().email(),
///     }
/// }
///
/// #[derive(serde::Serialize)]
/// struct FormData { name: String, email: String }
///
/// let data = FormData { name: "A".into(), email: "bad".into() };
/// let errors = vld_dioxus::check_all_fields::<UserSchema, _>(&data);
/// assert!(!errors.is_empty());
/// assert!(errors.iter().any(|e| e.field == ".name"));
/// ```
pub fn check_all_fields<S, T>(data: &T) -> Vec<FieldError>
where
    S: vld::schema::VldParse,
    T: serde::Serialize,
{
    let json = match serde_json::to_value(data) {
        Ok(j) => j,
        Err(_) => return vec![],
    };
    match S::vld_parse_value(&json) {
        Ok(_) => vec![],
        Err(e) => e
            .issues
            .iter()
            .map(|i| FieldError {
                field: i
                    .path
                    .iter()
                    .map(|p| p.to_string())
                    .collect::<Vec<_>>()
                    .join("."),
                message: i.message.clone(),
            })
            .collect(),
    }
}

// ========================= Macro =============================================

/// Validate server function arguments inline.
///
/// Each argument is validated against its schema. All errors are accumulated
/// and returned as a [`VldServerError`].
///
/// # Example
///
/// ```ignore
/// #[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:
///
/// ```ignore
/// // 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()));
/// ```
#[macro_export]
macro_rules! validate_args {
    ($($field:ident => $schema:expr),* $(,)?) => {{
        use ::vld::schema::VldSchema as _;
        let mut __vld_field_errors: ::std::vec::Vec<$crate::FieldError> = ::std::vec::Vec::new();

        $(
            {
                let __vld_json = ::vld::serde_json::to_value(&$field)
                    .unwrap_or(::vld::serde_json::Value::Null);
                if let ::std::result::Result::Err(e) = ($schema).parse_value(&__vld_json) {
                    for issue in &e.issues {
                        __vld_field_errors.push($crate::FieldError {
                            field: ::std::string::String::from(stringify!($field)),
                            message: issue.message.clone(),
                        });
                    }
                }
            }
        )*

        if __vld_field_errors.is_empty() {
            ::std::result::Result::Ok::<(), $crate::VldServerError>(())
        } else {
            ::std::result::Result::Err($crate::VldServerError::validation(__vld_field_errors))
        }
    }};
}

/// Prelude — import everything you need.
pub mod prelude {
    pub use crate::{
        check_all_fields, check_field, check_field_all, validate, validate_args, validate_value,
        FieldError, VldServerError,
    };
    pub use vld::prelude::*;
}