Skip to content

Commit

Permalink
feat: Add field information when raising validation errors. (#956)
Browse files Browse the repository at this point in the history
* feat: Add field information when raising validation errors.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* fix: Fix string formatting error

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
Z33DD and gcf-owl-bot[bot] committed Feb 29, 2024
1 parent 7dfd56b commit 17caf0b
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions google/cloud/ndb/model.py
Expand Up @@ -1485,7 +1485,9 @@ def _set_value(self, entity, value):
if self._repeated:
if not isinstance(value, (list, tuple, set, frozenset)):
raise exceptions.BadValueError(
"Expected list or tuple, got {!r}".format(value)
"In field {}, expected list or tuple, got {!r}".format(
self._name, value
)
)
value = [self._do_validate(v) for v in value]
else:
Expand Down Expand Up @@ -2372,7 +2374,9 @@ def _validate(self, value):
.BadValueError: If ``value`` is not a :class:`bool`.
"""
if not isinstance(value, bool):
raise exceptions.BadValueError("Expected bool, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected bool, got {!r}".format(self._name, value)
)
return value

def _from_base_type(self, value):
Expand Down Expand Up @@ -2417,7 +2421,9 @@ def _validate(self, value):
to one.
"""
if not isinstance(value, six.integer_types):
raise exceptions.BadValueError("Expected integer, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected integer, got {!r}".format(self._name, value)
)
return int(value)


Expand Down Expand Up @@ -2447,7 +2453,9 @@ def _validate(self, value):
to one.
"""
if not isinstance(value, six.integer_types + (float,)):
raise exceptions.BadValueError("Expected float, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected float, got {!r}".format(self._name, value)
)
return float(value)


Expand Down Expand Up @@ -2578,7 +2586,9 @@ def _validate(self, value):
exceeds the maximum length (1500 bytes).
"""
if not isinstance(value, bytes):
raise exceptions.BadValueError("Expected bytes, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected bytes, got {!r}".format(self._name, value)
)

if self._indexed and len(value) > _MAX_STRING_LENGTH:
raise exceptions.BadValueError(
Expand Down Expand Up @@ -2761,11 +2771,13 @@ def _validate(self, value):
value = value.decode("utf-8")
except UnicodeError:
raise exceptions.BadValueError(
"Expected valid UTF-8, got {!r}".format(value)
"In field {}, expected valid UTF-8, got {!r}".format(
self._name, value
)
)
else:
raise exceptions.BadValueError(
"Expected string, got {!r}".format(value)
"In field {}, expected string, got {!r}".format(self._name, value)
)

def _to_base_type(self, value):
Expand Down Expand Up @@ -2920,7 +2932,9 @@ def _validate(self, value):
value = value.decode("utf-8")
except UnicodeError:
raise exceptions.BadValueError(
"Expected valid UTF-8, got {!r}".format(value)
"In field {}, expected valid UTF-8, got {!r}".format(
self._name, value
)
)
elif isinstance(value, six.string_types):
encoded_length = len(value.encode("utf-8"))
Expand Down Expand Up @@ -3026,7 +3040,9 @@ def _validate(self, value):
.BadValueError: If ``value`` is not a :attr:`.GeoPt`.
"""
if not isinstance(value, GeoPt):
raise exceptions.BadValueError("Expected GeoPt, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected GeoPt, got {!r}".format(self._name, value)
)


class PickleProperty(BlobProperty):
Expand Down Expand Up @@ -3447,7 +3463,9 @@ def _validate(self, value):
"""
# Might be GAE User or our own version
if type(value).__name__ != "User":
raise exceptions.BadValueError("Expected User, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected User, got {!r}".format(self._name, value)
)

def _prepare_for_put(self, entity):
"""Pre-put hook
Expand Down Expand Up @@ -3659,19 +3677,22 @@ def _validate(self, value):
and ``value`` does not match that kind.
"""
if not isinstance(value, Key):
raise exceptions.BadValueError("Expected Key, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected Key, got {!r}".format(self._name, value)
)

# Reject incomplete keys.
if not value.id():
raise exceptions.BadValueError(
"Expected complete Key, got {!r}".format(value)
"In field {}, expected complete Key, got {!r}".format(self._name, value)
)

# Verify kind if provided.
if self._kind is not None:
if value.kind() != self._kind:
raise exceptions.BadValueError(
"Expected Key with kind={!r}, got " "{!r}".format(self._kind, value)
"In field {}, expected Key with kind={!r}, got "
"{!r}".format(self._name, self._kind, value)
)

def _to_base_type(self, value):
Expand Down Expand Up @@ -3722,7 +3743,9 @@ def _validate(self, value):
:class:`~google.cloud.ndb.model.BlobKey`.
"""
if not isinstance(value, BlobKey):
raise exceptions.BadValueError("Expected BlobKey, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected BlobKey, got {!r}".format(self._name, value)
)


class DateTimeProperty(Property):
Expand Down Expand Up @@ -3838,7 +3861,9 @@ def _validate(self, value):
.BadValueError: If ``value`` is not a :class:`~datetime.datetime`.
"""
if not isinstance(value, datetime.datetime):
raise exceptions.BadValueError("Expected datetime, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected datetime, got {!r}".format(self._name, value)
)

if self._tzinfo is None and value.tzinfo is not None:
raise exceptions.BadValueError(
Expand Down Expand Up @@ -3935,7 +3960,9 @@ def _validate(self, value):
.BadValueError: If ``value`` is not a :class:`~datetime.date`.
"""
if not isinstance(value, datetime.date):
raise exceptions.BadValueError("Expected date, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected date, got {!r}".format(self._name, value)
)

def _to_base_type(self, value):
"""Convert a value to the "base" value type for this property.
Expand Down Expand Up @@ -3993,7 +4020,9 @@ def _validate(self, value):
.BadValueError: If ``value`` is not a :class:`~datetime.time`.
"""
if not isinstance(value, datetime.time):
raise exceptions.BadValueError("Expected time, got {!r}".format(value))
raise exceptions.BadValueError(
"In field {}, expected time, got {!r}".format(self._name, value)
)

def _to_base_type(self, value):
"""Convert a value to the "base" value type for this property.
Expand Down Expand Up @@ -4191,8 +4220,9 @@ def _validate(self, value):
return self._model_class(**value)
if not isinstance(value, self._model_class):
raise exceptions.BadValueError(
"Expected %s instance, got %s"
% (self._model_class.__name__, value.__class__)
"In field {}, expected {} instance, got {!r}".format(
self._name, self._model_class.__name__, value.__class__
)
)

def _has_value(self, entity, rest=None):
Expand Down Expand Up @@ -4399,7 +4429,9 @@ def _validate(self, value):

if not isinstance(value, self._model_class):
raise exceptions.BadValueError(
"Expected {}, got {!r}".format(self._model_class.__name__, value)
"In field {}, expected {}, got {!r}".format(
self._name, self._model_class.__name__, value
)
)

def _get_for_dict(self, entity):
Expand Down

0 comments on commit 17caf0b

Please sign in to comment.