restful 0.2.0

REST API helpers: validated Axum extractors, response envelopes, pagination, ordering, and field selection
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
use axum::extract::rejection::JsonRejection;
use serde_json::json;
use valid8r::{ValidationErrors, ValidationErrorsKind};

/// Extracts field-level details from a JSON deserialization error.
///
/// The failing path becomes the details key. For map types the path contains
/// the client-sent key: that is intentional — the sender needs it to locate
/// the error, and paths never reach the logs (only statuses are logged).
pub fn json_data_error_details(rejection: &JsonRejection) -> Option<serde_json::Value> {
    let JsonRejection::JsonDataError(rejection) = rejection else {
        return None;
    };

    let mut source = std::error::Error::source(rejection);
    while let Some(err) = source {
        if let Some(err) = err.downcast_ref::<serde_path_to_error::Error<serde_json::Error>>() {
            let field = err.path().to_string();
            if field.is_empty() || field == "." {
                return None;
            }

            // serde_json appends " at line L column C" to the message.
            let message = err.inner().to_string();
            let message = message
                .rsplit_once(" at line ")
                .map_or(message.as_str(), |(message, _)| message);
            let message = drop_submitted_value(message);

            let mut details = serde_json::Map::new();
            details.insert(
                field,
                json!([{ "code": "invalid", "message": message, "params": {} }]),
            );
            return Some(serde_json::Value::Object(details));
        }
        source = err.source();
    }

    None
}

/// Limitation: the allowlist below can only vouch for messages serde itself
/// generates. A hand-written Deserialize impl that embeds input in a message
/// mimicking an allowlisted prefix (e.g. starting with "missing field") can
/// still leak; keep submitted values out of custom deserializer messages.
///
/// Sanitized message for a JSON body that failed to deserialize at the root
/// (no field path), e.g. a bare scalar where a struct was expected.
pub fn json_data_error_message(rejection: &JsonRejection) -> Option<String> {
    let JsonRejection::JsonDataError(rejection) = rejection else {
        return None;
    };
    let mut source = std::error::Error::source(rejection);
    while let Some(err) = source {
        if let Some(err) = err.downcast_ref::<serde_path_to_error::Error<serde_json::Error>>() {
            let message = err.inner().to_string();
            let message = message
                .rsplit_once(" at line ")
                .map_or(message.as_str(), |(message, _)| message);
            return Some(drop_submitted_value(message));
        }
        source = err.source();
    }
    None
}

/// Serde messages like `invalid type: integer `123456`, expected a string`
/// echo the submitted value, which may be a credential. Keep only what was
/// expected, and allowlist the shapes we know are value-free: anything else
/// (e.g. a custom Deserialize impl's message) may embed the input.
pub(super) fn drop_submitted_value(message: &str) -> String {
    // `unknown field` / `duplicate field` echo client-sent key names, which
    // are also input; only `missing field` names come from our own schema.
    for prefix in [
        "invalid type",
        "invalid value",
        "invalid length",
        "unknown variant",
        "unknown field",
        "duplicate field",
    ] {
        if message.starts_with(prefix) {
            return match message.rsplit_once(", expected ") {
                Some((_, expected)) => format!("{prefix}, expected {expected}"),
                None => prefix.to_string(),
            };
        }
    }
    if message.starts_with("missing field") {
        return message.to_string();
    }
    "could not deserialize value".to_string()
}

/// Converts nested validation errors into API error details.
///
/// Policy: error keys are camelCased, so payload structs must use
/// `#[serde(rename_all = "camelCase")]` for keys to match the wire names.
pub fn validation_errors_to_json(validation_errors: &ValidationErrors) -> serde_json::Value {
    let mut errors = serde_json::Map::new();

    for (field, field_errors) in validation_errors.errors() {
        // `valid8r` keys errors by the raw Rust field ident; the API field names
        // are camelCase (`#[serde(rename_all = "camelCase")]`), so map the key.
        let key = to_camel_case(field);
        match field_errors {
            ValidationErrorsKind::Field(field_errors) => {
                let details: Vec<serde_json::Value> = field_errors
                    .iter()
                    .map(|err| {
                        json!({
                            "code": err.code.to_string(),
                            "message": err.message.as_ref().map(|msg| msg.to_string()).unwrap_or_default(),
                            "params": err.params
                        })
                    })
                    .collect();

                errors.insert(key, json!(details));
            }
            ValidationErrorsKind::Struct(struct_errors) => {
                errors.insert(key, validation_errors_to_json(struct_errors));
            }
            ValidationErrorsKind::List(list_errors) => {
                // Keyed by the item's index so clients know which entry failed.
                let items: serde_json::Map<String, serde_json::Value> = list_errors
                    .iter()
                    .map(|(index, item_errors)| {
                        (index.to_string(), validation_errors_to_json(item_errors))
                    })
                    .collect();

                errors.insert(key, serde_json::Value::Object(items));
            }
        }
    }

    serde_json::Value::Object(errors)
}

