mochi/validation

Types

pub type LocatedError =
  #(ValidationError, option.Option(#(Int, Int)))

Validation context tracks state during validation

pub type ValidationContext {
  ValidationContext(
    schema: schema.Schema,
    fragments: dict.Dict(String, @internal Fragment),
    defined_variables: set.Set(String),
    used_variables: set.Set(String),
    used_fragments: set.Set(String),
    errors: List(#(ValidationError, option.Option(#(Int, Int)))),
    current_type: option.Option(schema.ObjectType),
    fragment_spread_visited: set.Set(String),
    current_location: option.Option(#(Int, Int)),
  )
}

Constructors

pub type ValidationError {
  ParseError(message: String)
  UnknownField(field_name: String, type_name: String)
  MissingRequiredArgument(
    field_name: String,
    argument_name: String,
  )
  UnknownArgument(field_name: String, argument_name: String)
  DuplicateArgument(field_name: String, argument_name: String)
  UndefinedFragment(fragment_name: String)
  InvalidTypeCondition(fragment_name: String, type_name: String)
  FragmentOnNonCompositeType(
    fragment_name: String,
    type_name: String,
  )
  FragmentSpreadNotPossible(
    fragment_name: String,
    fragment_type: String,
    parent_type: String,
  )
  UnusedFragment(fragment_name: String)
  UndefinedVariable(variable_name: String)
  UnusedVariable(variable_name: String)
  DuplicateVariable(variable_name: String)
  VariableNotInputType(variable_name: String, type_name: String)
  DuplicateOperationName(name: String)
  DuplicateFragmentName(name: String)
  AnonymousOperationNotAlone
  SubscriptionMultipleRootFields(
    operation_name: option.Option(String),
  )
  CircularFragmentReference(
    fragment_name: String,
    path: List(String),
  )
  DirectiveNotAllowed(directive_name: String, location: String)
  UnknownDirective(directive_name: String)
  DuplicateDirective(directive_name: String)
  SelectionSetRequired(field_name: String, type_name: String)
  SelectionSetNotAllowed(field_name: String, type_name: String)
  FieldsCannotMerge(field_name: String, reason: String)
  ArgumentTypeMismatch(
    field_name: String,
    argument_name: String,
    expected_type: String,
    got: String,
  )
}

Constructors

  • ParseError(message: String)

    Query string failed to parse

  • UnknownField(field_name: String, type_name: String)

    Field does not exist on the type

  • MissingRequiredArgument(
      field_name: String,
      argument_name: String,
    )

    Required argument is missing

  • UnknownArgument(field_name: String, argument_name: String)

    Unknown argument provided

  • DuplicateArgument(field_name: String, argument_name: String)

    Duplicate argument name on a field

  • UndefinedFragment(fragment_name: String)

    Fragment is not defined

  • InvalidTypeCondition(fragment_name: String, type_name: String)

    Fragment type condition is invalid (type doesn’t exist)

  • FragmentOnNonCompositeType(
      fragment_name: String,
      type_name: String,
    )

    Fragment type condition must be on composite type

  • FragmentSpreadNotPossible(
      fragment_name: String,
      fragment_type: String,
      parent_type: String,
    )

    Fragment spread not possible (types don’t overlap)

  • UnusedFragment(fragment_name: String)

    Fragment is defined but not used

  • UndefinedVariable(variable_name: String)

    Variable is not defined

  • UnusedVariable(variable_name: String)

    Variable is defined but not used

  • DuplicateVariable(variable_name: String)

    Duplicate variable definition

  • VariableNotInputType(variable_name: String, type_name: String)

    Variable type must be an input type

  • DuplicateOperationName(name: String)

    Duplicate operation name

  • DuplicateFragmentName(name: String)

    Duplicate fragment name

  • AnonymousOperationNotAlone

    Anonymous operation must be alone

  • SubscriptionMultipleRootFields(
      operation_name: option.Option(String),
    )

    Subscription has multiple root fields

  • CircularFragmentReference(
      fragment_name: String,
      path: List(String),
    )

    Circular fragment reference

  • DirectiveNotAllowed(directive_name: String, location: String)

    Directive not allowed in location

  • UnknownDirective(directive_name: String)

    Unknown directive

  • DuplicateDirective(directive_name: String)

    Directive is used more than once where not allowed

  • SelectionSetRequired(field_name: String, type_name: String)

    Selection set required on non-leaf field

  • SelectionSetNotAllowed(field_name: String, type_name: String)

    Selection set not allowed on leaf field (scalar/enum)

  • FieldsCannotMerge(field_name: String, reason: String)

    Fields with same response name cannot be merged

  • ArgumentTypeMismatch(
      field_name: String,
      argument_name: String,
      expected_type: String,
      got: String,
    )

    Argument value has the wrong type

Values

pub fn format_error(error: ValidationError) -> String

Format a validation error as a human-readable string

pub fn format_errors(errors: List(ValidationError)) -> String

Format all validation errors as a single string

pub fn validate(
  document: @internal Document,
  schema: schema.Schema,
) -> Result(@internal Document, List(ValidationError))

Validate a document against a schema

pub fn validate_located(
  document: @internal Document,
  schema: schema.Schema,
) -> Result(
  @internal Document,
  List(#(ValidationError, option.Option(#(Int, Int)))),
)

Like validate, but preserves source locations for each error

pub fn validate_query(
  query: String,
  schema: schema.Schema,
) -> Result(@internal Document, List(ValidationError))

Validate a single query string against a schema (convenience function)

Search Document