Skip to content

Commit

Permalink
feat: adding validation for inputs of IncludedChainId (#670)
Browse files Browse the repository at this point in the history
* feat: adding validation for inputs of IncludedChainId

* fix: add missing async statement

---------

Co-authored-by: Gerald Iakobinyi-Pich <gerald@gitcoin.co>
  • Loading branch information
nutrina and Gerald Iakobinyi-Pich authored Sep 9, 2024
1 parent a49b24a commit 52b048e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion api/account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,15 +569,39 @@ async def aget_customization_dynamic_weights(self) -> dict:
return weights


def hex_number_validator(value):
if not value.startswith("0x"):
raise ValidationError("Value expected to start with '0x'")
try:
int(value, 16)
except ValueError as e:
raise ValidationError("Invalid hex number") from e


class IncludedChainId(models.Model):
chain_id = models.CharField(max_length=200, blank=False, null=False)
chain_id = models.CharField(
max_length=200,
blank=False,
null=False,
help_text="Chain ID in hex format (0x1)! You can find this on for example on: https://chainlist.org",
validators=[hex_number_validator],
)
customization = models.ForeignKey(
Customization, on_delete=models.CASCADE, related_name="included_chain_ids"
)

class Meta:
unique_together = ["chain_id", "customization"]

def __str__(self):
int_value = " - bad hex value - "
try:
int_value = int(self.chain_id, 16)
except ValueError:
pass

return f"{self.chain_id} ({int_value})"


class AllowList(models.Model):
address_list = models.ForeignKey(
Expand Down

0 comments on commit 52b048e

Please sign in to comment.