|
7 | 7 | from jsonschema.validators import Draft202012Validator |
8 | 8 | from jsonschema.validators import create |
9 | 9 | from jsonschema.validators import extend |
10 | | -from jsonschema_specifications import REGISTRY as SPECIFICATIONS |
11 | 10 |
|
12 | 11 | from openapi_schema_validator import _format as oas_format |
13 | 12 | from openapi_schema_validator import _keywords as oas_keywords |
14 | 13 | from openapi_schema_validator import _types as oas_types |
| 14 | +from openapi_schema_validator._dialects import JSONSCHEMA_SPECIFICATIONS |
| 15 | +from openapi_schema_validator._dialects import ( |
| 16 | + OAS31_BASE_DIALECT_ID as _OAS31_BASE_DIALECT_ID, |
| 17 | +) |
| 18 | +from openapi_schema_validator._dialects import OAS31_BASE_DIALECT_METASCHEMA |
| 19 | +from openapi_schema_validator._dialects import register_openapi_dialect |
15 | 20 | from openapi_schema_validator._types import oas31_type_checker |
16 | 21 |
|
| 22 | +OAS31_BASE_DIALECT_ID = _OAS31_BASE_DIALECT_ID |
| 23 | + |
17 | 24 |
|
18 | 25 | def _oas30_id_of(schema: Any) -> str: |
19 | 26 | if isinstance(schema, dict): |
@@ -63,64 +70,88 @@ def _oas30_id_of(schema: Any) -> str: |
63 | 70 | }, |
64 | 71 | ) |
65 | 72 |
|
66 | | -OAS30Validator = create( |
67 | | - meta_schema=SPECIFICATIONS.contents( |
68 | | - "http://json-schema.org/draft-04/schema#", |
69 | | - ), |
70 | | - validators=OAS30_VALIDATORS, |
71 | | - type_checker=oas_types.oas30_type_checker, |
72 | | - format_checker=oas_format.oas30_format_checker, |
73 | | - # NOTE: version causes conflict with global jsonschema validator |
74 | | - # See https://github.com/python-openapi/openapi-schema-validator/pull/12 |
75 | | - # version="oas30", |
76 | | - id_of=_oas30_id_of, |
77 | | -) |
78 | 73 |
|
79 | | -OAS30StrictValidator = extend( |
80 | | - OAS30Validator, |
81 | | - validators={ |
82 | | - "type": oas_keywords.strict_type, |
83 | | - }, |
84 | | - type_checker=oas_types.oas30_type_checker, |
85 | | - format_checker=oas_format.oas30_strict_format_checker, |
86 | | - # NOTE: version causes conflict with global jsonschema validator |
87 | | - # See https://github.com/python-openapi/openapi-schema-validator/pull/12 |
88 | | - # version="oas30-strict", |
89 | | -) |
| 74 | +def _build_oas30_validator() -> Any: |
| 75 | + return create( |
| 76 | + meta_schema=JSONSCHEMA_SPECIFICATIONS.contents( |
| 77 | + "http://json-schema.org/draft-04/schema#", |
| 78 | + ), |
| 79 | + validators=OAS30_VALIDATORS, |
| 80 | + type_checker=oas_types.oas30_type_checker, |
| 81 | + format_checker=oas_format.oas30_format_checker, |
| 82 | + # NOTE: version causes conflict with global jsonschema validator |
| 83 | + # See https://github.com/python-openapi/openapi-schema-validator/pull/12 |
| 84 | + # version="oas30", |
| 85 | + id_of=_oas30_id_of, |
| 86 | + ) |
90 | 87 |
|
91 | | -OAS30ReadValidator = extend( |
92 | | - OAS30Validator, |
93 | | - validators={ |
94 | | - "required": oas_keywords.read_required, |
95 | | - "writeOnly": oas_keywords.read_writeOnly, |
96 | | - }, |
97 | | -) |
98 | 88 |
|
99 | | -OAS30WriteValidator = extend( |
100 | | - OAS30Validator, |
101 | | - validators={ |
102 | | - "required": oas_keywords.write_required, |
103 | | - "readOnly": oas_keywords.write_readOnly, |
104 | | - }, |
105 | | -) |
| 89 | +def _build_oas30_strict_validator(oas30_validator: Any) -> Any: |
| 90 | + return extend( |
| 91 | + oas30_validator, |
| 92 | + validators={ |
| 93 | + "type": oas_keywords.strict_type, |
| 94 | + }, |
| 95 | + type_checker=oas_types.oas30_type_checker, |
| 96 | + format_checker=oas_format.oas30_strict_format_checker, |
| 97 | + # NOTE: version causes conflict with global jsonschema validator |
| 98 | + # See https://github.com/python-openapi/openapi-schema-validator/pull/12 |
| 99 | + # version="oas30-strict", |
| 100 | + ) |
106 | 101 |
|
107 | | -OAS31Validator = extend( |
108 | | - Draft202012Validator, |
109 | | - { |
110 | | - # adjusted to OAS |
111 | | - "allOf": oas_keywords.allOf, |
112 | | - "oneOf": oas_keywords.oneOf, |
113 | | - "anyOf": oas_keywords.anyOf, |
114 | | - "description": oas_keywords.not_implemented, |
115 | | - # fixed OAS fields |
116 | | - "discriminator": oas_keywords.not_implemented, |
117 | | - "xml": oas_keywords.not_implemented, |
118 | | - "externalDocs": oas_keywords.not_implemented, |
119 | | - "example": oas_keywords.not_implemented, |
120 | | - }, |
121 | | - type_checker=oas31_type_checker, |
122 | | - format_checker=oas_format.oas31_format_checker, |
123 | | -) |
| 102 | + |
| 103 | +def _build_oas30_read_validator(oas30_validator: Any) -> Any: |
| 104 | + return extend( |
| 105 | + oas30_validator, |
| 106 | + validators={ |
| 107 | + "required": oas_keywords.read_required, |
| 108 | + "writeOnly": oas_keywords.read_writeOnly, |
| 109 | + }, |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def _build_oas30_write_validator(oas30_validator: Any) -> Any: |
| 114 | + return extend( |
| 115 | + oas30_validator, |
| 116 | + validators={ |
| 117 | + "required": oas_keywords.write_required, |
| 118 | + "readOnly": oas_keywords.write_readOnly, |
| 119 | + }, |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +def _build_oas31_validator() -> Any: |
| 124 | + validator = extend( |
| 125 | + Draft202012Validator, |
| 126 | + { |
| 127 | + # adjusted to OAS |
| 128 | + "allOf": oas_keywords.allOf, |
| 129 | + "oneOf": oas_keywords.oneOf, |
| 130 | + "anyOf": oas_keywords.anyOf, |
| 131 | + "description": oas_keywords.not_implemented, |
| 132 | + # fixed OAS fields |
| 133 | + "discriminator": oas_keywords.not_implemented, |
| 134 | + "xml": oas_keywords.not_implemented, |
| 135 | + "externalDocs": oas_keywords.not_implemented, |
| 136 | + "example": oas_keywords.not_implemented, |
| 137 | + }, |
| 138 | + type_checker=oas31_type_checker, |
| 139 | + format_checker=oas_format.oas31_format_checker, |
| 140 | + ) |
| 141 | + return register_openapi_dialect( |
| 142 | + validator=validator, |
| 143 | + dialect_id=OAS31_BASE_DIALECT_ID, |
| 144 | + version_name="oas31", |
| 145 | + metaschema=OAS31_BASE_DIALECT_METASCHEMA, |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +OAS30Validator = _build_oas30_validator() |
| 150 | +OAS30StrictValidator = _build_oas30_strict_validator(OAS30Validator) |
| 151 | +OAS30ReadValidator = _build_oas30_read_validator(OAS30Validator) |
| 152 | +OAS30WriteValidator = _build_oas30_write_validator(OAS30Validator) |
| 153 | + |
| 154 | +OAS31Validator = _build_oas31_validator() |
124 | 155 |
|
125 | 156 | # OAS 3.2 uses JSON Schema Draft 2020-12 as its base dialect, same as |
126 | 157 | # OAS 3.1. The OAS-specific vocabulary differs slightly (e.g. xml keyword |
|
0 commit comments