Skip to content

Commit 836e406

Browse files
authored
Merge pull request #1 from mike-oakley/datatype-enum
Add datatype enum.
2 parents 6b7cc75 + 19c0a39 commit 836e406

File tree

6 files changed

+99
-2
lines changed

6 files changed

+99
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import enum
2+
3+
4+
class DataType(str, enum.Enum):
5+
"""Data type of an object."""
6+
7+
STRING = "string"
8+
NUMBER = "number"
9+
INTEGER = "integer"
10+
BOOLEAN = "boolean"
11+
ARRAY = "array"
12+
OBJECT = "object"

openapi_schema_pydantic/v3/v3_0_3/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import Any, Dict, List, Optional, Union
22

33
from pydantic import BaseModel, Extra, Field
4+
5+
from .datatype import DataType
46
from .discriminator import Discriminator
57
from .external_documentation import ExternalDocumentation
68
from .reference import Reference
@@ -206,7 +208,7 @@ class Schema(BaseModel):
206208
but their definitions were adjusted to the OpenAPI Specification.
207209
"""
208210

209-
type: Optional[str] = None
211+
type: Optional[DataType] = None
210212
"""
211213
**From OpenAPI spec:
212214
Value MUST be a string. Multiple types via an array are not supported.**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from enum import Enum
2+
3+
4+
class DataType(str, Enum):
5+
"""Data type of an object."""
6+
7+
NULL = "null"
8+
STRING = "string"
9+
NUMBER = "number"
10+
INTEGER = "integer"
11+
BOOLEAN = "boolean"
12+
ARRAY = "array"
13+
OBJECT = "object"

openapi_schema_pydantic/v3/v3_1_0/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Any, Dict, List, Optional, Union
22

33
from pydantic import BaseModel, Extra, Field
4+
from .datatype import DataType
45
from .discriminator import Discriminator
56
from .external_documentation import ExternalDocumentation
67
from .reference import Reference
@@ -358,7 +359,7 @@ class Schema(BaseModel):
358359
and follow the same specifications:
359360
"""
360361

361-
type: Optional[Union[str, List[str]]] = None
362+
type: Optional[Union[DataType, List[DataType]]] = None
362363
"""
363364
The value of this keyword MUST be either a string or an array. If it
364365
is an array, elements of the array MUST be strings and MUST be

tests/v3_0_3/test_datatype.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
from pydantic import ValidationError
3+
4+
from openapi_schema_pydantic.v3.v3_0_3 import Schema
5+
6+
7+
@pytest.mark.parametrize(
8+
"datatype",
9+
(
10+
"string",
11+
"number",
12+
"integer",
13+
"boolean",
14+
"array",
15+
"object",
16+
),
17+
)
18+
def test_good_types_parse_and_equate(datatype: str):
19+
assert Schema(type=datatype).type == datatype
20+
21+
22+
def test_bad_types_raise_validation_errors():
23+
with pytest.raises(ValidationError):
24+
Schema(type="invalid")
25+
26+
with pytest.raises(ValidationError):
27+
Schema(anyOf=[{"type": "invalid"}])
28+
29+
with pytest.raises(ValidationError):
30+
Schema(
31+
properties={
32+
"a": Schema(type="invalid"),
33+
},
34+
)

tests/v3_1_0/test_datatype.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
from pydantic import ValidationError
3+
4+
from openapi_schema_pydantic.v3.v3_1_0 import Schema
5+
6+
7+
@pytest.mark.parametrize(
8+
"datatype",
9+
(
10+
"string",
11+
"number",
12+
"integer",
13+
"boolean",
14+
"array",
15+
"object",
16+
"null",
17+
),
18+
)
19+
def test_good_types_parse_and_equate(datatype: str):
20+
assert Schema(type=datatype).type == datatype
21+
22+
23+
def test_bad_types_raise_validation_errors():
24+
with pytest.raises(ValidationError):
25+
Schema(type="invalid")
26+
27+
with pytest.raises(ValidationError):
28+
Schema(anyOf=[{"type": "invalid"}])
29+
30+
with pytest.raises(ValidationError):
31+
Schema(
32+
properties={
33+
"a": Schema(type="invalid"),
34+
},
35+
)

0 commit comments

Comments
 (0)