sova-vld 0.1.4

vld validation integration for Sova
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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! `Validate<T>` route attribute, erased hook, and `req.valid()`.

use crate::coerce::coerce_object;
use crate::ValidationError;
use sova_core::extend::{named, BoxFuture, MwEntry, RouteTable, RouteValue};
use sova_core::{App, Request, Response, Router};
use serde_json::{Map, Value};
use std::any::type_name;
use std::borrow::Cow;
use std::marker::PhantomData;
use std::sync::Arc;
use vld::schema::VldParse;

/// Where to read input for [`Validate`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidateSource {
    Body,
    Query,
    Params,
    /// Merge: query, then body, then params (params win).
    All,
    /// Multipart form (feature `form`).
    Form,
}

/// Route attribute: declare the validated DTO for a route.
pub struct Validate<T> {
    pub source: ValidateSource,
    _ty: PhantomData<fn() -> T>,
}

impl<T> Validate<T> {
    pub fn body() -> Self {
        Self {
            source: ValidateSource::Body,
            _ty: PhantomData,
        }
    }
    pub fn query() -> Self {
        Self {
            source: ValidateSource::Query,
            _ty: PhantomData,
        }
    }
    pub fn params() -> Self {
        Self {
            source: ValidateSource::Params,
            _ty: PhantomData,
        }
    }
    pub fn all() -> Self {
        Self {
            source: ValidateSource::All,
            _ty: PhantomData,
        }
    }
    pub fn form() -> Self {
        Self {
            source: ValidateSource::Form,
            _ty: PhantomData,
        }
    }
}

impl<T: Send + Sync + 'static> RouteValue for Validate<T> {
    fn label(&self) -> Cow<'static, str> {
        let src = match self.source {
            ValidateSource::Body => "body",
            ValidateSource::Query => "query",
            ValidateSource::Params => "params",
            ValidateSource::All => "all",
            ValidateSource::Form => "form",
        };
        Cow::Owned(format!("Validate<{src}: {}>", type_name::<T>()))
    }
}

/// Parsed DTO stored on the request by [`ValidateHook`].
#[derive(Debug, Clone)]
pub struct Validated<T>(pub T);

#[allow(clippy::type_complexity, clippy::result_large_err)]
type HookFn = Arc<dyn Fn(Request) -> BoxFuture<Result<Request, Response>> + Send + Sync>;

/// Type-erased runner invoked by route middleware.
pub struct ValidateHook {
    run: HookFn,
}

impl RouteValue for ValidateHook {
    fn label(&self) -> Cow<'static, str> {
        Cow::Borrowed("ValidateHook")
    }
}

impl ValidateHook {
    pub fn wrap<F>(f: F) -> Self
    where
        F: Fn(Request) -> BoxFuture<Result<Request, Response>> + Send + Sync + 'static,
    {
        Self { run: Arc::new(f) }
    }

    pub fn body<T: VldParse + Send + Sync + 'static>() -> Self {
        Self::wrap(|mut req| {
            Box::pin(async move {
                let value = read_body_json(&mut req).await.map_err(|e| e.respond(&req))?;
                finish::<T>(req, value)
            })
        })
    }

    pub fn query<T: VldParse + Send + Sync + 'static>() -> Self {
        Self::wrap(|req| {
            Box::pin(async move {
                let value = read_query_value(&req);
                finish::<T>(req, value)
            })
        })
    }

    pub fn params<T: VldParse + Send + Sync + 'static>() -> Self {
        Self::wrap(|req| {
            Box::pin(async move {
                let value = read_params_value(&req);
                finish::<T>(req, value)
            })
        })
    }

    pub fn all<T: VldParse + Send + Sync + 'static>() -> Self {
        Self::wrap(|mut req| {
            Box::pin(async move {
                let query = read_query_value(&req);
                let body = match read_body_json(&mut req).await {
                    Ok(b) => b,
                    Err(_) if matches!(req.method, http::Method::GET | http::Method::HEAD) => {
                        Value::Object(Map::new())
                    }
                    Err(e) => return Err(e.respond(&req)),
                };
                let params = read_params_value(&req);
                finish::<T>(req, merge_objects(query, body, params))
            })
        })
    }

    pub fn form<T: VldParse + Send + Sync + 'static>() -> Self {
        #[cfg(feature = "form")]
        {
            Self::wrap(|mut req| {
                Box::pin(async move {
                    let value = read_form_value(&mut req)
                        .await
                        .map_err(|e| e.respond(&req))?;
                    finish::<T>(req, value)
                })
            })
        }
        #[cfg(not(feature = "form"))]
        {
            let _ = type_name::<T>();
            Self::wrap(|_req| {
                Box::pin(async move {
                    Err(Response::text(
                        "Validate::form requires sova_vld feature `form`",
                    )
                    .status(500))
                })
            })
        }
    }

    pub(crate) fn run(&self, req: Request) -> BoxFuture<Result<Request, Response>> {
        (self.run)(req)
    }
}

