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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! # Validify support
//!
//! ## Feature
//!
//! Enable the `validify` feature to use `Validated<E>`, `Modified<E>`, `Validified<E>` and `ValidifiedByRef<E>`.
//!

#[cfg(test)]
pub mod test;

use crate::{HasValidate, ValidationRejection};
use axum::async_trait;
use axum::extract::{FromRequest, FromRequestParts, Request};
use axum::http::request::Parts;
use axum::response::{IntoResponse, Response};
use std::fmt::{Display, Formatter};
use std::ops::{Deref, DerefMut};
use validify::{Modify, Validate, ValidationErrors, ValidifyPayload};

/// # `Validated` data extractor
///
/// `Validated` provides simple data validation based on `validify`.
///
/// It only does validation, usage is similar to `Valid`.
///
#[derive(Debug, Clone, Copy, Default)]
pub struct Validated<E>(pub E);

impl<E> Deref for Validated<E> {
    type Target = E;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<E> DerefMut for Validated<E> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Display> Display for Validated<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl<E> Validated<E> {
    /// Consumes the `Validated` and returns the validated data within.
    ///
    /// This returns the `E` type which represents the data that has been
    /// successfully validated.
    pub fn into_inner(self) -> E {
        self.0
    }
}

#[cfg(feature = "aide")]
impl<T> aide::OperationInput for Validated<T>
where
    T: aide::OperationInput,
{
    fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) {
        T::operation_input(ctx, operation);
    }
}

/// # `Modified` data extractor / response
///
/// ## Extractor
///
/// `Modified` uses `validify`'s modification capabilities to alter data, without validation.
///
/// Operations like trimming and case modification can be done based on `modify` attributes.
///
/// ## Response
///
/// `Modified` also implements the `IntoResponse` trait. When its inner `IntoResponse` type also implements the `HasModify` trait:
///
/// `Modified` will call `validify`'s modify method to alter the inner data.
/// Then call the inner type's own `into_response` method to convert it into a HTTP response.
///
/// This allows applying modifications during response conversion by leveraging validify.
#[derive(Debug, Clone, Copy, Default)]
pub struct Modified<E>(pub E);

impl<E> Deref for Modified<E> {
    type Target = E;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<E> DerefMut for Modified<E> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Display> Display for Modified<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl<E> Modified<E> {
    /// Consumes the `Modified` and returns the modified data within.
    ///
    /// This returns the `E` type which represents the data that has been
    /// modified.
    pub fn into_inner(self) -> E {
        self.0
    }
}

impl<E: IntoResponse + HasModify> IntoResponse for Modified<E> {
    fn into_response(mut self) -> Response {
        self.get_modify().modify();
        self.0.into_response()
    }
}

#[cfg(feature = "aide")]
impl<T> aide::OperationInput for Modified<T>
where
    T: aide::OperationInput,
{
    fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) {
        T::operation_input(ctx, operation);
    }
}

#[cfg(feature = "aide")]
impl<T> aide::OperationOutput for Modified<T>
where
    T: aide::OperationOutput,
{
    type Inner = T::Inner;

    fn operation_response(
        ctx: &mut aide::gen::GenContext,
        operation: &mut aide::openapi::Operation,
    ) -> Option<aide::openapi::Response> {
        T::operation_response(ctx, operation)
    }

    fn inferred_responses(
        ctx: &mut aide::gen::GenContext,
        operation: &mut aide::openapi::Operation,
    ) -> Vec<(Option<u16>, aide::openapi::Response)> {
        T::inferred_responses(ctx, operation)
    }
}

/// # `Validified` data extractor
///
/// `Validified` provides construction, modification and validation abilities based on `validify`.
///
/// It requires a serde-based inner extractor.
///
/// And can treat missing fields as validation errors.
///
#[derive(Debug, Clone, Copy, Default)]
pub struct Validified<E>(pub E);

impl<E> Deref for Validified<E> {
    type Target = E;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<E> DerefMut for Validified<E> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Display> Display for Validified<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl<E> Validified<E> {
    /// Consumes the `Validified` and returns the modified and validated data within.
    ///
    /// This returns the `E` type which represents the data that has been
    /// successfully validated.
    pub fn into_inner(self) -> E {
        self.0
    }
}

#[cfg(feature = "aide")]
impl<T> aide::OperationInput for Validified<T>
where
    T: aide::OperationInput,
{
    fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) {
        T::operation_input(ctx, operation);
    }
}

/// # `ValidifiedByRef` data extractor
///
/// `ValidifiedByRef` is similar to `Validified`, but operates via reference.
///
/// Suitable for inner extractors not based on `serde`.
///
#[derive(Debug, Clone, Copy, Default)]
pub struct ValidifiedByRef<E>(pub E);

impl<E> Deref for ValidifiedByRef<E> {
    type Target = E;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<E> DerefMut for ValidifiedByRef<E> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Display> Display for ValidifiedByRef<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl<E> ValidifiedByRef<E> {
    /// Consumes the `ValidifiedByRef` and returns the modified and validated data within.
    ///
    /// This returns the `E` type which represents the data that has been
    /// successfully validated.
    pub fn into_inner(self) -> E {
        self.0
    }
}

#[cfg(feature = "aide")]
impl<T> aide::OperationInput for ValidifiedByRef<T>
where
    T: aide::OperationInput,
{
    fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) {
        T::operation_input(ctx, operation);
    }
}

