88
99from django .urls import resolve
1010import openapi_core
11- from openapi_core .unmarshalling .schemas .factories import (
12- SchemaUnmarshallersFactory ,
13- )
1411from openapi_core .contrib .django import DjangoOpenAPIRequest
1512from openapi_core .contrib .django import DjangoOpenAPIResponse
1613from openapi_core .exceptions import OpenAPIError
1714from openapi_core .templating import util
18- from openapi_core .unmarshalling .schemas .formatters import Formatter
19- from openapi_core .validation .request .validators import RequestValidator
20- from openapi_core .validation .response .validators import ResponseValidator
21- from openapi_schema_validator import OAS31Validator
15+ from openapi_core import shortcuts
2216from rest_framework import status
2317import yaml
2418
@@ -64,26 +58,17 @@ def __call__(self, value):
6458 return self .regex .match (value )
6559
6660
67- CUSTOM_FORMATTERS = {
68- 'uri' : Formatter .from_callables (
69- RegexValidator (
70- r'^(?:http|ftp)s?://'
71- r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # noqa: E501
72- r'localhost|'
73- r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
74- r'(?::\d+)?'
75- r'(?:/?|[/?]\S+)$' ,
76- ),
77- str ,
78- ),
79- 'iso8601' : Formatter .from_callables (
80- RegexValidator (r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{6}$' ),
81- str ,
82- ),
83- 'email' : Formatter .from_callables (
84- RegexValidator (r'[^@]+@[^@]+\.[^@]+' ),
85- str ,
61+ EXTRA_FORMAT_VALIDATORS = {
62+ 'uri' : RegexValidator (
63+ r'^(?:http|ftp)s?://'
64+ r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # noqa: E501
65+ r'localhost|'
66+ r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
67+ r'(?::\d+)?'
68+ r'(?:/?|[/?]\S+)$' ,
8669 ),
70+ 'iso8601' : RegexValidator (r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{6}$' ),
71+ 'email' : RegexValidator (r'[^@]+@[^@]+\.[^@]+' ),
8772}
8873
8974
@@ -102,13 +87,17 @@ def _load_spec(version):
10287 with open (spec_path , 'r' ) as fh :
10388 data = yaml .load (fh , Loader = yaml .SafeLoader )
10489
105- _LOADED_SPECS [version ] = openapi_core .Spec .create (data )
90+ _LOADED_SPECS [version ] = openapi_core .Spec .from_dict (data )
10691
10792 return _LOADED_SPECS [version ]
10893
10994
11095def validate_data (
111- path , request , response , validate_request , validate_response
96+ path ,
97+ request ,
98+ response ,
99+ validate_request ,
100+ validate_response ,
112101):
113102 if response .status_code == status .HTTP_405_METHOD_NOT_ALLOWED :
114103 return
@@ -117,25 +106,24 @@ def validate_data(
117106 request = DjangoOpenAPIRequest (request )
118107 response = DjangoOpenAPIResponse (response )
119108
120- schema_unmarshallers_factory = SchemaUnmarshallersFactory (
121- OAS31Validator ,
122- custom_formatters = CUSTOM_FORMATTERS ,
123- # context=UnmarshalContext.RESPONSE,
124- )
125-
126109 # request
127110 if validate_request :
128- validator = RequestValidator (schema_unmarshallers_factory )
129- result = validator .validate (spec , request )
130111 try :
131- result .raise_for_errors ()
112+ shortcuts .validate_request (
113+ request ,
114+ spec = spec ,
115+ extra_format_validators = EXTRA_FORMAT_VALIDATORS ,
116+ )
132117 except OpenAPIError :
133118 # TODO(stephenfin): In API v2.0, this should be an error. As things
134119 # stand, we silently ignore these issues.
135120 assert response .status_code == status .HTTP_200_OK
136121
137122 # response
138123 if validate_response :
139- validator = ResponseValidator (schema_unmarshallers_factory )
140- result = validator .validate (spec , request , response )
141- result .raise_for_errors ()
124+ shortcuts .validate_response (
125+ request ,
126+ response ,
127+ spec = spec ,
128+ extra_format_validators = EXTRA_FORMAT_VALIDATORS ,
129+ )
0 commit comments