New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Cerberus 1.1 #1001

Closed
wants to merge 1 commit into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+271 −777
Diff settings

Always

Just for now

Support for Cerberus 1.1

(Based on original patch from dkellner)

This is a rather big change. I still decided to do a single commit, as
intermediate commits would be in a non-working state anyway.

Breaking changes:

- `keyschema` was renamed to `valueschema` and `propertyschema` to
  `keyschema` (following changes in Cerberus).
- A PATCH on a document which misses a field having a default value will
  now result in setting this value, even if the field was not provided
  in the PATCH's payload.
- Error messages for `keyschema` are now returned as dictionary.
  Before: {'propertyschema_dict': 'propertyschema_dict'}
  Now: {'keyschema_dict': {'AAA': "value does not match regex '[a-z]+'"}}
- Error messages for `type` validations are different now (following
  changes in Cerberus).
- It is no longer valid to have a field with `default` = None and
  `nullable` = False.
  (see patch.py:test_patch_nested_document_nullable_missing)

In a nutshell, changes to the codebase are as follows:

- Add data layer independent subclass of `cerberus.Validator`
  * Support new signature of `__init__` and `validate`
  * Use `_config`-aware properties instead of bare member attributes to
    pass the `resource`, `document_id` and `persisted_document` to make
    them available to child validators
  * Add schema-docstrings to all `_validate_*` methods

- Adjust Mongo-specific `Validator` subclass
  * Adjust `_validate_type_*` methods (following changes in Cerberus)
  * Add schema-docstrings to all `_validate_*` methods

- Add custom ErrorHandler to support `VALIDATION_ERROR_AS_LIST`

- A few renames:
  * `ValidationError` -> `DocumentError`
  * `propertyschema` -> `keyschema` and `keyschema` -> `valueschema`

- Adjust tests due to different validation error messages
  (mostly for `type`)

- Remove `transparent_schema_rules` without replacement
- Remove `default`-handling, as Cerberus takes care of this now
  • Loading branch information...
