Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ __pypackages__/
# Environments
venv/

# PyCharm
# IDEs
.idea/
.zed/

# PyPI configuration file
.pypirc
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 0.8.0

* Add schema for example object.

## Version 0.7.1

* Add schema for tags object.
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ clean:
rm -rf venv/
rm -rf dist/
rm -rf src/Schema_First.egg-info
find . -type d -name __pycache__ -exec rm -r {} \+

build: clean venv
$(PYTHON_VENV) -m build
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ license = {file = "LICENSE"}
name = "Schema-First"
readme = "README.md"
requires-python = ">=3.13"
version = "0.7.1"
version = "0.8.0"

[project.optional-dependencies]
dev = [
Expand Down
21 changes: 21 additions & 0 deletions src/schema_first/openapi/schemas/v3_1_1/example_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from marshmallow import fields
from marshmallow import validate
from marshmallow import validates_schema
from marshmallow import ValidationError

from ..base import BaseSchema
from ..base import DocStringFields
from .schema_object_schema import SchemaObjectSchema


class ExampleSchema(DocStringFields, BaseSchema):
dataValue = fields.Nested(SchemaObjectSchema)
serializedValue = fields.String(validate=validate.Length(min=1))
externalValue = fields.String(validate=validate.Length(min=1))

@validates_schema
def validate_exclusive(self, data, **kwargs) -> None:
if 'serializedValue' in data and 'externalValue' in data:
raise ValidationError(
'The <serializedValue> field is mutually exclusive of the <externalValue> field.'
)
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,4 @@ def load(
data, many=many, partial=partial, unknown=unknown
)
except KeyError:
raise ValidationError(f'Data type <{data["type"]}> not supported.')
raise ValidationError(f'Data type in <{data}> not exist.')
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
tests_dir_abspath = os.path.dirname(os.path.abspath(__file__))

pytest_plugins = (
'tests.objects.info_object.conftest',
'tests.objects.contact_object.conftest',
'tests.objects.example_object.conftest',
'tests.objects.external_docs_object.conftest',
'tests.objects.info_object.conftest',
'tests.objects.license_object.conftest',
'tests.objects.schema_object.conftest',
'tests.objects.schema_object.string_field.conftest',
Expand Down
17 changes: 17 additions & 0 deletions tests/objects/example_object/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest


@pytest.fixture
def fx_example_object_required() -> dict:
return {}


@pytest.fixture
def fx_example_object_full(fx_example_object_required, fx_schema_object__full) -> dict:
return {
**fx_example_object_required,
'summary': 'A work with an average rating of 4.5 stars',
'description': 'A work with an average rating of 4.5 stars',
'dataValue': fx_schema_object__full,
'serializedValue': '| {"author": "A. Writer", "title": "An Older Book", "rating": 4.5}',
}
19 changes: 19 additions & 0 deletions tests/objects/example_object/test_example_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from marshmallow.exceptions import ValidationError
import pytest

from src.schema_first.openapi.schemas.v3_1_1.example_schema import ExampleSchema


def test_example_schema_required(fx_example_object_required):
ExampleSchema().load(fx_example_object_required)


def test_example_schema_full(fx_example_object_full):
ExampleSchema().load(fx_example_object_full)


def test_example_schema_bad_kind(fx_example_object_full):
fx_example_object_full['kind'] = 'bad_kind'

with pytest.raises(ValidationError):
ExampleSchema().load(fx_example_object_full)