Scala field Validation using “require”

Jul - 08 2016 | By

Something i often forget about to easily

 

When we create a Case Class we should valid the field values

 

case class Person(FirstName: String, LastName: String) {
  require(!FirstName.isEmpty, "Person.FirstName.empty")
  require(FirstName.length < 64, "Person.FirstName.sizeExceeded")
  require(FirstName.matches("^[a-zA-Z](((?!__)[a-zA-Z0-9_])*[a-zA-Z0-9])?$"), "Person.FirstName.mustStartWithLetter")

  require(!LastName.isEmpty, "Person.LastName.empty")
  require(LastName.length < 64, "Person.LastName.sizeExceeded")
  require(LastName.matches("^[a-zA-Z](((?!__)[a-zA-Z0-9_])*[a-zA-Z0-9])?$"), "Person.LastName.mustStartWithLetter")
}

Comments are closed. Please see front page on how to contact me