Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Union validation (#230) #233

Merged
merged 1 commit into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/scala/caliban/CalibanError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object CalibanError {
*/
case class ValidationError(msg: String, explanatoryText: String, locationInfo: Option[LocationInfo] = None)
extends CalibanError {
override def toString: String = s"ValidationError Error: $msg}"
override def toString: String = s"ValidationError Error: $msg"
ghostdogpr marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
33 changes: 31 additions & 2 deletions core/src/main/scala/caliban/validation/Validator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ object Validator {
def validateSchema(rootType: RootType): IO[ValidationError, Unit] =
IO.foreach(rootType.types.values) { t =>
t.kind match {
case __TypeKind.ENUM => validateEnum(t)
case _ => IO.unit
case __TypeKind.ENUM => validateEnum(t)
case __TypeKind.UNION => validateUnion(t)
case _ => IO.unit
}
}
.unit
Expand Down Expand Up @@ -573,6 +574,34 @@ object Validator {
)
}

private def validateUnion(t: __Type): IO[ValidationError, Unit] = {

def isObject(t: __Type): Boolean = t.kind match {
case __TypeKind.OBJECT => true
case _ => false
}

t.possibleTypes match {
case None | Some(Nil) =>
IO.fail(
ValidationError(
s"Union ${t.name.getOrElse("")} doesn't contain any type.",
"A Union type must include one or more unique member types."
)
)
case Some(types) if !types.forall(isObject) =>
IO.fail(
ValidationError(
s"Union ${t.name.getOrElse("")} contains the following non Object types: " +
types.filterNot(isObject).map(_.name.getOrElse("")).filterNot(_.isEmpty).mkString("", ", ", "."),
s"The member types of a Union type must all be Object base types."
)
)
case _ => IO.unit
}

}

case class Context(
document: Document,
rootType: RootType,
Expand Down