dkellner authored and bcrochet committed Sep 26, 2016
commit 1110f807b478efa9f13ad1d217d22ceaa2a9e42d
View
14 CHANGES
@@ -29,6 +29,20 @@ Breaking Changes
``MONGO_CONNECT_TIMEOUT_MS``, ``MONGO_REPLICA_SET``,
``MONGO_READ_PREFERENCE`` removed. Use ``MONGO_OPTIONS`` or ``MONGO_URI``
instead.
- `keyschema` was renamed to `valueschema` and `propertyschema` to
`keyschema` (following changes in Cerberus).
- A PATCH on a document which misses a field having a default value will
now result in setting this value, even if the field was not provided
in the PATCH's payload.
- Error messages for `keyschema` are now returned as dictionary.
Before: {'propertyschema_dict': 'propertyschema_dict'}
Now: {'keyschema_dict': {'AAA': "value does not match regex '[a-z]+'"}}
- Error messages for `type` validations are different now (following
changes in Cerberus).
- It is no longer valid to have a field with `default` = None and
`nullable` = False.
(see patch.py:test_patch_nested_document_nullable_missing)
- See also: `Cerberus changes <https://cerberus.readthedocs.io/en/stable/upgrading.html>_`
Stable
------
View
@@ -370,9 +370,6 @@ uppercase.
:ref:`unknown` for more information.
Defaults to ``False``.
``TRANSPARENT_SCHEMA_RULES`` When ``True``, this option globally disables
:ref:`schema_validation` for any API endpoint.
``PROJECTION`` When ``True``, this option enables the
:ref:`projections` feature. Can be
overridden by resource settings. Defaults
@@ -1302,11 +1299,10 @@ defining the field validation rules. Allowed validation rules are:
for all of which must validate with given
schema. See `valueschema example <http://docs.python-cerberus.org/en/latest/usage.html#valueschema>`_.
``propertyschema`` This is the counterpart to ``valueschema`` that
``keyschema`` This is the counterpart to ``valueschema`` that
validates the keys of a dict. Validation
schema for all values of a ``dict``. See
`propertyschema example
<http://docs.python-cerberus.org/en/latest/usage.html#propertyschema>`_.
`keyschema example <http://docs.python-cerberus.org/en/latest/usage.html#keyschema>`_.
``regex`` Validation will fail if field value does not
@@ -82,13 +82,11 @@ details on custom validation):
"""
Extends the base mongo validator adding support for the uuid data-type
"""
def _validate_type_uuid(self, field, value):
def _validate_type_uuid(self, value):
try:
UUID(value)
except ValueError:
self._error(field, "value '%s' cannot be converted to a UUID" %
value)
pass
``UUID`` URLs
~~~~~~~~~~~~~
View
@@ -97,16 +97,13 @@ code.
.. code-block:: python
def _validate_type_objectid(self, field, value):
def _validate_type_objectid(self, value):
""" Enables validation for `objectid` schema attribute.
:param unique: Boolean, whether the field value should be
unique or not.
:param field: field name.
:param value: field value.
"""
if not re.match('[a-f0-9]{24}', value):
self._error(field, ERROR_BAD_TYPE % 'ObjectId')
if isinstance(value, ObjectId):
return True
This method enables support for MongoDB ``ObjectId`` type in your schema,
allowing something like this:
@@ -194,14 +191,8 @@ Schema validation
By default, schemas are validated to ensure they conform to the structure
documented in :ref:`schema`.
There are two ways to deal with non-conforming schemas:
1. Add :ref:`custom_validation_rules` for non-conforming keys used in the
schema.
2. Set the global option ``TRANSPARENT_SCHEMA_RULES`` to disable schema
validation globally or the resource option ``transparent_schema_rules``
to disable schema validation for a given endpoint.
In order to deal with non-conforming schemas, add
:ref:`custom_validation_rules` for non-conforming keys used in the schema.
.. _Cerberus: http://python-cerberus.org
.. _`source code`: https://github.com/pyeve/eve/blob/master/eve/io/mongo/validation.py
View
@@ -238,9 +238,6 @@
# http://geojson.org/geojson-spec.html#geojson-objects
ALLOW_CUSTOM_FIELDS_IN_GEOJSON = False
# don't ignore unknown schema rules (raise SchemaError)
TRANSPARENT_SCHEMA_RULES = False
# Rate limits are disabled by default. Needs a running redis-server.
RATE_LIMIT_GET = None
RATE_LIMIT_POST = None
View

This file was deleted.

Oops, something went wrong.
View
@@ -20,7 +20,6 @@
from werkzeug.serving import WSGIRequestHandler
import eve
from eve.defaults import build_defaults
from eve.endpoints import collections_endpoint, item_endpoint, home_endpoint, \
error_endpoint, media_endpoint, schema_collection_endpoint, \
schema_item_endpoint
@@ -605,8 +604,6 @@ def _set_resource_defaults(self, resource, settings):
settings.setdefault('auth_field',
self.config['AUTH_FIELD'])
settings.setdefault('allow_unknown', self.config['ALLOW_UNKNOWN'])
settings.setdefault('transparent_schema_rules',
self.config['TRANSPARENT_SCHEMA_RULES'])
settings.setdefault('extra_response_fields',
self.config['EXTRA_RESPONSE_FIELDS'])
settings.setdefault('mongo_write_concern',
@@ -619,12 +616,6 @@ def _set_resource_defaults(self, resource, settings):
schema = settings.setdefault('schema', {})
self.set_schema_defaults(schema, settings['id_field'])
# 'defaults' helper set contains the names of fields with default
# values in their schema definition.
# TODO support default values for embedded documents.
settings['defaults'] = build_defaults(schema)
# list of all media fields for the resource
settings['_media'] = [field for field, definition in schema.items() if
definition.get('type') == 'media']
@@ -705,12 +696,6 @@ def _set_resource_projection(self, ds, schema, settings):
ds['projection'] is not None:
ds['projection'][self.config['DELETED']] = 1
# 'defaults' helper set contains the names of fields with default
# values in their schema definition.
# TODO support default values for embedded documents.
settings['defaults'] = build_defaults(schema)
# list of all media fields for the resource
settings['_media'] = [field for field, definition in schema.items() if
definition.get('type') == 'media' or
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.