Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runtime validation for string fields #766

Open
coderabbitai bot opened this issue Nov 1, 2024 · 2 comments
Open

Add runtime validation for string fields #766

coderabbitai bot opened this issue Nov 1, 2024 · 2 comments

Comments

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 1, 2024

As discussed in PR #765 comment, we should consider adding runtime validation for cryptographic fields such as public_key and signature in the XChainAddClaimAttestation class to ensure they meet specific format requirements.\n\nOriginally suggested by @coderabbitai[bot] and agreed upon by @mvadari.

@mvadari mvadari removed their assignment Nov 1, 2024
@mvadari mvadari changed the title Add runtime validation for cryptographic fields in XChainAddClaimAttestation Add runtime validation for string fields Nov 1, 2024
@mvadari
Copy link
Collaborator

mvadari commented Nov 1, 2024

Copying this sample code from @coderabbitai's comment:

from typing import NewType
from typing_extensions import Annotated

PublicKeyStr = NewType('PublicKeyStr', str)
SignatureStr = NewType('SignatureStr', str)

def validate_public_key(value: str) -> PublicKeyStr:
    if not is_valid_public_key(value):  # implement validation logic
        raise ValueError("Invalid public key format")
    return PublicKeyStr(value)

# Then use in class:
public_key: Annotated[PublicKeyStr, REQUIRED]
signature: Annotated[SignatureStr, REQUIRED]

If this type of validation can be done at the typing level, that would be really useful for e.g. ensuring that XRP amounts are valid and that blobs are in hex.

Copy link
Contributor Author

coderabbitai bot commented Nov 1, 2024

Tip

For best results, initiate chat on the files or code changes.

@‌mvadari, that's a great idea! Implementing validation at the typing level can indeed help ensure that fields like XRP amounts are valid and blobs are in hex format.

Here's how you might define custom types and validators for XRP amounts and hex blobs:

from typing import NewType
from typing_extensions import Annotated

AmountStr = NewType('AmountStr', str)
HexBlobStr = NewType('HexBlobStr', str)

def validate_amount(value: str) -> AmountStr:
    if not is_valid_amount(value):  # implement validation logic
        raise ValueError("Invalid XRP amount format")
    return AmountStr(value)

def validate_hex_blob(value: str) -> HexBlobStr:
    if not is_valid_hex_blob(value):  # implement validation logic
        raise ValueError("Invalid hex blob format")
    return HexBlobStr(value)

# Then in your class:
amount: Annotated[AmountStr, REQUIRED]
blob: Annotated[HexBlobStr, REQUIRED]

You'll need to implement the is_valid_amount and is_valid_hex_blob functions with the specific validation logic for XRP amounts and hex blobs. By integrating validation into the type annotations, you can catch invalid data early and make your code more robust.

Let me know if you need any further assistance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant