AppEngine NDB property validations

本小妞迷上赌 提交于 2019-12-13 03:07:19

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15200952/appengine-ndb-property-validations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!