graphql_schema_validation/
lib.rs

1#![deny(unsafe_code, missing_docs, rust_2018_idioms)]
2#![allow(unused_crate_dependencies)]
3#![doc = include_str!("../README.md")]
4
5mod context;
6mod diagnostics;
7mod validate;
8
9use self::{context::*, diagnostics::*};
10use std::collections::HashMap;
11
12/// Validate the GraphQL SDL document and produce a possibly empty collection of errors.
13pub fn validate(sdl: &str) -> Diagnostics {
14    validate_with_options(sdl, Options::default())
15}
16
17/// Validate the GraphQL SDL document and produce a possibly empty collection of errors.
18pub fn validate_with_options(sdl: &str, options: Options) -> Diagnostics {
19    let parsed_ast = match async_graphql_parser::parse_schema(sdl) {
20        Ok(ast) => ast,
21        Err(err) => {
22            return Diagnostics {
23                errors: vec![miette::miette! {
24                    "Syntax error: {}",
25                    err.to_string()
26                }],
27            };
28        }
29    };
30
31    let mut ctx = Context::new(
32        sdl,
33        HashMap::with_capacity(parsed_ast.definitions.len()),
34        Diagnostics::default(),
35        options,
36    );
37
38    validate::validate(&parsed_ast, &mut ctx);
39
40    ctx.diagnostics
41}
42
43bitflags::bitflags! {
44    /// Options to configure validation.
45    #[derive(Default)]
46    pub struct Options: u8 {
47        /// If included, this flag enables the validation checking that any type extension extends
48        /// a type defined in the same document.
49        const FORBID_EXTENDING_UNKNOWN_TYPES = 0b1;
50        /// Include validations that are in the current spec draft but not included or not relevant
51        /// in the 2021 edition of the spec.
52        const DRAFT_VALIDATIONS = 0b01;
53    }
54}