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

fix: Input object null fields validation #1090

Merged
merged 5 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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/validation/Validator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ object Validator {
IO.when(
inputField.defaultValue.isEmpty &&
inputField.`type`().kind == __TypeKind.NON_NULL &&
!fields.contains(inputField.name)
fields.get(inputField.name).getOrElse(NullValue) == NullValue
)(
failValidation(
s"Required field '${inputField.name}' on object '${inputType.name.getOrElse("?")}' was not provided.",
Expand Down
32 changes: 32 additions & 0 deletions core/src/test/scala/caliban/validation/InputObjectSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package caliban.validation

import caliban.CalibanError
import caliban.GraphQL._
import caliban.RootResolver
import zio.test.Assertion._
import zio.test._
import zio.test.environment.TestEnvironment

object InputObjectSpec extends DefaultRunnableSpec {
override def spec: ZSpec[TestEnvironment, Any] =
suite("InputObjectSpect")(
frekw marked this conversation as resolved.
Show resolved Hide resolved
testM("fails if a non-null field on an input object is null") {
val query =
"""query {
| query(input: {string: null})
|}""".stripMargin

case class TestInputObject(string: String)
case class TestInput(input: TestInputObject)
case class Query(query: TestInput => String)
val gql = graphQL(RootResolver(Query(_.input.string)))

for {
int <- gql.interpreter
res <- int.execute(query)
} yield assert(res.errors.headOption)(
isSome((isSubtype[CalibanError.ValidationError](anything)))
)
}
)
}