Open GoogleCodeExporter opened 9 years ago
Somehow I missed this in the tracker. Could you elaborate a bit more on a
possible API for this feature that would satisfy your requirements? Could you
perhaps implement a version of it for a specific model class in user code to
show the pattern?
Original comment by guido@google.com
on 27 Sep 2012 at 5:50
I also starred this issue, and my issue was actually very similar (altho less
specific and more general).
I was looking for a data validation hook that allows me to "sanitize" data
going into a data model (e.g. ensure that a field contains a valid phone
number, but then morph that data down into an Integer for example).
Pseudo Code:
m = Model()
m.phonenumber = "(480) 555 1212"
>> Validate: Has 10 digits? Yes, lets strip it down to an "int" and re-write
the property
m.put()
m = Model()
print m.phonenumber
>> 4805551212
Then, I could write a Jinja filter (or similar) to display the value how my
template chooses.
Original comment by ric...@onixnet.com
on 27 Sep 2012 at 6:06
Ah, but if the validation is at the property level you can do this very easily
by defining your own subclass of StringProperty() of IntegerProperty() and
overriding the _validate() method. Just make the latter raise an exception if
the value is incorrect and return the normalized value otherwise. See
https://developers.google.com/appengine/docs/python/ndb/subclassprop
Original comment by guido@google.com
on 27 Sep 2012 at 6:22
Ahh, Makes sense. This is a better fit for my specific use case (e.g.
PhoneNumberProperty)
Not too sure about OP's use case :D
Original comment by ric...@onixnet.com
on 27 Sep 2012 at 6:30
Ironically, the old db package has a PhoneNumberProperty but it doesn't have
any validation except insisting on a non-empty value, which is exactly the
opposite from what you want. Also note that even if you do implement a
validating PhoneNumberProperty, you're still going to want to implement a
separate validation routine in your UI so users get reasonable error messages
(preferably in JavaScript so they won't need a server roundtrip). Undoubtedly
web frameworks have form features to support this. But it's still necessary to
double-check the validation on the server-side (although a good web framework
would use the same specification to generate both the JavaScript and the
server-side validation code).
I think Jeffrey wants to check constraints across properties, and things that
aren't properties (e.g. the key id and the ancestor).
I think the key id and ancestor could probably be validated by overriding the
_key and key fields: these both point to a ModelKey object; that class
subclasses Property and you can probably just override the _validate() method
in the subclass. But that still leaves validation involving multiple properties
as a use case. (Also subclassing ModelKey is undocumented. :-)
Original comment by guido@google.com
on 27 Sep 2012 at 6:40
I'll "bump" this and say that having an ability to validate the model would be
very helpful for different models. For example, modeling a meeting with start
time and end time, would be helpful to validate that end time is not before
start time.
Having a per property validator would not be very useful because that relies on
assignment ordering.
My current solution is to override the put method, but as mentioned this is
pretty bad for error handling.
Original comment by AceP...@gmail.com
on 18 Apr 2014 at 1:16
I'd alos like a validation hook at the model level.
This is my workaround: http://stackoverflow.com/a/25786896/2003429
Original comment by a...@schenkman.info
on 11 Sep 2014 at 12:00
Original issue reported on code.google.com by
jyass...@gmail.com
on 15 Apr 2012 at 10:54