Skip to content

Commit

Permalink
Move validate_path outside of Asset model
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnesbitt committed Oct 11, 2022
1 parent d02a9cd commit c55e06b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion dandiapi/api/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
ASSET_PATH_REGEX = fr'^({ASSET_CHARS_REGEX}?\/?\.?{ASSET_CHARS_REGEX})+$'


def validate_asset_path(path: str):
if path.startswith('/'):
raise ValidationError('Path must not begin with /')
if not re.match(ASSET_PATH_REGEX, path):
raise ValidationError(f'Path violates regex: {ASSET_PATH_REGEX}')

return path


class BaseAssetBlob(TimeStampedModel):
SHA256_REGEX = r'[0-9a-f]{64}'
ETAG_REGEX = r'[0-9a-f]{32}(-[1-9][0-9]*)?'
Expand Down Expand Up @@ -109,6 +118,7 @@ class Status(models.TextChoices):
VALID = 'Valid'
INVALID = 'Invalid'

@staticmethod
def validate_path(path: str):
if path.startswith('/'):
raise ValidationError('Path must not begin with /')
Expand All @@ -118,7 +128,7 @@ def validate_path(path: str):
return path

asset_id = models.UUIDField(unique=True, default=uuid.uuid4)
path = models.CharField(max_length=512, validators=[validate_path])
path = models.CharField(max_length=512, validators=[validate_asset_path])
blob = models.ForeignKey(
AssetBlob, related_name='assets', on_delete=models.CASCADE, null=True, blank=True
)
Expand Down

0 comments on commit c55e06b

Please sign in to comment.