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
//! Deserialize-and-validate helpers.
//!
//! These functions deserialize JSON into a struct using `serde_path_to_error`,
//! then run validation. Deserialization errors are converted to field-level
//! [`ValidationErrors`] — so a `uuid::Uuid` field
//! that receives `"not-a-uuid"` produces `{"id": ["expected UUID"]}` instead of
//! a generic parse error.
//!
//! # Usage
//!
//! ```rust,ignore
//! use scrutiny::deserialize::from_json;
//!
//! let input = br#"{"name": "Jo", "id": "not-a-uuid"}"#;
//! match from_json::<CreateUser>(input) {
//! Ok(user) => { /* deserialized AND validated */ }
//! Err(errors) => {
//! // errors.messages() → {"id": ["invalid type: ..."]}
//! }
//! }
//! ```
//!
//! ## For axum users
//!
//! You don't need these functions directly — the `scrutiny-axum` extractors
//! (`Valid<T>`, `ValidForm<T>`, etc.) call them automatically.
//!
//! Requires the `serde_json` and `serde_path_to_error` features (both default).
use crate;
use crateValidate;
/// Convert a `serde_path_to_error` path to a dot-notation field name.
/// Deserialize JSON bytes into `T`, then validate.
///
/// Deserialization errors are returned as field-level `ValidationErrors` with
/// the field path from `serde_path_to_error`. If deserialization succeeds,
/// `T::validate()` is called and its errors (if any) are returned.
///
/// # Example
///
/// ```rust
/// use scrutiny::Validate;
/// use scrutiny::traits::Validate as _;
/// use serde::Deserialize;
///
/// #[derive(Validate, Deserialize, Debug)]
/// struct Input {
/// #[validate(required, min = 2)]
/// name: Option<String>,
/// count: u32,
/// }
///
/// // Deserialization error on `count` → field-level error
/// let result = scrutiny::deserialize::from_json::<Input>(br#"{"name": "Jo", "count": "abc"}"#);
/// let err = result.unwrap_err();
/// assert!(err.messages().contains_key("count"));
///
/// // Validation error on `name` (too short is fine, but missing is caught)
/// let result = scrutiny::deserialize::from_json::<Input>(br#"{"count": 5}"#);
/// let err = result.unwrap_err();
/// assert!(err.messages().contains_key("name"));
///
/// // Both valid
/// let result = scrutiny::deserialize::from_json::<Input>(br#"{"name": "John", "count": 5}"#);
/// assert!(result.is_ok());
/// ```
/// Deserialize JSON bytes with field-level error tracking (without validation).
///
/// Use this if you only want deserialization errors as `ValidationErrors`
/// without running the validation rules.