Skip to content

Commit

Permalink
KnownArgumentNames rule now works correctly with directives
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegIlyenko committed Aug 7, 2015
1 parent 1a88cb1 commit d751be4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/main/scala/sangria/validation/Violation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ case class UnknownArgViolation(argName: String, fieldName: String, typeName: Str
lazy val errorMessage = s"Unknown argument '$argName' on field '$fieldName' of type '$typeName'.$astLocation"
}

case class UnknownDirectiveArgViolation(argName: String, dirName: String, sourceMapper: Option[SourceMapper], positions: List[Position]) extends AstNodeViolation {
lazy val errorMessage = s"Unknown argument '$argName' on directive '$dirName'.$astLocation"
}

case class UnknownDirectiveViolation(name: String, sourceMapper: Option[SourceMapper], positions: List[Position]) extends AstNodeViolation {
lazy val errorMessage = s"Unknown directive '$name'.$astLocation"
}
Expand Down
34 changes: 26 additions & 8 deletions src/main/scala/sangria/validation/rules/KnownArgumentNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,32 @@ class KnownArgumentNames extends ValidationRule {
override def visitor(ctx: ValidationContext) = new AstValidatingVisitor {
override val onEnter: ValidationVisit = {
case ast.Argument(name, _, pos) =>
ctx.typeInfo.fieldDef match {
case Some(field) if !field.arguments.exists(_.name == name) =>
Left(Vector(UnknownArgViolation(
name,
field.name,
ctx.typeInfo.previousParentType.fold("")(SchemaRenderer.renderTypeName(_, topLevel = true)),
ctx.sourceMapper,
pos.toList)))
ctx.typeInfo.ancestors.drop(1).head match {
case _: ast.Field =>
ctx.typeInfo.fieldDef match {
case Some(field) if !field.arguments.exists(_.name == name) =>
Left(Vector(UnknownArgViolation(
name,
field.name,
ctx.typeInfo.previousParentType.fold("")(SchemaRenderer.renderTypeName(_, topLevel = true)),
ctx.sourceMapper,
pos.toList)))
case _ =>
Right(Continue)
}

case _: ast.Directive =>
ctx.typeInfo.directive match {
case Some(dir) if !dir.arguments.exists(_.name == name) =>
Left(Vector(UnknownDirectiveArgViolation(
name,
dir.name,
ctx.sourceMapper,
pos.toList)))
case _ =>
Right(Continue)
}

case _ =>
Right(Continue)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ class KnownArgumentNamesSpec extends WordSpec with ValidationSupport {
}
""")

"directive args are known" in expectPasses(
"""
{
dog @skip(if: true)
}
""")

"undirective args are invalid" in expectFails(
"""
{
dog @skip(unless: true)
}
""",
List(
"Unknown argument 'unless' on directive 'skip'." -> Some(Pos(3, 21))
))

"invalid arg name" in expectFails(
"""
fragment invalidArgName on Dog {
Expand Down

0 comments on commit d751be4

Please sign in to comment.