/// `ValidifyRejection` is returned when the `Validated` / `Modified` / `Validified` / `ValidifiedByRef` extractor fails.
///
pub type ValidifyRejection<E> = ValidationRejection<ValidationErrors, E>;

impl<E> From<ValidationErrors> for ValidifyRejection<E> {
    fn from(value: ValidationErrors) -> Self {
        Self::Valid(value)
    }
}

/// Trait for types that can supply a reference that can be modified.
///
/// Extractor types `T` that implement this trait can be used with `Modified`.
///
pub trait HasModify {
    /// Inner type that can be modified
    type Modify: Modify;
    /// Get the inner value
    fn get_modify(&mut self) -> &mut Self::Modify;
}

/// Extractor to extract payload for constructing data
pub trait PayloadExtractor {
    /// Type of payload for constructing data
    type Payload;
    /// Get payload from the extractor
    fn get_payload(self) -> Self::Payload;
}

/// Trait for extractors whose inner data type that can be constructed using some payload,  
/// then modified and validated using `validify`.
///
/// Extractor types `T` that implement this trait can be used with `Validified`.
///
pub trait HasValidify: Sized {
    /// Inner type that can be modified and validated using `validify`.
    type Validify: ValidifyPayload;

    /// Extracts payload from the request,
    /// which will be used to construct the `Self::Validify` type  
    /// and perform modification and validation on it.
    type PayloadExtractor: PayloadExtractor<Payload = <Self::Validify as ValidifyPayload>::Payload>;

    /// Re-packages the validified data back into the inner Extractor type.  
    fn from_validify(v: Self::Validify) -> Self;
}

#[async_trait]
impl<State, Extractor> FromRequest<State> for Validated<Extractor>
where
    State: Send + Sync,
    Extractor: HasValidate + FromRequest<State>,
    Extractor::Validate: validify::Validate,
{
    type Rejection = ValidifyRejection<<Extractor as FromRequest<State>>::Rejection>;

    async fn from_request(req: Request, state: &State) -> Result<Self, Self::Rejection> {
        let inner = Extractor::from_request(req, state)
            .await
            .map_err(ValidifyRejection::Inner)?;
        inner.get_validate().validate()?;
        Ok(Validated(inner))
    }
}

#[async_trait]
impl<State, Extractor> FromRequestParts<State> for Validated<Extractor>
where
    State: Send + Sync,
    Extractor: HasValidate + FromRequestParts<State>,
    Extractor::Validate: Validate,
{
    type Rejection = ValidifyRejection<<Extractor as FromRequestParts<State>>::Rejection>;

    async fn from_request_parts(parts: &mut Parts, state: &State) -> Result<Self, Self::Rejection> {
        let inner = Extractor::from_request_parts(parts, state)
            .await
            .map_err(ValidifyRejection::Inner)?;
        inner.get_validate().validate()?;
        Ok(Validated(inner))
    }
}

#[async_trait]
impl<State, Extractor> FromRequest<State> for Modified<Extractor>
where
    State: Send + Sync,
    Extractor: HasModify + FromRequest<State>,
{
    type Rejection = <Extractor as FromRequest<State>>::Rejection;

    async fn from_request(req: Request, state: &State) -> Result<Self, Self::Rejection> {
        let mut inner = Extractor::from_request(req, state).await?;
        inner.get_modify().modify();
        Ok(Modified(inner))
    }
}

#[async_trait]
impl<State, Extractor> FromRequestParts<State> for Modified<Extractor>
where
    State: Send + Sync,
    Extractor: HasModify + FromRequestParts<State>,
{
    type Rejection = <Extractor as FromRequestParts<State>>::Rejection;

    async fn from_request_parts(parts: &mut Parts, state: &State) -> Result<Self, Self::Rejection> {
        let mut inner = Extractor::from_request_parts(parts, state).await?;
        inner.get_modify().modify();
        Ok(Modified(inner))
    }
}

#[async_trait]
impl<State, Extractor> FromRequest<State> for Validified<Extractor>
where
    State: Send + Sync,
    Extractor: HasValidify,
    Extractor::PayloadExtractor: FromRequest<State>,
{
    type Rejection =
        ValidifyRejection<<Extractor::PayloadExtractor as FromRequest<State>>::Rejection>;

    async fn from_request(req: Request, state: &State) -> Result<Self, Self::Rejection> {
        let payload = Extractor::PayloadExtractor::from_request(req, state)
            .await
            .map_err(ValidifyRejection::Inner)?
            .get_payload();
        let validify = Extractor::Validify::validify_from(payload)?;
        Ok(Validified(Extractor::from_validify(validify)))
    }
}

