diff --git a/changes/4641-jparise.md b/changes/4641-jparise.md new file mode 100644 index 00000000000..b671f3c6209 --- /dev/null +++ b/changes/4641-jparise.md @@ -0,0 +1 @@ +Allow dict schemas to have both `patternProperties` and `additionalProperties` diff --git a/pydantic/schema.py b/pydantic/schema.py index e7af56f120d..1348fbd5eef 100644 --- a/pydantic/schema.py +++ b/pydantic/schema.py @@ -490,7 +490,7 @@ def field_type_schema( # Dict keys have a regex pattern # items_schema might be a schema or empty dict, add it either way f_schema['patternProperties'] = {regex.pattern: items_schema} - elif items_schema: + if items_schema: # The dict values are not simply Any, so they need a schema f_schema['additionalProperties'] = items_schema elif field.shape == SHAPE_TUPLE or (field.shape == SHAPE_GENERIC and not issubclass(field.type_, BaseModel)): diff --git a/tests/test_schema.py b/tests/test_schema.py index 143768a8005..60e31776098 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -1654,7 +1654,13 @@ class Foo(BaseModel): 'title': 'Foo', 'type': 'object', 'properties': { - 'a': {'type': 'object', 'title': 'A', 'default': {}, 'patternProperties': {regex_str: {'type': 'string'}}} + 'a': { + 'type': 'object', + 'title': 'A', + 'default': {}, + 'additionalProperties': {'type': 'string'}, + 'patternProperties': {regex_str: {'type': 'string'}}, + } }, }