-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexey Nikitenko
committed
Mar 9, 2021
1 parent
edef993
commit a0d4396
Showing
3 changed files
with
32 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters