-
Notifications
You must be signed in to change notification settings - Fork 67
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
api: addition of FormatChecker support #144
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,10 +75,35 @@ def updated(self): | |
"""Get last updated timestamp.""" | ||
return self.model.updated if self.model else None | ||
|
||
def validate(self): | ||
"""Validate record according to schema defined in ``$schema`` key.""" | ||
def validate(self, **kwargs): | ||
"""Validate record according to schema defined in ``$schema`` key. | ||
|
||
This method accepts arbitrary kwargs to pass the ``format_checker`` | ||
keyword argument to the underlying JSON Schema implementation. | ||
|
||
A ``format_checker`` is an instance of class | ||
:class:`jsonschema.FormatChecker` containing business logic to validate | ||
arbitrary formats. For example: | ||
|
||
>>> from jsonschema import FormatChecker | ||
>>> from jsonschema.validators import validate | ||
>>> checker = FormatChecker() | ||
>>> checker.checks('foo')(lambda el: el.startswith('foo')) | ||
<function <lambda> at ...> | ||
>>> validate('foo', {'format': 'foo'}, format_checker=checker) | ||
|
||
returns ``None``, which means that the validation was successful, while | ||
|
||
>>> validate('bar', {'format': 'foo'}, format_checker=checker) | ||
Traceback (most recent call last): | ||
... | ||
ValidationError: 'bar' is not a 'foo' | ||
... | ||
|
||
raises a :class:`jsonschema.exceptions.ValidationError`. | ||
""" | ||
if '$schema' in self and self['$schema'] is not None: | ||
return _records_state.validate(self, self['$schema']) | ||
return _records_state.validate(self, self['$schema'], **kwargs) | ||
return True | ||
|
||
def replace_refs(self): | ||
|
@@ -94,8 +119,8 @@ class Record(RecordBase): | |
"""Define API for metadata creation and manipulation.""" | ||
|
||
@classmethod | ||
def create(cls, data, id_=None): | ||
"""Create a record instance and store it in database. | ||
def create(cls, data, id_=None, **kwargs): | ||
r"""Create a record instance and store it in database. | ||
|
||
Procedure followed: | ||
|
||
|
@@ -111,15 +136,22 @@ def create(cls, data, id_=None): | |
|
||
:param data: Dict with record metadata. | ||
:param id_: Force the UUID for the record. | ||
:param \**kwargs: See below. | ||
:returns: A new Record instance. | ||
|
||
:Keyword Arguments: | ||
* **format_checker** -- | ||
An instance of class :class:`jsonschema.FormatChecker`, which | ||
contains validation rules for formats. See | ||
:func:`~invenio_records.api.RecordBase.validate` for details. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This style of documenting kwargs is lifted from http://stackoverflow.com/a/27724915/374865. |
||
""" | ||
from .models import RecordMetadata | ||
with db.session.begin_nested(): | ||
record = cls(data) | ||
|
||
before_record_insert.send(record) | ||
|
||
record.validate() | ||
record.validate(**kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit worried about future compatibility here. Shall we open wider discussion if we should create e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I forgot about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jacquerie In order to move forward, we can keep the |
||
|
||
record.model = RecordMetadata(id=id_, json=record) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checker returns not None, hence you need to add
<function <lambda> at ...>
on next line