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
//! # `vld_web::async_graphql` — async-graphql 7 integration for `vld`
//!
//! Validate GraphQL input objects (and any other input value) with `vld`
//! schemas. The input is converted to JSON
//! ([`InputType::to_value`] → `ConstValue::into_json`) and parsed by the
//! schema `S`; issues end up in the error's `extensions.issues` with the
//! same `{"path", "message", "code"}` shape as the
//! [HTTP 422 body](crate::common#error-bodies).
//!
//! | Item | Use case |
//! |---|---|
//! | [`VldValidator<S>`] | `#[graphql(validator(custom = "VldValidator::<S>::new()"))]` on an argument or input field |
//! | [`validate`] | Validate inside a resolver: `validate::<S, _>(&input)?` |
//! | [`vld_error`] | Turn a [`VldError`] into an [`async_graphql::Error`] with `extensions.issues` |
//!
//! # Example
//!
//! ```
//! use async_graphql::{EmptyMutation, EmptySubscription, InputObject, Object, Schema};
//! use vld_web::async_graphql::VldValidator;
//!
//! #[derive(InputObject)]
//! struct NewUser {
//! name: String,
//! email: String,
//! }
//!
//! // The rules, as a vld schema mirroring the input's JSON.
//! vld::schema! {
//! pub struct NewUserRules {
//! pub name: String => vld::string().min(2),
//! pub email: String => vld::string().email(),
//! }
//! }
//!
//! struct Query;
//!
//! #[Object]
//! impl Query {
//! async fn register(
//! &self,
//! #[graphql(validator(custom = "VldValidator::<NewUserRules>::new()"))] user: NewUser,
//! ) -> String {
//! user.name
//! }
//! }
//!
//! # tokio_test(async {
//! let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
//! let res = schema
//! .execute(r#"{ register(user: {name: "A", email: "bad"}) }"#)
//! .await;
//! let issues = res.errors[0].extensions.as_ref().unwrap().get("issues").unwrap();
//! assert_eq!(issues.clone().into_json().unwrap()[0]["path"], ".name");
//! # });
//! # fn tokio_test(f: impl std::future::Future<Output = ()>) {
//! # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(f)
//! # }
//! ```
//!
//! Guide: <https://s00d.github.io/vld/integrations/web/async-graphql>
use PhantomData;
use ;
use ;
use VldParse;
/// Message of every validation error this module builds.
const MESSAGE: &str = "Validation failed";
/// A `CustomValidator` checking an input value against the vld schema `S`.
///
/// `S` is any [`VldParse`] type (a `vld::schema!` struct, a
/// `#[derive(Validate)]` type) whose rules describe the input's JSON. On
/// failure the GraphQL error has the message
/// `Failed to parse "<Input>": Validation failed` (async-graphql adds the
/// prefix) and `extensions.issues`.
///
/// ```ignore
/// #[graphql(validator(custom = "VldValidator::<NewUserRules>::new()"))] user: NewUser
/// ```
>);
/// Validate an input value against the vld schema `S` inside a resolver.
///
/// ```ignore
/// async fn register(&self, user: NewUser) -> async_graphql::Result<String> {
/// vld_web::async_graphql::validate::<NewUserRules, _>(&user)?;
/// Ok(user.name)
/// }
/// ```
/// An [`async_graphql::Error`] with message `"Validation failed"` and
/// `extensions.issues`: `[{"path": ".name", "message": "...", "code":
/// "too_small"}]`, the issues of the HTTP 422 body.
/// Prelude — import everything you need.