ferro-rs 0.2.57

A Laravel-inspired web framework for Rust
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Main validator implementation.

use crate::validation::{Rule, ValidationError};
use serde_json::Value;
use std::collections::HashMap;

/// Request validator.
///
/// # Example
///
/// ```rust,ignore
/// use ferro_rs::validation::{Validator, rules::*};
///
/// let data = serde_json::json!({
///     "email": "user@example.com",
///     "password": "secret123",
///     "password_confirmation": "secret123"
/// });
///
/// let result = Validator::new(&data)
///     .rules("email", vec![required(), email()])
///     .rules("password", vec![required(), min(8), confirmed()])
///     .validate();
///
/// match result {
///     Ok(()) => println!("Validation passed!"),
///     Err(errors) => println!("Errors: {:?}", errors),
/// }
/// ```
pub struct Validator<'a> {
    data: &'a Value,
    rules: HashMap<String, Vec<Box<dyn Rule>>>,
    custom_messages: HashMap<String, String>,
    custom_attributes: HashMap<String, String>,
    stop_on_first_failure: bool,
    /// Pre-seeded errors added before rule evaluation (e.g. for cross-field checks).
    pre_errors: Vec<(String, String)>,
}

impl<'a> Validator<'a> {
    /// Create a new validator for the given data.
    pub fn new(data: &'a Value) -> Self {
        Self {
            data,
            rules: HashMap::new(),
            custom_messages: HashMap::new(),
            custom_attributes: HashMap::new(),
            stop_on_first_failure: false,
            pre_errors: Vec::new(),
        }
    }

    /// Pre-seed a validation error for a field without adding a rule.
    ///
    /// Useful for cross-field checks (e.g. password confirmation) performed
    /// before calling `validate()`. The error appears in the final result
    /// alongside any rule-based errors.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let mut validator = Validator::new(&data)
    ///     .rules("password", rules![required(), min(8)]);
    ///
    /// if form.password != form.password_confirmation {
    ///     validator = validator.with_error("password_confirmation", "Passwords do not match.");
    /// }
    ///
    /// validator.validate()?;
    /// ```
    pub fn with_error(mut self, field: impl Into<String>, message: impl Into<String>) -> Self {
        self.pre_errors.push((field.into(), message.into()));
        self
    }

