-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvalidators.py
More file actions
184 lines (162 loc) · 6.09 KB
/
validators.py
File metadata and controls
184 lines (162 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from typing import Any
from typing import cast
from jsonschema import _keywords
from jsonschema import _legacy_keywords
from jsonschema.exceptions import SchemaError
from jsonschema.exceptions import ValidationError
from jsonschema.validators import Draft202012Validator
from jsonschema.validators import create
from jsonschema.validators import extend
from jsonschema.validators import validator_for
from openapi_schema_validator import _format as oas_format
from openapi_schema_validator import _keywords as oas_keywords
from openapi_schema_validator import _types as oas_types
from openapi_schema_validator._dialects import OAS31_BASE_DIALECT_ID
from openapi_schema_validator._dialects import OAS31_BASE_DIALECT_METASCHEMA
from openapi_schema_validator._dialects import OAS32_BASE_DIALECT_ID
from openapi_schema_validator._dialects import OAS32_BASE_DIALECT_METASCHEMA
from openapi_schema_validator._dialects import register_openapi_dialect
from openapi_schema_validator._specifications import (
REGISTRY as OPENAPI_SPECIFICATIONS,
)
from openapi_schema_validator._types import oas31_type_checker
_CHECK_SCHEMA_UNSET = object()
def check_openapi_schema(
cls: Any,
schema: Any,
format_checker: Any = _CHECK_SCHEMA_UNSET,
) -> None:
if format_checker is _CHECK_SCHEMA_UNSET:
format_checker = cls.FORMAT_CHECKER
validator_class = validator_for(cls.META_SCHEMA, default=cls)
validator_for_metaschema = validator_class(
cls.META_SCHEMA,
format_checker=format_checker,
registry=OPENAPI_SPECIFICATIONS,
)
for error in validator_for_metaschema.iter_errors(schema):
raise SchemaError.create_from(error)
def _oas30_id_of(schema: Any) -> str:
if isinstance(schema, dict):
return schema.get("id", "") # type: ignore[no-any-return]
return ""
OAS30_VALIDATORS = cast(
Any,
{
"multipleOf": _keywords.multipleOf,
# exclusiveMaximum supported inside maximum_draft3_draft4
"maximum": _legacy_keywords.maximum_draft3_draft4,
# exclusiveMinimum supported inside minimum_draft3_draft4
"minimum": _legacy_keywords.minimum_draft3_draft4,
"maxLength": _keywords.maxLength,
"minLength": _keywords.minLength,
"pattern": _keywords.pattern,
"maxItems": _keywords.maxItems,
"minItems": _keywords.minItems,
"uniqueItems": _keywords.uniqueItems,
"maxProperties": _keywords.maxProperties,
"minProperties": _keywords.minProperties,
"enum": _keywords.enum,
# adjusted to OAS
"type": oas_keywords.type,
"allOf": oas_keywords.allOf,
"oneOf": oas_keywords.oneOf,
"anyOf": oas_keywords.anyOf,
"not": _keywords.not_,
"items": oas_keywords.items,
"properties": _keywords.properties,
"required": oas_keywords.required,
"additionalProperties": oas_keywords.additionalProperties,
# TODO: adjust description
"format": oas_keywords.format,
# TODO: adjust default
"$ref": _keywords.ref,
# fixed OAS fields
"discriminator": oas_keywords.not_implemented,
"readOnly": oas_keywords.not_implemented,
"writeOnly": oas_keywords.not_implemented,
"xml": oas_keywords.not_implemented,
"externalDocs": oas_keywords.not_implemented,
"example": oas_keywords.not_implemented,
"deprecated": oas_keywords.not_implemented,
},
)
def _build_oas30_validator() -> Any:
return create(
meta_schema=OPENAPI_SPECIFICATIONS.contents(
"http://json-schema.org/draft-04/schema#",
),
validators=OAS30_VALIDATORS,
type_checker=oas_types.oas30_type_checker,
format_checker=oas_format.oas30_format_checker,
# NOTE: version causes conflict with global jsonschema validator
# See https://github.com/python-openapi/openapi-schema-validator/pull/12
# version="oas30",
id_of=_oas30_id_of,
)
def _build_oas31_validator() -> Any:
validator = extend(
Draft202012Validator,
{
# adjusted to OAS
"allOf": oas_keywords.allOf,
"oneOf": oas_keywords.oneOf,
"anyOf": oas_keywords.anyOf,
"description": oas_keywords.not_implemented,
# fixed OAS fields
"discriminator": oas_keywords.not_implemented,
"xml": oas_keywords.not_implemented,
"externalDocs": oas_keywords.not_implemented,
"example": oas_keywords.not_implemented,
},
type_checker=oas31_type_checker,
format_checker=oas_format.oas31_format_checker,
)
return register_openapi_dialect(
validator=validator,
dialect_id=OAS31_BASE_DIALECT_ID,
version_name="oas31",
metaschema=OAS31_BASE_DIALECT_METASCHEMA,
)
def _build_oas32_validator() -> Any:
validator = extend(
OAS31Validator,
{},
format_checker=oas_format.oas32_format_checker,
)
return register_openapi_dialect(
validator=validator,
dialect_id=OAS32_BASE_DIALECT_ID,
version_name="oas32",
metaschema=OAS32_BASE_DIALECT_METASCHEMA,
)
OAS30Validator = _build_oas30_validator()
OAS30StrictValidator = extend(
OAS30Validator,
validators={
"type": oas_keywords.strict_type,
},
type_checker=oas_types.oas30_type_checker,
format_checker=oas_format.oas30_strict_format_checker,
# NOTE: version causes conflict with global jsonschema validator
# See https://github.com/python-openapi/openapi-schema-validator/pull/12
# version="oas30-strict",
)
OAS30ReadValidator = extend(
OAS30Validator,
validators={
"required": oas_keywords.read_required,
"writeOnly": oas_keywords.read_writeOnly,
},
)
OAS30WriteValidator = extend(
OAS30Validator,
validators={
"required": oas_keywords.write_required,
"readOnly": oas_keywords.write_readOnly,
},
)
OAS31Validator = _build_oas31_validator()
OAS32Validator = _build_oas32_validator()
OAS31Validator.check_schema = classmethod(check_openapi_schema)
OAS32Validator.check_schema = classmethod(check_openapi_schema)