Search code examples
pythongoogle-app-enginepython-2.7app-engine-ndb

NDB validator argument vs extending base property classes


I'm using AppEngine NDB properties and I wonder what would be the best approach to:

  • limit StringProperty to be not longer than 100 characters
  • apply regexp validation to StringProperty
  • prohibit IntegerProperty to be less than 0

Would it be best to use the validator argument or to subclass base ndb properties?


Solution

  • It depends. Are the restrictions one-off or is any particular restriction going to be reused in many different fields/models?

    For one-off restrictions, the validator argument is simpler and involves less boilerplate.

    For reuse, subclassing lets you avoid having to repeatedly specify the validator argument.