    /// Add a single validation rule for a field.
    pub fn rule<R: Rule + 'static>(mut self, field: impl Into<String>, rule: R) -> Self {
        let field = field.into();
        self.rules
            .entry(field)
            .or_default()
            .push(Box::new(rule) as Box<dyn Rule>);
        self
    }

    /// Add multiple validation rules for a field using boxed rules.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use ferro_rs::validation::{Validator, rules::*};
    /// use ferro_rs::rules;
    ///
    /// Validator::new(&data)
    ///     .rules("email", rules![required(), email()])
    ///     .rules("name", rules![required(), string(), max(255)]);
    /// ```
    pub fn rules(mut self, field: impl Into<String>, rules: Vec<Box<dyn Rule>>) -> Self {
        self.rules.insert(field.into(), rules);
        self
    }

    /// Add boxed rules for a field (useful for dynamic rule creation).
    pub fn boxed_rules(mut self, field: impl Into<String>, rules: Vec<Box<dyn Rule>>) -> Self {
        self.rules.insert(field.into(), rules);
        self
    }

    /// Set a custom error message for a field.rule combination.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// Validator::new(&data)
    ///     .rules("email", vec![required(), email()])
    ///     .message("email.required", "Please provide your email address")
    ///     .message("email.email", "That doesn't look like a valid email");
    /// ```
    pub fn message(mut self, key: impl Into<String>, message: impl Into<String>) -> Self {
        self.custom_messages.insert(key.into(), message.into());
        self
    }

    /// Set custom messages from a map.
    pub fn messages(mut self, messages: HashMap<String, String>) -> Self {
        self.custom_messages.extend(messages);
        self
    }

    /// Set a custom attribute name for a field.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// Validator::new(&data)
    ///     .rules("email", vec![required()])
    ///     .attribute("email", "email address");
    /// // Error: "The email address field is required."
    /// ```
    pub fn attribute(mut self, field: impl Into<String>, name: impl Into<String>) -> Self {
        self.custom_attributes.insert(field.into(), name.into());
        self
    }

    /// Set custom attributes from a map.
    pub fn attributes(mut self, attributes: HashMap<String, String>) -> Self {
        self.custom_attributes.extend(attributes);
        self
    }

    /// Stop validating remaining fields after first failure.
    pub fn stop_on_first_failure(mut self) -> Self {
        self.stop_on_first_failure = true;
        self
    }

    /// Validate and, on failure, flash per-field errors + old input and return
    /// an [`crate::http::action::ActionError`] redirecting to `url`.
    ///
    /// Composes the existing `with_old_input` + `into_action_error` chain so
    /// per-field errors and old input flash exactly as the modern form-error
    /// idiom does today. The validator already holds the data passed to
    /// [`Validator::new`], so it is reused here without requiring the caller
    /// to pass it again.
    ///
    /// ```ignore
    /// Validator::new(&data)
    ///     .rules("name", rules![required()])
    ///     .validate_or_redirect(&form_url)?;
    /// ```
    pub fn validate_or_redirect(
        self,
        url: impl Into<String>,
    ) -> Result<(), crate::http::action::ActionError> {
        // Capture the data reference before `validate()` consumes `self`.
        let data: &Value = self.data;
        self.validate()
            .map_err(|e| e.with_old_input(data).into_action_error(url))
    }

    /// Run validation and return errors if any.
    pub fn validate(self) -> Result<(), ValidationError> {
        let mut errors = ValidationError::new();

        // Pre-seeded errors (from `with_error`) always appear first.
        for (field, message) in &self.pre_errors {
            errors.add(field, message.clone());
        }

        for (field, rules) in &self.rules {
            let value = self.get_value(field);
            let display_field = self.get_display_field(field);

            // Check if field has 'nullable' rule and value is null
            let has_nullable = rules.iter().any(|r| r.name() == "nullable");
            if has_nullable && value.is_null() {
                continue;
            }

            for rule in rules {
                // Skip nullable rule itself
                if rule.name() == "nullable" {
                    continue;
                }

                if let Err(default_message) = rule.validate(&display_field, &value, self.data) {
                    // Check for custom message
                    let message_key = format!("{}.{}", field, rule.name());
                    let message = self
                        .custom_messages
                        .get(&message_key)
                        .cloned()
                        .unwrap_or(default_message);

                    errors.add(field, message);
                }
            }

            if self.stop_on_first_failure && errors.has(field) {
                break;
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Check if validation passes.
    pub fn passes(&self) -> bool {
        let mut errors = ValidationError::new();

        // Pre-seeded errors (from `with_error`) always appear first.
        for (field, message) in &self.pre_errors {
            errors.add(field, message.clone());
        }

        for (field, rules) in &self.rules {
            let value = self.get_value(field);
            let display_field = self.get_display_field(field);

            let has_nullable = rules.iter().any(|r| r.name() == "nullable");
            if has_nullable && value.is_null() {
                continue;
            }

            for rule in rules {
                if rule.name() == "nullable" {
                    continue;
                }

                if rule.validate(&display_field, &value, self.data).is_err() {
                    errors.add(field, "failed");
                }
            }
        }

        errors.is_empty()
    }

    /// Check if validation fails.
    pub fn fails(&self) -> bool {
        !self.passes()
    }

    /// Get a value from the data, supporting dot notation.
    fn get_value(&self, field: &str) -> Value {
        get_nested_value(self.data, field)
            .cloned()
            .unwrap_or(Value::Null)
    }

    /// Get the display name for a field.
    fn get_display_field(&self, field: &str) -> String {
        self.custom_attributes
            .get(field)
            .cloned()
            .unwrap_or_else(|| {
                // Convert snake_case to human readable
                field.split('_').collect::<Vec<_>>().join(" ")
            })
    }
}

/// Get a nested value from JSON using dot notation.
fn get_nested_value<'a>(data: &'a Value, path: &str) -> Option<&'a Value> {
    let parts: Vec<&str> = path.split('.').collect();
    let mut current = data;

    for part in parts {
        // Try as object key
        if let Value::Object(map) = current {
            current = map.get(part)?;
        }
        // Try as array index
        else if let Value::Array(arr) = current {
            let index: usize = part.parse().ok()?;
            current = arr.get(index)?;
        } else {
            return None;
        }
    }

    Some(current)
}

