0

I wonder what the best approach is for validating NDB entity properties likes:

  • a date must be in the future
  • a grade (integer property) must be between 1 and 10
  • a reference to another entity must have certain property values (e.g. book.category.active must be True)

I'm also using WTForms to validate submitted requests, but I want to enforce validations also on a lower level like the datastore entities itself.

So basically what I'm looking for is to call a validate on a datastore entity to see if it's valid or not. In case it's valid I can put the entity to the datastore, but if it's not valid I want to retrieve the invalid properties including the applies validator that did not validate successfully.

Another reason WTForms might not be sufficient is that I'm experiencing with the new Cloud Endpoints. In this model I'm receiving the actial entity and not a http request.

How are other AppEngine users solving this?

Marcel Overdijk
  • 11,041
  • 17
  • 71
  • 110
  • Note in Java I would use something like Hibernate Validator and/or the validation mechanism of the Spring framework. – Marcel Overdijk Mar 04 '13 at 11:56
  • 1
    Unclear what your question is. You can subclass NDB Property classes and define a validate() method. This is in the docs though -- perhaps that's not what you're asking? – Guido van Rossum Mar 05 '13 at 15:53
  • Hi Guido, yes I noticed this in the docs and probably this is what I should use. The docs are unfortunately not really clear enough for me how to use this validate() method. E.g. is there also a method to check if entity is valid? And to check which properties are invalid? – Marcel Overdijk Mar 05 '13 at 19:40

1 Answers1

0

Not this is is always the best solution, but you could roll your own. Just pre-define a bunch of properties with reg-exs/mins and maxs etc. It seems like your properties are straight forward enough that it wouldn't be too difficult.

flynn
  • 1,572
  • 2
  • 12
  • 26