I am parsing three request parameters, all of which are wrapped in an Option
type. If any of these Options are None
, then I want to return a 400 error. How do I check if any of these return values are of type None
?
Why not just like this?
if (o1.isEmpty || o2.isEmpty || o3.isEmpty) BadRequest("Foo")
Alternativeley depending on your implementation you might have your options in some kind of collection. Then you could use exists
if (parsedRequestParameters.exists(_.isEmpty)) BadRequest("Foo")
A third alternative you might like, in case you want to do something with the contents of your options:
val response = for {
v1 <- o1
v2 <- o2
v3 <- o3
} yield <some response depending on the values of o1..o3>
response getOrElse BadRequest("something wasn't specified")
Another possibility, added for completeness:
(o1, o2, o3) match {
case(Some(p1), Some(p2), Some(p3)) => Ok // Do stuff with p1, p2, p3
case _ => BadRequest
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With