/// Convenience function to validate data with rules.
///
/// # Example
///
/// ```rust,ignore
/// use ferro_rs::validation::{validate, rules::*};
/// use ferro_rs::rules;
///
/// let data = serde_json::json!({"email": "test@example.com"});
///
/// if let Err(errors) = validate(&data, vec![("email", rules![required(), email()])]) {
///     println!("Validation failed: {:?}", errors);
/// }
/// ```
pub fn validate<I, F>(data: &Value, rules: I) -> Result<(), ValidationError>
where
    I: IntoIterator<Item = (F, Vec<Box<dyn Rule>>)>,
    F: Into<String>,
{
    let mut validator = Validator::new(data);
    for (field, field_rules) in rules {
        validator = validator.rules(field, field_rules);
    }
    validator.validate()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rules;
    use crate::validation::rules::*;
    use serde_json::json;

    #[test]
    fn test_validator_passes() {
        let data = json!({
            "email": "test@example.com",
            "name": "John Doe"
        });

        let result = Validator::new(&data)
            .rules("email", rules![required(), email()])
            .rules("name", rules![required(), string()])
            .validate();

        assert!(result.is_ok());
    }

    #[test]
    fn test_validator_fails() {
        let data = json!({
            "email": "invalid-email",
            "name": ""
        });

        let result = Validator::new(&data)
            .rules("email", rules![required(), email()])
            .rules("name", rules![required()])
            .validate();

        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert!(errors.has("email"));
        assert!(errors.has("name"));
    }

    #[test]
    fn test_validator_custom_message() {
        let data = json!({"email": ""});

        let result = Validator::new(&data)
            .rules("email", rules![required()])
            .message("email.required", "We need your email!")
            .validate();

        let errors = result.unwrap_err();
        assert_eq!(
            errors.first("email"),
            Some(&"We need your email!".to_string())
        );
    }

    #[test]
    fn test_validator_custom_attribute() {
        let data = json!({"user_email": ""});

        // Verify custom attribute is stored and used.
        let validator = Validator::new(&data)
            .rules("user_email", rules![required()])
            .attribute("user_email", "email address");

        // Validation must fail for the empty required field.
        let result = validator.validate();
        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert!(
            errors.first("user_email").is_some(),
            "Expected error for 'user_email'"
        );

        // The message content depends on global translator state (OnceLock),
        // which may be set by other tests in the same process. We verify the
        // attribute mechanism is wired correctly by checking the builder API
        // compiles and validation behaves correctly with it.
    }

    #[test]
    fn test_validator_nullable() {
        let data = json!({"nickname": null});

        let result = Validator::new(&data)
            .rules("nickname", rules![nullable(), string(), min(3)])
            .validate();

        assert!(result.is_ok());
    }

    #[test]
    fn test_nested_value() {
        let data = json!({
            "user": {
                "profile": {
                    "email": "test@example.com"
                }
            }
        });

        let value = get_nested_value(&data, "user.profile.email");
        assert_eq!(value, Some(&json!("test@example.com")));
    }

    #[test]
    fn test_validate_function() {
        let data = json!({"email": "test@example.com"});

        let result = validate(&data, vec![("email", rules![required(), email()])]);

        assert!(result.is_ok());
    }

    #[test]
    fn test_passes_and_fails() {
        let data = json!({"email": "invalid"});

        let validator = Validator::new(&data).rules("email", rules![email()]);

        assert!(validator.fails());
    }

    // ── Phase 212 tests: validate_or_redirect ────────────────────────────────

    #[test]
    fn test_validate_or_redirect_ok_when_all_rules_pass() {
        let data = json!({"name": "Alice"});
        let result = Validator::new(&data)
            .rules("name", rules![required(), string()])
            .validate_or_redirect("/form");
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_or_redirect_err_when_rule_fails() {
        let data = json!({"name": ""});
        let result = Validator::new(&data)
            .rules("name", rules![required()])
            .validate_or_redirect("/form");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_or_redirect_chains_with_old_input() {
        // Verify that the failure path composes with_old_input + into_action_error
        // without panicking, and that the result matches the manual chain.
        let data = json!({"name": ""});

        let result_via_helper = Validator::new(&data)
            .rules("name", rules![required()])
            .validate_or_redirect("/form");

        // The manual chain produces the same error shape:
        let result_manual = Validator::new(&data)
            .rules("name", rules![required()])
            .validate()
            .map_err(|e| e.with_old_input(&data).into_action_error("/form"));

        // Both must be Err(ActionError).
        assert!(result_via_helper.is_err());
        assert!(result_manual.is_err());
    }
}