-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvalidators.py
More file actions
123 lines (112 loc) · 4.09 KB
/
validators.py
File metadata and controls
123 lines (112 loc) · 4.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
from typing import Any
from typing import cast
from jsonschema import _keywords
from jsonschema import _legacy_keywords
from jsonschema.exceptions import ValidationError
from jsonschema.validators import Draft202012Validator
from jsonschema.validators import create
from jsonschema.validators import extend
from jsonschema_specifications import REGISTRY as SPECIFICATIONS
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._types import oas31_type_checker
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,
},
)
OAS30Validator = create(
meta_schema=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,
)
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 = 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,
)