#[allow(clippy::result_large_err)]
fn finish<T: VldParse + Send + Sync + 'static>(
    mut req: Request,
    value: Value,
) -> Result<Request, Response> {
    match T::vld_parse_value(&value) {
        Ok(v) => {
            req.set(Validated(v));
            Ok(req)
        }
        Err(e) => {
            let err = ValidationError::from(e);
            #[cfg(feature = "i18n")]
            let err = crate::i18n_msg::localize(err, &req);
            Err(err.respond(&req))
        }
    }
}

fn merge_objects(query: Value, body: Value, params: Value) -> Value {
    let mut map = Map::new();
    if let Value::Object(q) = query {
        map.extend(q);
    }
    if let Value::Object(b) = body {
        map.extend(b);
    }
    if let Value::Object(p) = params {
        map.extend(p);
    }
    Value::Object(map)
}

pub(crate) async fn read_body_json(req: &mut Request) -> Result<Value, ValidationError> {
    let bytes = req.body().await?;
    if bytes.is_empty() {
        return Ok(Value::Object(Map::new()));
    }
    serde_json::from_slice(&bytes).map_err(|e| {
        ValidationError(vld::error::VldError::single(
            vld::error::IssueCode::ParseError,
            format!("Invalid JSON: {e}"),
        ))
    })
}

pub(crate) fn read_params_value(req: &Request) -> Value {
    let mut map = Map::new();
    for (k, v) in &req.params {
        map.insert(k.clone(), Value::String(v.clone()));
    }
    Value::Object(map)
}

pub(crate) fn read_query_value(req: &Request) -> Value {
    let raw = req.raw_query();
    if !raw.is_empty() {
        // serde_qs cannot deserialize into `Value` at the top level; use Map.
        if let Ok(map) = serde_qs::from_str::<Map<String, Value>>(raw) {
            return Value::Object(map);
        }
    }
    let mut map = Map::new();
    for (k, v) in &req.query {
        map.insert(k.clone(), Value::String(v.clone()));
    }
    Value::Object(map)
}

#[cfg(feature = "form")]
pub(crate) async fn read_form_value(req: &mut Request) -> Result<Value, ValidationError> {
    let data = req.input().await?;
    let mut map = Map::new();
    for (name, values) in data.text_map() {
        match values.as_slice() {
            [] => {}
            [one] => {
                map.insert(name.clone(), Value::String(one.clone()));
            }
            many => {
                map.insert(
                    name.clone(),
                    Value::Array(many.iter().cloned().map(Value::String).collect()),
                );
            }
        }
    }
    for (name, uploads) in data.file_map() {
        if let Some(f) = uploads.first() {
            map.insert(
                name.clone(),
                serde_json::json!({
                    "filename": f.filename,
                    "content_type": f.content_type,
                    "size": f.data.len(),
                }),
            );
        }
    }
    Ok(Value::Object(map))
}

#[cfg_attr(not(feature = "openapi"), allow(dead_code))]
pub(crate) fn coerce_with_schema(value: &mut Value, schema: &Value) {
    if let Value::Object(map) = value {
        coerce_object(map, schema);
    }
}

/// Access DTO stored by [`ValidateHook`].
pub trait ValidExt {
    fn valid<T: Send + Sync + 'static>(&self) -> &T;
    fn take_valid<T: Send + Sync + 'static>(&mut self) -> Option<T>;
}

