Squeryl Custom Field Types

I am trying to create a custom field type in Squeryl. This field represents Isin code , so it is backed by a string field. Following the example in the documentation , I added a simple check before creating a new isin (it doesn't matter what the Isin code is or how it works):

trait Domain[A] { self: CustomType[A] => def validate(a: A): Unit validate(value) } class Isin(v: String) extends StringField(v) with Domain[String] { println("instantiating Isin") override def validate(s: String) { println("calling validate with " + s) assert(checkIsin(s)) } private def checkIsin(isin: String): Boolean = { // never mind the exact procedure } } 

I added a few println to find out what happens. I use this field inside the model, for example

 case class Asset( val id: Long = 0, val isin: Isin ) extends KeyedEntity[Long] object Asset { import Database.assets def create(isinCode: String) { inTransaction { assets.insert(new Asset(isin = new Isin(isinCode))) } } } 

Now when I call Asset.create("US0378331005") (a valid ISIN), I get an exception. In stacktrace, it turns out that this exception is caused by invoking the init method with a null value, which is supposedly passed to checkIsin . Indeed, println operators print

 calling validate with US0378331005 Instantiating Isin calling validate with 

So it seems that the validate method is actually called twice, but the second time it gets null .

What is going wrong?

0
scala squeryl
Aug 29 '12 at 10:15
source share
1 answer

There are several issues here. First, you seem to be using Lift Record, and if that happens, you are not properly implementing your validation logic. The correct way to check the record field is to override

 def validations: List[ValidationFunction] 

where ValidationFunction is an alias of type

 type ValidationFunction = ValueType => List[FieldError] 

and in your case ValueType == String.

The next question is your domain tag. Since your verification call is built into the class definition, it will be called when your field is built. Obviously there is no value at this point, so you see println that refers to an empty value. I believe that the order in which you see them has something to do with the flow of your application. You have an existing entry that is being verified, and after that a new entry is created that runs the next 2 println statements.

+1
Sep 03
source share



All Articles