Skip to content

Commit

Permalink
Merge pull request #25 from olxbr/feature/dont-try-parse-body
Browse files Browse the repository at this point in the history
Using vanilla json schema and removing body
  • Loading branch information
dmvieira authored May 14, 2020
2 parents a840d12 + 090c47f commit 8bfe281
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async def consumer_access_storage(msg):

### Schema Validation

Consumed messages can be validated by json schema draft-04:
Consumed messages can be validated by json schema:

```python
@barterdude.consume_amqp(
Expand Down
29 changes: 12 additions & 17 deletions barterdude/message.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import jsonschema
from functools import partial
from typing import Optional, Union
from python_jsonschema_objects.validators import ValidationError
from python_jsonschema_objects import ObjectBuilder
from asyncworker.rabbitmq.message import RabbitMQMessage


class ValidationException(ValidationError):
class ValidationException(jsonschema.ValidationError):
pass


class MessageBody(dict):
def __init__(self, **entries):
self.__dict__.update(entries)
super(MessageBody, self).__init__(**entries)


class Message:
def __init__(self, message: RabbitMQMessage):
self._message = message
if type(message.body) == dict:
self._body = MessageBody(**message.body)
else:
self._body = message.body

@property
def body(self):
return self._body
return self._message.body

@property
def raw(self):
Expand Down Expand Up @@ -54,13 +44,18 @@ async def process_exception(self):
class MessageValidation:
def __init__(self, validation_schema: Optional[dict] = {}):
self._validate = bool(validation_schema)
self._builder = ObjectBuilder(validation_schema)
resolver = jsonschema.RefResolver.from_schema(validation_schema)
self._builder = partial(
jsonschema.validate,
schema=validation_schema,
resolver=resolver
)

def validate(self, message: Union[RabbitMQMessage, Message]):
if self._validate:
try:
self._builder.validate(message.body)
except ValidationError as err:
self._builder(message.body)
except jsonschema.ValidationError as err:
raise ValidationException(err)

def __call__(self, message: RabbitMQMessage):
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements_base.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async-worker==0.14.0
aioamqp==0.14.0
python-json-logger==0.1.11
python-jsonschema-objects==0.3.12
jsonschema==3.2.0
2 changes: 1 addition & 1 deletion tests_unit/test__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def test_should_call_callback_for_valid_message(self):
await wrapper([message])
self.callback.assert_called_once()
self.assertEqual(
self.callback.await_args[0][0].body.key,
self.callback.await_args[0][0].body["key"],
message.body["key"]
)

Expand Down
14 changes: 1 addition & 13 deletions tests_unit/test_message.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from asynctest import TestCase, Mock
from asyncworker.rabbitmq.message import RabbitMQMessage
from barterdude.message import (
Message, MessageBody, MessageValidation, ValidationException)
Message, MessageValidation, ValidationException)
from tests_unit.helpers import load_fixture


Expand Down Expand Up @@ -49,18 +49,6 @@ def test_should_work_with_no_dict(self):
self.assertEqual(message.body, test_message)


class TestMessageBody(TestCase):
def test_should_convert_to_class(self):
test_message = {"key": "value"}
message = MessageBody(**test_message)
self.assertEqual(test_message["key"], message.key)

def test_should_persist_dict_notation(self):
test_message = {"key": "value"}
message = MessageBody(**test_message)
self.assertEqual(message["key"], message.key)


class TestMessageValidation(TestCase):
def setUp(self):
self.schema = load_fixture("schema.json")
Expand Down

0 comments on commit 8bfe281

Please sign in to comment.