Frage

Assuming that I have a Scala case class that is persisted using the Salat/Casbah/Mongo stack, I want to set up pre-persistence validation logic like I could easily do in Rails using ActiveRecord hooks or in Java using JSR 303 bean validation.

Perhaps there is a better way to think about this in a functional paradigm, but I want to accomplish something like the following:

case class SomeItem(
  id: ObjectId = new ObjectId,
  someProperty: String) {

  @PrePersistence
  def validate() = {
     //perform some logic
     //fail document save in certain conditions
  }
}

I am having trouble finding any documentation on how to do something like this in Salat. I do see a @Persist annotation but it seems focused on serializing specific values and not creating hooks.

It seems like one option is to override the save method in the SalatDAO for my case class. Does anyone have an example of this or know of a better, built-in way to handle validation tied to a pre-persistence event?

Thanks!

War es hilfreich?

Lösung

Salat developer here.

Yes, @Persist is simply for ensuring that fields that aren't in the constructor are serialized - this is particularly useful for manipulating data in MongoDB. One example is where you want to ensure that all the fields are populated with a value so you can sort sensibly, but the value is an Option which may not be present.

Unfortunately, the Java driver doesn't offer lifecycle callbacks like the Ruby driver :(

But what you want should be easy enough to do. Please file an issue at https://github.com/novus/salat/issues and describe how you would like the validation to behave - we can start a discussion and I can try to get something in for you in the 1.9.2 release.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top