Web frameworks
Validate async-graphql input with a vld schema: as a custom validator on an argument or input field, or by hand inside a resolver. Failures are GraphQL errors whose extensions.issues has the same {path, message, code} items as the HTTP error body.
Install
Cargo.toml
[dependencies]
vld = "0.8"
vld-web = { version = "0.8", features = ["async-graphql"] }
async-graphql = "7"
Module vld_web::async_graphql. One major is supported: async-graphql 7 (Rust 1.89 for 7.2, see MSRV).
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
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let res = schema.execute(r#"{ register(user: {name: "A", email: "bad"}) }"#).await;
The response:
{
"data": null,
"errors": [{
"message": "Failed to parse \"NewUser\": Validation failed",
"locations": [{ "line": 1, "column": 12 }],
"extensions": {
"issues": [
{ "path": ".name", "message": "String must be at least 2 characters", "code": "too_small" },
{ "path": ".email", "message": "Invalid email address", "code": "invalid_string" }
]
}
}]
}
Inside a resolver
async fn create(&self, user: NewUser) -> async_graphql::Result<String> {
vld_web::async_graphql::validate::<NewUserRules, _>(&user)?;
Ok(user.name)
}
The error message is Validation failed and carries the resolver's path.
API
| Item | Purpose |
|---|---|
VldValidator<S> (::new(), Default) | CustomValidator<T> for any input type T: checks T's JSON against schema S |
validate::<S, T>(&value) | Same check in a resolver; async_graphql::Result<()> |
vld_error(&VldError) | async_graphql::Error "Validation failed" with extensions.issues |
prelude | All of the above plus vld::prelude::* |
Gotchas
Sis anyVldParsetype (avld::schema!struct, a#[derive(Validate)]type) describing the input's GraphQL JSON: field names as the client sends them (camelCaseby default in async-graphql), enums as their GraphQL names.- The input is converted with
InputType::to_value()→ConstValue::into_json(), so it is checked after async-graphql has parsed it: type errors (a string for anInt) are async-graphql's own errors, before vld runs. - On an
Option<T>argument the validator runs only when a value is present; on a list it runs per item (async-graphql'slistvalidator flag). - async-graphql prefixes the validator's message (
Failed to parse "NewUser": …); readextensions.issues, not the message.
vld_web::common
Framework-independent HTTP helpers behind every vld-web module — value coercion, query/form/header/cookie parsing, content-type checks and the error body — for writing your own adapter.
WebSocket
Validate WebSocket messages with vld schemas — tungstenite (tokio-tungstenite, async-tungstenite) and axum.