5
5
from openapi_core .schema .schemas .models import Schema
6
6
from openapi_core .schema_validator import OAS30Validator
7
7
from openapi_core .schema_validator import oas30_format_checker
8
+ from openapi_core .unmarshalling .schemas .enums import UnmarshalContext
8
9
from openapi_core .unmarshalling .schemas .exceptions import (
9
10
FormatterNotFoundError ,
10
11
)
@@ -29,11 +30,17 @@ class SchemaUnmarshallersFactory(object):
29
30
SchemaType .ANY : AnyUnmarshaller ,
30
31
}
31
32
32
- def __init__ (self , resolver = None , custom_formatters = None ):
33
+ CONTEXT_VALIDATION = {
34
+ UnmarshalContext .REQUEST : 'write' ,
35
+ UnmarshalContext .RESPONSE : 'read' ,
36
+ }
37
+
38
+ def __init__ (self , resolver = None , custom_formatters = None , context = None ):
33
39
self .resolver = resolver
34
40
if custom_formatters is None :
35
41
custom_formatters = {}
36
42
self .custom_formatters = custom_formatters
43
+ self .context = context
37
44
38
45
def create (self , schema , type_override = None ):
39
46
"""Create unmarshaller from the schema."""
@@ -50,7 +57,9 @@ def create(self, schema, type_override=None):
50
57
elif schema_type in self .COMPLEX_UNMARSHALLERS :
51
58
klass = self .COMPLEX_UNMARSHALLERS [schema_type ]
52
59
kwargs = dict (
53
- schema = schema , unmarshallers_factory = self )
60
+ schema = schema , unmarshallers_factory = self ,
61
+ context = self .context ,
62
+ )
54
63
55
64
formatter = self .get_formatter (klass .FORMATTERS , schema .format )
56
65
@@ -70,10 +79,17 @@ def get_formatter(self, default_formatters, type_format=SchemaFormat.NONE):
70
79
return default_formatters .get (schema_format )
71
80
72
81
def get_validator (self , schema ):
73
- format_checker = deepcopy (oas30_format_checker )
82
+ format_checker = self ._get_format_checker ()
83
+ kwargs = {
84
+ 'resolver' : self .resolver ,
85
+ 'format_checker' : format_checker ,
86
+ }
87
+ if self .context is not None :
88
+ kwargs [self .CONTEXT_VALIDATION [self .context ]] = True
89
+ return OAS30Validator (schema .__dict__ , ** kwargs )
90
+
91
+ def _get_format_checker (self ):
92
+ fc = deepcopy (oas30_format_checker )
74
93
for name , formatter in self .custom_formatters .items ():
75
- format_checker .checks (name )(formatter .validate )
76
- return OAS30Validator (
77
- schema .__dict__ ,
78
- resolver = self .resolver , format_checker = format_checker ,
79
- )
94
+ fc .checks (name )(formatter .validate )
95
+ return fc
0 commit comments