graphql_query/validate/mod.rs
1//! # Validation Rules for GraphQL ASTs
2//!
3//! This module contains logic to run validation rules on GraphQL Query Language documents.
4//! It provides rules that have already been implemented to validate a document as much as it can
5//! without using any schema information, which are rules grouped into this module's [`DefaultRules`](rules::DefaultRules)
6//! and utilities to create your own [`ValidationRules`](ValidationRule).
7//!
8//! The rules this module already comes with are:
9//!
10//! - [`rules::KnownFragmentNames`]: validates that all spread fragments are defined
11//! - [`rules::LoneAnonymousOperation`]: validates that a document only contains a single anonymous operation
12//! - [`rules::NoFragmentCycles`]: validates that no fragment is spread in on itself to avoid looping
13//! - [`rules::NoUndefinedVariables`]: checks that all used variables are defined per operation
14//! - [`rules::NoUnusedFragments`]: validates that all fragments in a document are used at least once
15//! - [`rules::UniqueArgumentNames`]: checks for arguments that are used to not contain duplicates
16//! - [`rules::UniqueFragmentNames`]: checks that no fragments share the same name
17//! - [`rules::UniqueOperationNames`]: checks that no operations share the same name
18//! - [`rules::UniqueVariableNames`]: checks that no variables share the same name
19//!
20//! The [visit](crate::visit) module is used to actually execute validation rules.
21//! The [`ValidationRule`] trait is simply defined to implement the [Visitor](crate::visit::Visitor) trait
22//! and to accept the [`ValidationContext`], which is used to keep track of validation errors.
23//!
24//! As such, the [`DefaultRules`](rules::DefaultRules) rule is a [`ValidationRule`] itself that's
25//! composed using the [`ComposedVisitor`](crate::visit::ComposedVisitor) utility.
26//!
27//! All rules must implement the `Default` trait, which makes it easier to quickly run a validation
28//! rule and isolates them from external state, since no validation requires any external state.
29//!
30//! For example, this is one way to run a validation rule, in this case `DefaultRules`:
31//!
32//! ```
33//! use graphql_query::{ast::*, validate::*};
34//!
35//! let ctx = ASTContext::new();
36//! let document = Document::parse(&ctx, "{ field }").unwrap();
37//!
38//! DefaultRules::validate(&ctx, &document).unwrap()
39//! ```
40//!
41//! Another way is to utilize the [`ValidateNode`] trait instead to run validation starting from an
42//! AST Node rather from the rule itself:
43//!
44//! ```
45//! use graphql_query::{ast::*, validate::*};
46//!
47//! let ctx = ASTContext::new();
48//! let document = Document::parse(&ctx, "{ field }").unwrap();
49
50//! document.validate::<DefaultRules>(&ctx).unwrap()
51//! ```
52
53#[allow(clippy::module_inception)]
54mod validate;
55
56mod context;
57
58pub mod rules;
59pub use context::ValidationContext;
60pub use rules::DefaultRules;
61pub use validate::*;