Skip to content

Commit 5cda539

Browse files
committed
Fix no URL validation for ModelSchema URLFields
Fixes #1157
1 parent eecb05f commit 5cda539

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

ninja/orm/fields.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from typing import Any, Callable, Dict, List, Tuple, Type, TypeVar, Union, no_type_check
44
from uuid import UUID
55

6-
from django.db.models import ManyToManyField
6+
from django.db.models import ManyToManyField, URLField
77
from django.db.models.fields import Field as DjangoField
8-
from pydantic import IPvAnyAddress
8+
from pydantic import AnyUrl, IPvAnyAddress
99
from pydantic.fields import FieldInfo
1010
from pydantic_core import PydanticUndefined, core_schema
1111

@@ -69,6 +69,7 @@ def validate(cls, value: Any, _: Any) -> Any:
6969
"SmallIntegerField": int,
7070
"TextField": str,
7171
"TimeField": datetime.time,
72+
"URLField": AnyUrl,
7273
"UUIDField": UUID,
7374
# postgres fields:
7475
"ArrayField": List,
@@ -148,6 +149,9 @@ def get_schema_field(
148149
max_length = field_options.get("max_length")
149150

150151
internal_type = field.get_internal_type()
152+
if isinstance(field, URLField):
153+
internal_type = "URLField"
154+
151155
python_type = TYPES[internal_type]
152156

153157
if field.primary_key or blank or null or optional:

tests/test_orm_schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Meta:
143143
"smallintegerfield": {"type": "integer", "title": "Smallintegerfield"},
144144
"textfield": {"type": "string", "title": "Textfield"},
145145
"timefield": {"type": "string", "format": "time", "title": "Timefield"},
146-
"urlfield": {"type": "string", "title": "Urlfield"},
146+
"urlfield": {'format': 'uri', 'minLength': 1, 'title': 'Urlfield', 'type': 'string'},
147147
"uuidfield": {"type": "string", "format": "uuid", "title": "Uuidfield"},
148148
"arrayfield": {"type": "array", "items": {}, "title": "Arrayfield"},
149149
"cicharfield": {"type": "string", "title": "Cicharfield"},

0 commit comments

Comments
 (0)