|
18 | 18 | from jsonschema.protocols import Validator |
19 | 19 |
|
20 | 20 |
|
21 | | -def include_nullable_validator( |
22 | | - schema: Dict[Hashable, Any] |
23 | | -) -> ItemsView[Hashable, Any]: |
24 | | - """ |
25 | | - Include ``nullable`` validator always. |
26 | | - Suitable for use with `create`'s ``applicable_validators`` argument. |
27 | | - """ |
28 | | - _schema = deepcopy(schema) |
29 | | - |
30 | | - # append defaults to trigger nullable validator, except where $ref in the schema checks null |
31 | | - if "nullable" not in _schema \ |
32 | | - and "$ref" not in _schema \ |
33 | | - and "oneOf" not in _schema \ |
34 | | - and "anyOf" not in _schema \ |
35 | | - and "allOf" not in _schema: |
36 | | - _schema.update( |
37 | | - { |
38 | | - "nullable": False, |
39 | | - } |
40 | | - ) |
41 | | - |
42 | | - return _schema.items() |
43 | | - |
44 | | - |
45 | 21 | def handle_discriminator( |
46 | 22 | validator: Validator, _: Any, instance: Any, schema: Mapping[Hashable, Any] |
47 | 23 | ) -> Iterator[ValidationError]: |
@@ -131,7 +107,14 @@ def type( |
131 | 107 | schema: Mapping[Hashable, Any], |
132 | 108 | ) -> Iterator[ValidationError]: |
133 | 109 | if instance is None: |
134 | | - return |
| 110 | + # nullable implementation based on OAS 3.0.3 |
| 111 | + # * nullable is only meaningful if its value is true |
| 112 | + # * nullable: true is only meaningful in combination with a type |
| 113 | + # assertion specified in the same Schema Object. |
| 114 | + # * nullable: true operates within a single Schema Object |
| 115 | + if "nullable" in schema and schema["nullable"] == True: |
| 116 | + return |
| 117 | + yield ValidationError("None for not nullable") |
135 | 118 |
|
136 | 119 | if not validator.is_type(instance, data_type): |
137 | 120 | data_repr = repr(data_type) |
@@ -167,16 +150,6 @@ def items( |
167 | 150 | yield from validator.descend(item, items, path=index) |
168 | 151 |
|
169 | 152 |
|
170 | | -def nullable( |
171 | | - validator: Validator, |
172 | | - is_nullable: bool, |
173 | | - instance: Any, |
174 | | - schema: Mapping[Hashable, Any], |
175 | | -) -> Iterator[ValidationError]: |
176 | | - if instance is None and not is_nullable: |
177 | | - yield ValidationError("None for not nullable") |
178 | | - |
179 | | - |
180 | 153 | def required( |
181 | 154 | validator: Validator, |
182 | 155 | required: List[str], |
|
0 commit comments