Skip to content

Commit

Permalink
fixup! API refactor to make it harder to ignore errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Nov 21, 2023
1 parent c779602 commit 3040405
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/apollo-compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Schema>` 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
Expand Down
14 changes: 13 additions & 1 deletion crates/apollo-compiler/src/validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,32 @@ 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<T>` 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.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Valid<T>(pub(crate) T);

impl<T> Valid<T> {
/// 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
Expand Down

0 comments on commit 3040405

Please sign in to comment.