0

I'm using squeryl with playframework, and defined some entifies:

case class User(name:String, age:Int, ...) extends KeyedEntity[Long] {
  val id = 0
}

The length of name field defined as varchar(50) in database.

Since we need to validate the values such as name before saving to database, I have to validate them manually:

checkLengthOf(user.name, 50);

If there are a lot of more fields, I need to do a lot of validation manually:

checkLengthOf(user.field1, ???);
checkLengthOf(user.field2, ???);
checkLengthOf(user.field3, ???);
checkLengthOf(user.field3, ???);
checkLengthOf(user.field4, ???);

I want to know is there any simple way to do this?

When I was in Java, there are some orm frameworks provide some annotations to do the validation automatically, can I do the same with squeryl?

Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

0

Squeryl itself does not provide validation. It is purposely designed to only do what is necessary to be a very effective DSL for interacting with your database.

If you really want to automate this though, you could do it yourself. Squeryl 0.9.6 (which is at RC2 at the moment) provides life cycle callback methods which you could use to plug in your own validation logic. It isn't documented yet but you can see some examples In the tests.

Since Squeryl makes use of POSOs, which compile to regular Java objects, you could even use this to plug-in Java libraries like hibernate-validations..... if you really wanted to.

I don't know how you're receiving your updates, but one other thing to check out might be lift-squeryl-record. It allows you to define your fields as objects along with their validation logic. Record also integrates with Lift's CRUD generation and LiftScreen which is a pretty powerful tool for defining forms along with validation and processing logic.

Dave Whittaker
  • 3,102
  • 13
  • 14