diff --git a/crates/apollo-compiler/CHANGELOG.md b/crates/apollo-compiler/CHANGELOG.md index 8b0fbcbbe..ebd923eeb 100644 --- a/crates/apollo-compiler/CHANGELOG.md +++ b/crates/apollo-compiler/CHANGELOG.md @@ -31,7 +31,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm On success they return the schema or document (unmodified) wrapped in a `Valid<_>` marker type, which is **immutable**. - Change `ExecutableDocument` to require a `&Valid` instead of `&Schema`, - so that schema validation is mandatory. + forcing callers to either run validation or opt out explicitly with `Valid::assert_valid`. - Make `parse_mixed` and `to_mixed` validate both the schema and document. Rename them with a `_validate` suffix. - Corresponding changes to all of the above in `Parser` method signatures diff --git a/crates/apollo-compiler/src/validation/mod.rs b/crates/apollo-compiler/src/validation/mod.rs index ad70a6ab4..27d46fa34 100644 --- a/crates/apollo-compiler/src/validation/mod.rs +++ b/crates/apollo-compiler/src/validation/mod.rs @@ -38,13 +38,15 @@ pub(crate) use validation_db::{ValidationDatabase, ValidationStorage}; /// Wraps a [`Schema`] or [`ExecutableDocument`] to mark it /// as [valid](https://spec.graphql.org/October2021/#sec-Validation). /// -/// This is obtained from one of: +/// This is obtained either by running validation with one of: /// /// * [`Schema::parse_and_validate`] /// * [`Schema::validate`] /// * [`ExecutableDocument::parse_and_validate`] /// * [`ExecutableDocument::validate`] /// +/// … or by explicitly skipping it with [`Valid::assert_valid`]. +/// /// The schema or document inside `Valid` is immutable (`&mut T` is not given out). /// It can be extracted with [`into_inner`][Self::into_inner], /// such as to mutate it then possibly re-validate it. @@ -52,6 +54,16 @@ pub(crate) use validation_db::{ValidationDatabase, ValidationStorage}; pub struct Valid(pub(crate) T); impl Valid { + /// Construct a `Valid` document without actually running validation. + /// + /// The caller takes responsibility to ascertain that + /// the document is known through some other means to be valid. + /// For example, if it was loaded from some external storag + /// where it was only stored after validation. + pub fn assert_valid(document: T) -> Self { + Self(document) + } + /// Extract the schema or document, such as to mutate it then possibly re-validate it. pub fn into_inner(self) -> T { self.0