mavarazy
mavarazy

Reputation: 7735

Is there a way to use Playframework Json macro generated format for partial entity validation?

My program stack is ReactiveMongo 0.11.0, Scala 2.11.6, Play 2.4.2.

I'm adding PATCH functionality support to my Controllers. I want it to be type safe, so that PATCH would not mess the data in Mongo.

Current dirty solution of doing this, is

  1. Reading object from Mongo first,
  2. Performing JsObject.deepMerge with provided patch,
  3. Checking that value can still be deserialized to target type.
  4. Serializing merged object back to JsObject, and check, that patch contains only fields that are present in merged Json (So that there is no trash added to the stored object)
  5. Call actual $set on mongo

This is obviously not perfect, but works fine. I would write macros to generate appropriate format generalization, but it might take too much time, which I currently lack of.

Is there a way to use Playframework Json macro generated format for partial entity validation like this? Or any other solution, that can be easily integrated in Playframework for that matters.

Upvotes: 0

Views: 70

Answers (1)

mavarazy
mavarazy

Reputation: 7735

With the help of @julien-richard-foy made a small library, to do exactly what I wanted.

https://github.com/clemble/scala-validator

Need to add some documentation, and I'll publish it to repository.

Upvotes: 0

Related Questions