Skip to content

Commit 26ca612

Browse files
committed
Add max_length validation in the set_package_url of PackageURLMixin
Signed-off-by: Thomas Druez <tdruez@nexb.com>
1 parent 70a3b32 commit 26ca612

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/packageurl/contrib/django_models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from __future__ import print_function
2929
from __future__ import unicode_literals
3030

31+
from django.core.exceptions import ValidationError
3132
from django.db import models
3233
from django.utils.translation import ugettext_lazy as _
3334

@@ -120,4 +121,9 @@ def set_package_url(self, package_url):
120121
package_url = PackageURL.from_string(package_url)
121122

122123
for field_name, value in package_url.to_dict(encode=True).items():
124+
model_field = self._meta.get_field(field_name)
125+
126+
if value and len(value) > model_field.max_length:
127+
raise ValidationError(_(f'Value too long for field "{field_name}".'))
128+
123129
setattr(self, field_name, value or None)

0 commit comments

Comments
 (0)