Skip to content

Commit

Permalink
fix(schema): add basic support of Pattern type in schema generation
Browse files Browse the repository at this point in the history
closes #1767
  • Loading branch information
PrettyWood committed Aug 18, 2020
1 parent e985857 commit d239b2b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
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
6 changes: 4 additions & 2 deletions 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,7 @@ 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_):
if lenient_issubclass(field_type, type_):
schema.update(t_schema)
break

Expand Down Expand Up @@ -716,7 +718,7 @@ def field_singleton_schema( # noqa: C901 (ignore complexity)
if lenient_issubclass(getattr(field_type, '__pydantic_model__', None), BaseModel):
field_type = field_type.__pydantic_model__

if issubclass(field_type, BaseModel):
if lenient_issubclass(field_type, BaseModel):
model_name = model_name_map[field_type]
if field_type not in known_models:
sub_schema, sub_definitions, sub_nested_models = model_process_schema(
Expand Down
4 changes: 3 additions & 1 deletion pydantic/utils.py
Expand Up @@ -12,6 +12,7 @@
List,
Mapping,
Optional,
Pattern,
Set,
Tuple,
Type,
Expand Down Expand Up @@ -104,7 +105,8 @@ def validate_field_name(bases: List[Type['BaseModel']], field_name: str) -> None


def lenient_issubclass(cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...]]) -> bool:
return isinstance(cls, type) and issubclass(cls, class_or_tuple)
"""Fallback for `typing.Pattern` as it is not a valid class"""
return isinstance(cls, type) and issubclass(cls, class_or_tuple) or cls is class_or_tuple is Pattern


def in_ipython() -> bool:
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

0 comments on commit d239b2b

Please sign in to comment.