|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from jsonschema import ValidationError |
| 5 | +from referencing import Registry |
| 6 | +from referencing import Resource |
| 7 | +from referencing.jsonschema import DRAFT202012 |
5 | 8 |
|
6 | 9 | from openapi_schema_validator import OAS30ReadValidator |
7 | 10 | from openapi_schema_validator import OAS30Validator |
@@ -98,6 +101,51 @@ def test_string_invalid(self, validator_class, value): |
98 | 101 | with pytest.raises(ValidationError): |
99 | 102 | validator.validate(value) |
100 | 103 |
|
| 104 | + def test_referencing(self, validator_class): |
| 105 | + name_schema = Resource.from_contents( |
| 106 | + { |
| 107 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 108 | + "type": "string", |
| 109 | + } |
| 110 | + ) |
| 111 | + age_schema = DRAFT202012.create_resource( |
| 112 | + { |
| 113 | + "type": "integer", |
| 114 | + "format": "int32", |
| 115 | + "minimum": 0, |
| 116 | + "maximum": 120, |
| 117 | + } |
| 118 | + ) |
| 119 | + birth_date_schema = Resource.from_contents( |
| 120 | + { |
| 121 | + "type": "string", |
| 122 | + "format": "date", |
| 123 | + }, |
| 124 | + default_specification=DRAFT202012, |
| 125 | + ) |
| 126 | + registry = Registry().with_resources( |
| 127 | + [ |
| 128 | + ("urn:name-schema", name_schema), |
| 129 | + ("urn:age-schema", age_schema), |
| 130 | + ("urn:birth-date-schema", birth_date_schema), |
| 131 | + ], |
| 132 | + ) |
| 133 | + schema = { |
| 134 | + "type": "object", |
| 135 | + "required": ["name"], |
| 136 | + "properties": { |
| 137 | + "name": {"$ref": "urn:name-schema"}, |
| 138 | + "age": {"$ref": "urn:age-schema"}, |
| 139 | + "birth-date": {"$ref": "urn:birth-date-schema"}, |
| 140 | + }, |
| 141 | + "additionalProperties": False, |
| 142 | + } |
| 143 | + |
| 144 | + validator = validator_class(schema, registry=registry) |
| 145 | + result = validator.validate({"name": "John", "age": 23}, schema) |
| 146 | + |
| 147 | + assert result is None |
| 148 | + |
101 | 149 |
|
102 | 150 | class TestOAS30ValidatorValidate(BaseTestOASValidatorValidate): |
103 | 151 | @pytest.fixture |
|
0 commit comments