Skip to content

Instantly share code, notes, and snippets.

@conikeec
Forked from wfaler/validateanyProduct.scala
Created May 31, 2012 16:01
Show Gist options
  • Save conikeec/2844395 to your computer and use it in GitHub Desktop.
Save conikeec/2844395 to your computer and use it in GitHub Desktop.
validateanyProduct.scala
// modify to collect errors, use Either or whatever floats your boat, just an example that throws
// IllegalArgumentException on the first null or empty String it encounters, if there is one.
// Works for any case class or collection such as List.
def validateProduct[T <: Product](product: T): Unit = product.productIterator foreach {
case null => throw new IllegalArgumentException("null attributes are not allowed for " + product)
case x: String if x.isEmpty => throw new IllegalArgumentException("Empty strings are not allowed for " + product)
case x: Product => validateProduct(x)
case _ => {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment