Skip to content

Commit

Permalink
Black style, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Nikitenko committed Mar 9, 2021
1 parent edef993 commit a0d4396
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# django-json-schema-validator
[![Build Status](https://travis-ci.org/wblxyxolbkhv/django-json-schema-validator.svg?branch=main)](https://travis-ci.org/wblxyxolbkhv/django-json-schema-validator)
# django-json-field-schema-validator
[![Build Status](https://travis-ci.com/wblxyxolbkhv/django-json-schema-validator.svg?branch=main)](https://travis-ci.com/wblxyxolbkhv/django-json-schema-validator)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Tiny tool for Django JSONField validation through [JSON Schema](https://python-jsonschema.readthedocs.io/en/latest/validate/)
## Installation

```shell script
pip install django-json-field-schema-validator
```

## Example

Expand Down
50 changes: 23 additions & 27 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,49 @@
import pytest
from django.core.exceptions import ValidationError

from django_json_field_schema_validator.validators import JSONFieldSchemaValidator
from django_json_field_schema_validator.validators import (
JSONFieldSchemaValidator,
)


@pytest.mark.parametrize('draft_version', (3, 4, 6, 7))
@pytest.mark.parametrize("draft_version", (3, 4, 6, 7))
def test_validation_without_errors(draft_version):
schema = {
'$schema': f'http://json-schema.org/draft-{draft_version:02d}/schema#',
'type': 'object',
'properties': {
'id': {'type': 'number'},
'name': {'type': 'string'}
},
'required': ['id', 'name']
"$schema": f"http://json-schema.org/draft-{draft_version:02d}/schema#",
"type": "object",
"properties": {"id": {"type": "number"}, "name": {"type": "string"}},
"required": ["id", "name"],
}
validator = JSONFieldSchemaValidator(schema, draft_version)
validator({'id': 1, 'name': 'Foo'})
validator({"id": 1, "name": "Foo"})


def test_validation_with_errors():
schema = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'properties': {
'id': {'type': 'number'},
'name': {'type': 'string'}
},
'required': ['id', 'name']
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {"id": {"type": "number"}, "name": {"type": "string"}},
"required": ["id", "name"],
}
validator = JSONFieldSchemaValidator(schema)
with pytest.raises(ValidationError):
validator({'id': 1})
validator({"id": 1})


def test_validation_with_context():
schema = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'properties': {
'items': {
'anyOf': [
{'type': 'string', 'maxLength': 2},
{'type': 'integer', 'minimum': 5}
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"items": {
"anyOf": [
{"type": "string", "maxLength": 2},
{"type": "integer", "minimum": 5},
]
}
},
'required': ['items']
"required": ["items"],
}
validator = JSONFieldSchemaValidator(schema)
with pytest.raises(ValidationError):
validator({'items': [{}, 3, 'foo']})
validator({"items": [{}, 3, "foo"]})
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ deps =
pytest-cov==2.11.1
coveralls==3.0.1
[testenv:pytest-unit]
commands = coverage run -m pytest py.test --cov=django_json_field_schema_validator
commands = py.test --cov=django_json_field_schema_validator


[testenv:flake8]
Expand Down

0 comments on commit a0d4396

Please sign in to comment.