impl ValidExt for Request {
    fn valid<T: Send + Sync + 'static>(&self) -> &T {
        self.get::<Validated<T>>()
            .map(|v| &v.0)
            .unwrap_or_else(|| {
                panic!(
                    "validated `{}` missing — use `.validate_body::<T>()` (etc.) on the route",
                    type_name::<T>()
                )
            })
    }

    fn take_valid<T: Send + Sync + 'static>(&mut self) -> Option<T> {
        self.take::<Validated<T>>().map(|v| v.0)
    }
}

fn vld_mw() -> MwEntry {
    named("vld", |req: Request, next: sova_core::Next| async move {
        if let Some(hook) = req.route_meta::<ValidateHook>() {
            match hook.run(req).await {
                Ok(req) => next(req).await,
                Err(res) => res,
            }
        } else {
            next(req).await
        }
    })
}

#[cfg(feature = "openapi")]
pub trait ValidateSchema: crate::VldDocSchema {}
#[cfg(feature = "openapi")]
impl<T: crate::VldDocSchema> ValidateSchema for T {}

#[cfg(not(feature = "openapi"))]
pub trait ValidateSchema {}
#[cfg(not(feature = "openapi"))]
impl<T> ValidateSchema for T {}

/// Sugar: attach [`Validate`] + [`ValidateHook`] (+ OpenAPI schema when enabled).
pub trait ValidateRouteExt {
    fn validate_body<T>(&mut self) -> &mut Self
    where
        T: VldParse + Send + Sync + 'static + ValidateSchema;
    fn validate_query<T>(&mut self) -> &mut Self
    where
        T: VldParse + Send + Sync + 'static + ValidateSchema;
    fn validate_params<T>(&mut self) -> &mut Self
    where
        T: VldParse + Send + Sync + 'static + ValidateSchema;
    fn validate_all<T>(&mut self) -> &mut Self
    where
        T: VldParse + Send + Sync + 'static + ValidateSchema;
    fn validate_form<T>(&mut self) -> &mut Self
    where
        T: VldParse + Send + Sync + 'static + ValidateSchema;
}

fn attach_common<T>(router: &mut Router, validate: Validate<T>, hook: ValidateHook)
where
    T: Send + Sync + 'static,
{
    router.with(validate);
    router.with(hook);
    router.route_middleware(vld_mw());
}

#[cfg(feature = "openapi")]
fn attach_openapi(router: &mut Router, source: ValidateSource, schema: Value) {
    use sova_openapi::OpenApiValidate;
    router.with_update(|o: &mut OpenApiValidate| match source {
        ValidateSource::Body | ValidateSource::Form => o.body = Some(schema),
        ValidateSource::Query => o.query = Some(schema),
        ValidateSource::Params => o.params = Some(schema),
        ValidateSource::All => {
            o.body = Some(schema.clone());
            o.query = Some(schema.clone());
            o.params = Some(schema);
        }
    });
}

trait AsRouterMut {
    fn as_router_mut(&mut self) -> &mut Router;
}
impl AsRouterMut for Router {
    fn as_router_mut(&mut self) -> &mut Router {
        self
    }
}
impl AsRouterMut for App {
    fn as_router_mut(&mut self) -> &mut Router {
        &mut *self
    }
}

