Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save seanparsons/2844124 to your computer and use it in GitHub Desktop.
Save seanparsons/2844124 to your computer and use it in GitHub Desktop.
validateanyProduct.scala
def validateProduct[T <: Product](product: T): Seq[String] = product.productIterator.flatMap{value => value match {
case null => Seq("null attributes are not allowed for " + product)
case string: String if (string.isEmpty) => Seq("Empty strings are not allowed for " + product)
case innerProduct: Product => validateProduct(innerProduct)
case _ => Seq()
}}.toSeq
@missingfaktor
Copy link

Slightly cleaner, perhaps.

def validateProduct[T <: Product](product: T): Seq[String] = product.productIterator.toSeq collect {
  case null => "null attributes are not allowed for " + product
  case string: String if (string.isEmpty) => "Empty strings are not allowed for " + product
  case innerProduct: Product => validateProduct(innerProduct)
}

@seanparsons
Copy link
Author

Yeah, that's much nicer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment