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

fix(schema): add basic support of Pattern type in schema generation #1768

Merged
merged 1 commit into from Oct 8, 2020
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/1767-PrettyWood.md
@@ -0,0 +1 @@
add basic support of Pattern type in schema generation
7 changes: 7 additions & 0 deletions docs/build/schema_mapping.py
Expand Up @@ -145,6 +145,13 @@
'JSON Schema Validation',
''
],
[
'Pattern',
'string',
{'format': 'regex'},
'JSON Schema Validation',
''
],
[
'bytes',
'string',
Expand Down
5 changes: 4 additions & 1 deletion pydantic/schema.py
Expand Up @@ -14,6 +14,7 @@
Iterable,
List,
Optional,
Pattern,
Sequence,
Set,
Tuple,
Expand Down Expand Up @@ -618,6 +619,7 @@ def field_singleton_sub_fields_schema(
(IPv6Interface, {'type': 'string', 'format': 'ipv6interface'}),
(IPv4Address, {'type': 'string', 'format': 'ipv4'}),
(IPv6Address, {'type': 'string', 'format': 'ipv6'}),
(Pattern, {'type': 'string', 'format': 'regex'}),
(str, {'type': 'string'}),
(bytes, {'type': 'string', 'format': 'binary'}),
(bool, {'type': 'boolean'}),
Expand All @@ -643,7 +645,8 @@ def add_field_type_to_schema(field_type: Any, schema: Dict[str, Any]) -> None:
and then modifies the given `schema` with the information from that type.
"""
for type_, t_schema in field_class_to_schema:
if issubclass(field_type, type_):
# Fallback for `typing.Pattern` as it is not a valid class
if lenient_issubclass(field_type, type_) or field_type is type_ is Pattern:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may have a better check later to support for example Pattern[str] but typing API changed so much that we'll probably first need some utils to retrieve the right informations depending on python version.
I feel like this is a good enough solution for the time being

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2399 also hits this
a python >= 3.9 solution is in #2392

schema.update(t_schema)
break

Expand Down
7 changes: 7 additions & 0 deletions tests/test_types.py
Expand Up @@ -2055,6 +2055,13 @@ class Foobar(BaseModel):
f2 = Foobar(pattern=p)
assert f2.pattern is p

assert Foobar.schema() == {
'type': 'object',
'title': 'Foobar',
'properties': {'pattern': {'type': 'string', 'format': 'regex', 'title': 'Pattern'}},
'required': ['pattern'],
}


def test_pattern_error():
class Foobar(BaseModel):
Expand Down