Skip to content

Commit

Permalink
🚸 Validate minimum uid length (laminlabs#2144)
Browse files Browse the repository at this point in the history
  • Loading branch information
falexwolf authored Nov 8, 2024
1 parent 484a20e commit befe634
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
13 changes: 12 additions & 1 deletion lamindb/_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import dj_database_url
import lamindb_setup as ln_setup
from django.core.exceptions import FieldDoesNotExist
from django.db import connections, transaction
from django.db.models import IntegerField, Manager, Q, QuerySet, Value
from lamin_utils import colors, logger
Expand All @@ -21,7 +22,7 @@

from ._utils import attach_func_to_class_method
from .core._settings import settings
from .core.exceptions import RecordNameChangeIntegrityError
from .core.exceptions import RecordNameChangeIntegrityError, ValidationError

if TYPE_CHECKING:
import pandas as pd
Expand Down Expand Up @@ -58,6 +59,16 @@ def validate_required_fields(record: Record, kwargs):
]
if missing_fields:
raise TypeError(f"{missing_fields} are required.")
try:
uid_max_length = record.__class__._meta.get_field(
"uid"
).max_length # triggers FieldDoesNotExist
if len(kwargs["uid"]) != uid_max_length: # triggers KeyError
raise ValidationError(
f'`uid` must be exactly {uid_max_length} characters long, got {len(kwargs["uid"])}.'
)
except (FieldDoesNotExist, KeyError):
pass


def suggest_records_with_similar_names(record: Record, name_field: str, kwargs) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion sub/lamin-cli
2 changes: 1 addition & 1 deletion sub/lnschema-core
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@
"metadata": {},
"outputs": [],
"source": [
"import lamindb as ln"
"import lamindb as ln\n",
"import pytest\n",
"from lamindb.core.exceptions import ValidationError"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with pytest.raises(ValidationError):\n",
" ln.track(\"ujPaFZ\")"
]
},
{
Expand Down Expand Up @@ -55,7 +67,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "py39",
"display_name": "py310",
"language": "python",
"name": "python3"
},
Expand Down

0 comments on commit befe634

Please sign in to comment.