macro_rules! impl_ext {
    ($target:ty) => {
        impl ValidateRouteExt for $target {
            fn validate_body<T>(&mut self) -> &mut Self
            where
                T: VldParse + Send + Sync + 'static + ValidateSchema,
            {
                let router = self.as_router_mut();
                attach_common(router, Validate::<T>::body(), ValidateHook::body::<T>());
                #[cfg(feature = "openapi")]
                attach_openapi(router, ValidateSource::Body, T::json_schema());
                self
            }
            fn validate_query<T>(&mut self) -> &mut Self
            where
                T: VldParse + Send + Sync + 'static + ValidateSchema,
            {
                let router = self.as_router_mut();
                #[cfg(feature = "openapi")]
                {
                    let schema = T::json_schema();
                    let schema2 = schema.clone();
                    let hook = ValidateHook::wrap(move |req| {
                        let schema2 = schema2.clone();
                        Box::pin(async move {
                            let mut value = read_query_value(&req);
                            coerce_with_schema(&mut value, &schema2);
                            finish::<T>(req, value)
                        })
                    });
                    attach_common(router, Validate::<T>::query(), hook);
                    attach_openapi(router, ValidateSource::Query, schema);
                }
                #[cfg(not(feature = "openapi"))]
                {
                    attach_common(router, Validate::<T>::query(), ValidateHook::query::<T>());
                }
                self
            }
            fn validate_params<T>(&mut self) -> &mut Self
            where
                T: VldParse + Send + Sync + 'static + ValidateSchema,
            {
                let router = self.as_router_mut();
                #[cfg(feature = "openapi")]
                {
                    let schema = T::json_schema();
                    let schema2 = schema.clone();
                    let hook = ValidateHook::wrap(move |req| {
                        let schema2 = schema2.clone();
                        Box::pin(async move {
                            let mut value = read_params_value(&req);
                            coerce_with_schema(&mut value, &schema2);
                            finish::<T>(req, value)
                        })
                    });
                    attach_common(router, Validate::<T>::params(), hook);
                    attach_openapi(router, ValidateSource::Params, schema);
                }
                #[cfg(not(feature = "openapi"))]
                {
                    attach_common(router, Validate::<T>::params(), ValidateHook::params::<T>());
                }
                self
            }
            fn validate_all<T>(&mut self) -> &mut Self
            where
                T: VldParse + Send + Sync + 'static + ValidateSchema,
            {
                let router = self.as_router_mut();
                #[cfg(feature = "openapi")]
                {
                    let schema = T::json_schema();
                    let schema2 = schema.clone();
                    let hook = ValidateHook::wrap(move |mut req| {
                        let schema2 = schema2.clone();
                        Box::pin(async move {
                            let mut query = read_query_value(&req);
                            coerce_with_schema(&mut query, &schema2);
                            let body = match read_body_json(&mut req).await {
                                Ok(b) => b,
                                Err(_)
                                    if matches!(
                                        req.method,
                                        http::Method::GET | http::Method::HEAD
                                    ) =>
                                {
                                    Value::Object(Map::new())
                                }
                                Err(e) => return Err(e.respond(&req)),
                            };
                            let mut params = read_params_value(&req);
                            coerce_with_schema(&mut params, &schema2);
                            finish::<T>(req, merge_objects(query, body, params))
                        })
                    });
                    attach_common(router, Validate::<T>::all(), hook);
                    attach_openapi(router, ValidateSource::All, schema);
                }
                #[cfg(not(feature = "openapi"))]
                {
                    attach_common(router, Validate::<T>::all(), ValidateHook::all::<T>());
                }
                self
            }
            fn validate_form<T>(&mut self) -> &mut Self
            where
                T: VldParse + Send + Sync + 'static + ValidateSchema,
            {
                let router = self.as_router_mut();
                attach_common(router, Validate::<T>::form(), ValidateHook::form::<T>());
                #[cfg(feature = "openapi")]
                attach_openapi(router, ValidateSource::Form, T::json_schema());
                self
            }
        }
    };
}

impl_ext!(Router);
impl_ext!(App);

/// POST/PUT/PATCH routes that lack a [`ValidateHook`].
pub fn missing_validate_routes(table: &RouteTable) -> Vec<String> {
    let mut missing = Vec::new();
    for entry in &table.0 {
        let sova_core::extend::RouteEntry::Http {
            method,
            path,
            meta,
        } = entry
        else {
            continue;
        };
        if !matches!(
            *method,
            http::Method::POST | http::Method::PUT | http::Method::PATCH
        ) {
            continue;
        }
        if meta.get::<ValidateHook>().is_some() {
            continue;
        }
        missing.push(format!("{method} {path}"));
    }
    missing
}

/// Plugin: coverage check for POST/PUT/PATCH without [`ValidateHook`].
pub struct Vld;

impl sova_core::Plugin for Vld {
    fn id(&self) -> &'static str {
        "vld"
    }

    fn meta(&self) -> sova_core::PluginMeta {
        sova_core::PluginMeta::new("Validation")
            .description("Request validation hooks and coverage check")
            .version(env!("CARGO_PKG_VERSION"))
    }

    fn install(self, app: &mut App) {
        app.register_audit("vld", |state| async move {
            let Some(table) = state.get::<RouteTable>() else {
                return Ok(());
            };
            let missing = missing_validate_routes(&table);
            if missing.is_empty() {
                Ok(())
            } else {
                Err(sova_core::Error::Internal(format!(
                    "POST/PUT/PATCH without Validate: {}",
                    missing.join(", ")
                )))
            }
        });
    }
}