/// Returns field paths and error codes for logging without submitted values.
pub fn rejected_fields(validation_errors: &ValidationErrors) -> Vec<String> {
    let mut out = Vec::new();
    collect_rejected(validation_errors, "", &mut out);
    out.sort();
    out
}

fn collect_rejected(validation_errors: &ValidationErrors, prefix: &str, out: &mut Vec<String>) {
    for (field, field_errors) in validation_errors.errors() {
        let path = format!("{prefix}{field}");
        match field_errors {
            ValidationErrorsKind::Field(field_errors) => {
                out.extend(
                    field_errors
                        .iter()
                        .map(|err| format!("{path}:{}", err.code)),
                );
            }
            ValidationErrorsKind::Struct(struct_errors) => {
                collect_rejected(struct_errors, &format!("{path}."), out);
            }
            ValidationErrorsKind::List(list_errors) => {
                for (index, item_errors) in list_errors {
                    collect_rejected(item_errors, &format!("{path}[{index}]."), out);
                }
            }
        }
    }
}

fn to_camel_case(field: &str) -> String {
    // Schema-level errors use the sentinel `__all__`; leave it intact rather
    // than mangling it to "All".
    if field == "__all__" {
        return field.to_string();
    }
    let mut out = String::with_capacity(field.len());
    let mut capitalize_next = false;
    for (i, ch) in field.chars().enumerate() {
        if ch == '_' {
            capitalize_next = i != 0;
            continue;
        }
        if capitalize_next {
            out.extend(ch.to_uppercase());
            capitalize_next = false;
        } else {
            out.push(ch);
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::Deserialize;
    use serde_json::json;
    use valid8r::Validate;

    #[derive(Debug, Validate, Deserialize)]
    struct TestItem {
        #[validate(length(min = 1, message = "ID cannot be empty"))]
        id: String,

        #[validate(range(min = 0, max = 100, message = "Value out of range"))]
        value: i32,
    }

    #[derive(Debug, Validate, Deserialize)]
    struct TestNestedStruct {
        #[validate(email(message = "Invalid email format"))]
        email: String,
        #[validate(nested)]
        item: TestItem,
    }

    #[derive(Debug, Validate, Deserialize)]
    struct TestListStruct {
        #[validate(length(min = 3, message = "Name too short"))]
        name: String,
        #[validate(nested)]
        items: Vec<TestItem>,
    }

    #[test]
    fn test_validation_errors_to_json_simple_field_errors() {
        let item = TestItem {
            id: "".to_string(), // Fails length validation
            value: 101,         // Fails range validation
        };

        let validation_result = item.validate();

        assert!(validation_result.is_err(), "Expected validation errors");

        let validation_errors = validation_result.unwrap_err();

        let json_errors = validation_errors_to_json(&validation_errors);

        assert_eq!(
            json_errors,
            json!({
                "id": [
                    {
                        "code": "length",
                        "message": "ID cannot be empty",
                        "params": {
                            "min": 1,
                            "value": ""
                        }
                    }
                ],
                "value": [
                    {
                        "code": "range",
                        "message": "Value out of range",
                        "params": {
                            "min": 0,
                            "max": 100,
                            "value": 101
                        }
                    }
                ]
            })
        );
    }

    #[test]
    fn test_validation_errors_to_json_nested_struct_errors() {
        let nested_struct = TestNestedStruct {
            email: "not-an-email".to_string(), // Fails email validation
            item: TestItem {
                id: "".to_string(), // Fails length validation in nested struct
                value: 50,          // Valid
            },
        };

        let validation_result = nested_struct.validate();

        assert!(validation_result.is_err());

        let validation_error = validation_result.unwrap_err();

        let json_errors = validation_errors_to_json(&validation_error);

        assert_eq!(
            json_errors,
            json!({
                "email": [
                    {
                        "message": "Invalid email format",
                        "params": {
                        "value": "not-an-email"
                        },
                        "code": "email"
                    }
                ],
                "item": {
                    "id": [
                        {
                        "message": "ID cannot be empty",
                        "params": {
                            "min": 1,
                            "value": ""
                        },
                        "code": "length"
                        }
                    ]
                }
            })
        );
    }

    #[test]
    fn test_validation_errors_to_json_list_errors() {
        let list_struct = TestListStruct {
            name: "ab".to_string(), // Fails length validation
            items: vec![
                TestItem {
                    id: "valid_id_1".to_string(),
                    value: 10, // Valid
                },
                TestItem {
                    id: "".to_string(), // Fails length validation
                    value: 101,         // Fails range validation
                },
                TestItem {
                    id: "valid_id_2".to_string(),
                    value: 20, // Valid
                },
            ],
        };

        let validation_result = list_struct.validate();

        assert!(validation_result.is_err());

        let validation_error = validation_result.unwrap_err();

        let json_errors = validation_errors_to_json(&validation_error);

        assert_eq!(
            json_errors,
            json!({
                "name": [
                    {
                        "message": "Name too short",
                        "params": {
                            "min": 3,
                            "value": "ab"
                        },
                        "code": "length"
                    }
                ],
                // Keyed by index: the second item (index 1) is the invalid one.
                "items": {
                    "1": {
                        "id": [
                            {
                                "message": "ID cannot be empty",
                                "params": {
                                    "min": 1,
                                    "value": ""
                                },
                                "code": "length"
                            }
                        ],
                        "value": [
                            {
                                "message": "Value out of range",
                                "params": {
                                    "max": 100,
                                    "min": 0,
                                    "value": 101
                                },
                                "code": "range"
                            }
                        ]
                    }
                },
            })
        );
    }

    #[test]
    fn test_validation_errors_to_json_no_errors() {
        let item = TestItem {
            id: "valid_id".to_string(),
            value: 50,
        };

        let validation_result = item.validate();
        assert!(validation_result.is_ok(), "Expected no validation errors");
    }

    #[test]
    fn test_validation_errors_to_json_field_error_no_message() {
        #[derive(Debug, Validate, Deserialize)]
        struct TestNoMessage {
            #[validate(required)]
            // 'required' validator doesn't have a default message in the error itself
            field: Option<String>,
        }

        let item = TestNoMessage { field: None };
        let validation_result = item.validate();
        assert!(validation_result.is_err());

        if let Err(errors) = validation_result {
            let json_errors = validation_errors_to_json(&errors);
            assert_eq!(json_errors["field"][0]["message"], ""); // Expecting empty string as per current implementation
            assert_eq!(json_errors["field"][0]["code"], "required");
        } else {
            panic!("Expected validation errors");
        }
    }

    #[test]
    fn test_validation_errors_to_json_camel_cases_multiword_keys() {
        #[derive(Debug, Validate, Deserialize)]
        struct Inner {
            #[validate(length(min = 1, message = "required"))]
            id: String,
        }
        #[derive(Debug, Validate, Deserialize)]
        struct Outer {
            #[validate(length(min = 2, message = "too short"))]
            first_name: String,
            #[validate(nested)]
            home_address: Inner,
        }

        let errors = Outer {
            first_name: String::new(),
            home_address: Inner { id: String::new() },
        }
        .validate()
        .unwrap_err();

        let json = validation_errors_to_json(&errors);

        assert!(
            json.get("firstName").is_some(),
            "expected camelCase key, got {json}"
        );
        assert!(json.get("first_name").is_none());
        assert!(
            json["homeAddress"].get("id").is_some(),
            "nested struct key should camelCase to homeAddress"
        );
    }

    #[test]
    fn to_camel_case_handles_common_shapes() {
        assert_eq!(to_camel_case("first_name"), "firstName");
        assert_eq!(to_camel_case("max_workload"), "maxWorkload");
        assert_eq!(to_camel_case("name"), "name");
        assert_eq!(to_camel_case("iso2"), "iso2");
        assert_eq!(to_camel_case("assigned_employee_id"), "assignedEmployeeId");
    }
}