#[async_trait]
impl<State, Extractor> FromRequestParts<State> for Validified<Extractor>
where
    State: Send + Sync,
    Extractor: HasValidify,
    Extractor::PayloadExtractor: FromRequestParts<State>,
{
    type Rejection =
        ValidifyRejection<<Extractor::PayloadExtractor as FromRequestParts<State>>::Rejection>;

    async fn from_request_parts(parts: &mut Parts, state: &State) -> Result<Self, Self::Rejection> {
        let payload = Extractor::PayloadExtractor::from_request_parts(parts, state)
            .await
            .map_err(ValidifyRejection::Inner)?
            .get_payload();
        let validify = Extractor::Validify::validify_from(payload)?;
        Ok(Validified(Extractor::from_validify(validify)))
    }
}

#[async_trait]
impl<State, Extractor> FromRequest<State> for ValidifiedByRef<Extractor>
where
    State: Send + Sync,
    Extractor: HasValidate + HasModify + FromRequest<State>,
    Extractor::Validate: Validate,
{
    type Rejection = ValidifyRejection<<Extractor as FromRequest<State>>::Rejection>;

    async fn from_request(req: Request, state: &State) -> Result<Self, Self::Rejection> {
        let mut inner = Extractor::from_request(req, state)
            .await
            .map_err(ValidifyRejection::Inner)?;
        inner.get_modify().modify();
        inner.get_validate().validate()?;
        Ok(ValidifiedByRef(inner))
    }
}

#[async_trait]
impl<State, Extractor> FromRequestParts<State> for ValidifiedByRef<Extractor>
where
    State: Send + Sync,
    Extractor: HasValidate + HasModify + FromRequestParts<State>,
    Extractor::Validate: Validate,
{
    type Rejection = ValidifyRejection<<Extractor as FromRequestParts<State>>::Rejection>;

    async fn from_request_parts(parts: &mut Parts, state: &State) -> Result<Self, Self::Rejection> {
        let mut inner = Extractor::from_request_parts(parts, state)
            .await
            .map_err(ValidifyRejection::Inner)?;
        inner.get_modify().modify();
        inner.get_validate().validate()?;
        Ok(ValidifiedByRef(inner))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::Json;
    use serde::Serialize;
    use std::error::Error;
    use std::io;

    const VALIDIFY: &str = "validify";

    #[test]
    fn validify_deref_deref_mut_into_inner() {
        let mut inner = String::from(VALIDIFY);
        let mut v = Validated(inner.clone());
        assert_eq!(&inner, v.deref());
        inner.push_str(VALIDIFY);
        v.deref_mut().push_str(VALIDIFY);
        assert_eq!(&inner, v.deref());
        println!("{}", v);
        assert_eq!(inner, v.into_inner());

        let mut inner = String::from(VALIDIFY);
        let mut v = Modified(inner.clone());
        assert_eq!(&inner, v.deref());
        inner.push_str(VALIDIFY);
        v.deref_mut().push_str(VALIDIFY);
        assert_eq!(&inner, v.deref());
        println!("{}", v);
        assert_eq!(inner, v.into_inner());

        let mut inner = String::from(VALIDIFY);
        let mut v = Validified(inner.clone());
        assert_eq!(&inner, v.deref());
        inner.push_str(VALIDIFY);
        v.deref_mut().push_str(VALIDIFY);
        assert_eq!(&inner, v.deref());
        println!("{}", v);
        assert_eq!(inner, v.into_inner());

        let mut inner = String::from(VALIDIFY);
        let mut v = ValidifiedByRef(inner.clone());
        assert_eq!(&inner, v.deref());
        inner.push_str(VALIDIFY);
        v.deref_mut().push_str(VALIDIFY);
        assert_eq!(&inner, v.deref());
        println!("{}", v);
        assert_eq!(inner, v.into_inner());
    }

    #[test]
    fn display_error() {
        // ValidifyRejection::Valid Display
        let mut report = ValidationErrors::new();
        report.add(validify::ValidationError::new_schema(VALIDIFY));
        let s = report.to_string();
        let vr = ValidifyRejection::<String>::Valid(report);
        assert_eq!(vr.to_string(), s);

        // ValidifyRejection::Inner Display
        let inner = String::from(VALIDIFY);
        let vr = ValidifyRejection::<String>::Inner(inner.clone());
        assert_eq!(inner.to_string(), vr.to_string());

        // ValidifyRejection::Valid Error
        let mut report = ValidationErrors::new();
        report.add(validify::ValidationError::new_schema(VALIDIFY));
        let vr = ValidifyRejection::<io::Error>::Valid(report);
        assert!(
            matches!(vr.source(), Some(source) if source.downcast_ref::<ValidationErrors>().is_some())
        );

        // ValidifyRejection::Valid Error
        let vr =
            ValidifyRejection::<io::Error>::Inner(io::Error::new(io::ErrorKind::Other, VALIDIFY));
        assert!(
            matches!(vr.source(), Some(source) if source.downcast_ref::<io::Error>().is_some())
        );
    }

    #[test]
    fn modified_into_response() {
        use validify::Validify;
        #[derive(Validify, Serialize)]
        struct Data {
            #[modify(trim)]
            v: String,
        }
        println!(
            "{:?}",
            Modified(Json(Data { v: "a  ".into() })).into_response()
        );
    }
}