NDB Validator Prop Fields

I deployed a special validator for my ndb stringProperties to remove malicious code for my site.

def stringValidator(prop, value):
  lowerValue = value.lower()
  stripped = str(utils.escape(lowerValue))

  if stripped != lowerValue:
    raise datastore_errors.BadValueError(prop)

  return stripped

Elsewhere, I will catch this error and return the failure to the client. I want to be able to return the type of a property that could not be verified.

except datastore_errors.BadValueError as err:

If I print(err), I get:

StringProperty('email', validator=<function stringValidator at 0x1079e11b8>)

I see that this StringProperty contains the name of the property that I want to return: 'email'. How to extract it?

EDIT: Dmitry gave me the most important half of the answer - to access the value of the error object after passing the property ._name, I need to use:

str(err.args[0])
+4
source share
1 answer

_name.

from google.appengine.ext import ndb

def stringValidator(prop, value):
  lowerValue = value.lower()
  stripped = 'bla'

  if stripped != lowerValue:
    raise datastore_errors.BadValueError(prop._name)

  return stripped

class Foo(ndb.Model):
  email = ndb.StringProperty(validator=stringValidator)

Foo(email='blas')  # raises BadValueError: email

: "human friendly",

email = ndb.StringProperty(validator=stringValidator, verbose_name='E-mail')

, _verbose_name.

+2

All Articles