Skip to content

Commit

Permalink
Support for Cerberus 1.0 (work in progress)
Browse files Browse the repository at this point in the history
"Work in progress" because we still have to wait for the Cerberus issue
"`readonly` conflicts with `default`" to be resolved.
(see pyeve/cerberus#268)

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 committed Sep 26, 2016
1 parent 910b3c0 commit f0f3441
Show file tree
Hide file tree
Showing 20 changed files with 237 additions and 740 deletions.
3 changes: 0 additions & 3 deletions eve/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@
# don't allow unknown key/value pairs for POST/PATCH payloads.
ALLOW_UNKNOWN = 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
Expand Down
119 changes: 0 additions & 119 deletions eve/defaults.py

This file was deleted.

15 changes: 0 additions & 15 deletions eve/flaskapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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
Expand Down Expand Up @@ -586,8 +585,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',
Expand All @@ -600,12 +597,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']
Expand Down Expand Up @@ -686,12 +677,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']
Expand Down
Loading

0 comments on commit f0f3441

Please sign in to comment.