Skip to content
Open
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
12 changes: 12 additions & 0 deletions openapi_schema_pydantic/v3/v3_0_3/datatype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import enum


class DataType(str, enum.Enum):
"""Data type of an object."""

STRING = "string"
NUMBER = "number"
INTEGER = "integer"
BOOLEAN = "boolean"
ARRAY = "array"
OBJECT = "object"
4 changes: 3 additions & 1 deletion openapi_schema_pydantic/v3/v3_0_3/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Any, Dict, List, Optional, Union

from pydantic import BaseModel, Extra, Field

from .datatype import DataType
from .discriminator import Discriminator
from .external_documentation import ExternalDocumentation
from .reference import Reference
Expand Down Expand Up @@ -206,7 +208,7 @@ class Schema(BaseModel):
but their definitions were adjusted to the OpenAPI Specification.
"""

type: Optional[str] = None
type: Optional[DataType] = None
"""
**From OpenAPI spec:
Value MUST be a string. Multiple types via an array are not supported.**
Expand Down
13 changes: 13 additions & 0 deletions openapi_schema_pydantic/v3/v3_1_0/datatype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from enum import Enum


class DataType(str, Enum):
"""Data type of an object."""

NULL = "null"
STRING = "string"
NUMBER = "number"
INTEGER = "integer"
BOOLEAN = "boolean"
ARRAY = "array"
OBJECT = "object"
3 changes: 2 additions & 1 deletion openapi_schema_pydantic/v3/v3_1_0/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Dict, List, Optional, Union

from pydantic import BaseModel, Extra, Field
from .datatype import DataType
from .discriminator import Discriminator
from .external_documentation import ExternalDocumentation
from .reference import Reference
Expand Down Expand Up @@ -358,7 +359,7 @@ class Schema(BaseModel):
and follow the same specifications:
"""

type: Optional[Union[str, List[str]]] = None
type: Optional[Union[DataType, List[DataType]]] = None
"""
The value of this keyword MUST be either a string or an array. If it
is an array, elements of the array MUST be strings and MUST be
Expand Down
34 changes: 34 additions & 0 deletions tests/v3_0_3/test_datatype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from pydantic import ValidationError

from openapi_schema_pydantic.v3.v3_0_3 import Schema


@pytest.mark.parametrize(
"datatype",
(
"string",
"number",
"integer",
"boolean",
"array",
"object",
),
)
def test_good_types_parse_and_equate(datatype: str):
assert Schema(type=datatype).type == datatype


def test_bad_types_raise_validation_errors():
with pytest.raises(ValidationError):
Schema(type="invalid")

with pytest.raises(ValidationError):
Schema(anyOf=[{"type": "invalid"}])

with pytest.raises(ValidationError):
Schema(
properties={
"a": Schema(type="invalid"),
},
)
35 changes: 35 additions & 0 deletions tests/v3_1_0/test_datatype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest
from pydantic import ValidationError

from openapi_schema_pydantic.v3.v3_1_0 import Schema


@pytest.mark.parametrize(
"datatype",
(
"string",
"number",
"integer",
"boolean",
"array",
"object",
"null",
),
)
def test_good_types_parse_and_equate(datatype: str):
assert Schema(type=datatype).type == datatype


def test_bad_types_raise_validation_errors():
with pytest.raises(ValidationError):
Schema(type="invalid")

with pytest.raises(ValidationError):
Schema(anyOf=[{"type": "invalid"}])

with pytest.raises(ValidationError):
Schema(
properties={
"a": Schema(type="invalid"),
},
)