Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dicts to have both patternProperties and additionalProperties #4641

Merged
merged 1 commit into from Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/4641-jparise.md
@@ -0,0 +1 @@
Allow dict schemas to have both `patternProperties` and `additionalProperties`
2 changes: 1 addition & 1 deletion pydantic/schema.py
Expand Up @@ -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)):
Expand Down
8 changes: 7 additions & 1 deletion tests/test_schema.py
Expand Up @@ -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'}},
}
},
}

Expand Down