55from openapi_core .schema .schemas .models import Schema
66from openapi_core .schema_validator import OAS30Validator
77from openapi_core .schema_validator import oas30_format_checker
8+ from openapi_core .unmarshalling .schemas .enums import UnmarshalContext
89from openapi_core .unmarshalling .schemas .exceptions import (
910 FormatterNotFoundError ,
1011)
@@ -29,11 +30,17 @@ class SchemaUnmarshallersFactory(object):
2930 SchemaType .ANY : AnyUnmarshaller ,
3031 }
3132
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 ):
3339 self .resolver = resolver
3440 if custom_formatters is None :
3541 custom_formatters = {}
3642 self .custom_formatters = custom_formatters
43+ self .context = context
3744
3845 def create (self , schema , type_override = None ):
3946 """Create unmarshaller from the schema."""
@@ -50,7 +57,9 @@ def create(self, schema, type_override=None):
5057 elif schema_type in self .COMPLEX_UNMARSHALLERS :
5158 klass = self .COMPLEX_UNMARSHALLERS [schema_type ]
5259 kwargs = dict (
53- schema = schema , unmarshallers_factory = self )
60+ schema = schema , unmarshallers_factory = self ,
61+ context = self .context ,
62+ )
5463
5564 formatter = self .get_formatter (klass .FORMATTERS , schema .format )
5665
@@ -70,10 +79,17 @@ def get_formatter(self, default_formatters, type_format=SchemaFormat.NONE):
7079 return default_formatters .get (schema_format )
7180
7281 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 )
7493 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