问题
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