Skip to content

Commit

Permalink
Add custom validation fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Alladin9393 authored and dmytrostriletskyi committed Apr 23, 2019
1 parent f78058d commit c9d82f2
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 42 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ Get balance of the account by its address — ``remme account get-balance``:
$ remme account get-balance \
--address=1120076ecf036e857f42129b58303bcf1e03723764a1702cbe98529802aad8514ee3cf \
--node-url=node-genesis-testnet.remme.io
368440.0
{
"result": 368440.0
}
```

## Development
Expand Down
4 changes: 2 additions & 2 deletions cli/account/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from remme import Remme

from cli.account.forms import GetAccountBalanceForm
from cli.account.help import GET_ACCOUNT_BALANCE_ADDRESS_ARGUMENT_HELP_MESSAGE
from cli.account.help import ADDRESS_ARGUMENT_HELP_MESSAGE
from cli.account.service import Account
from cli.constants import (
FAILED_EXIT_FROM_COMMAND_CODE,
Expand All @@ -28,7 +28,7 @@ def account_commands():
pass


@click.option('--address', type=str, required=True, help=GET_ACCOUNT_BALANCE_ADDRESS_ARGUMENT_HELP_MESSAGE)
@click.option('--address', type=str, required=True, help=ADDRESS_ARGUMENT_HELP_MESSAGE)
@click.option('--node-url', type=str, required=False, help=NODE_URL_ARGUMENT_HELP_MESSAGE, default=default_node_url())
@account_commands.command('get-balance')
def get_balance(address, node_url):
Expand Down
43 changes: 6 additions & 37 deletions cli/account/forms.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
"""
Provide forms for command line interface's account commands.
"""
import re
from marshmallow import Schema

from marshmallow import (
Schema,
ValidationError,
fields,
validates,
)

from cli.constants import (
ADDRESS_REGEXP,
DOMAIN_NAME_REGEXP,
from cli.generic.forms.fields import (
AccountAddressField,
NodeURLField,
)


Expand All @@ -21,29 +14,5 @@ class GetAccountBalanceForm(Schema):
Get balance of the account form.
"""

address = fields.String(required=True)
node_url = fields.String(allow_none=True, required=False)

@validates('address')
def validate_address(self, address):
"""
Validate account address.
"""
if re.match(pattern=ADDRESS_REGEXP, string=address) is None:
raise ValidationError(f'The following address `{address}` is invalid.')

@validates('node_url')
def validate_node_url(self, node_url):
"""
Validate node URL.
If node URL is localhost, it means client didn't passed any URL, so nothing to validate.
"""
if node_url == 'localhost':
return

if 'http' in node_url or 'https' in node_url:
raise ValidationError(f'Pass the following node URL `{node_url}` without protocol (http, https, etc.).')

if re.match(pattern=DOMAIN_NAME_REGEXP, string=node_url) is None:
raise ValidationError(f'The following node URL `{node_url}` is invalid.')
address = AccountAddressField(required=True)
node_url = NodeURLField(allow_none=True, required=False)
2 changes: 1 addition & 1 deletion cli/account/help.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Provide help messages for command line interface's account commands.
"""
GET_ACCOUNT_BALANCE_ADDRESS_ARGUMENT_HELP_MESSAGE = 'Account address to get a balance by.'
ADDRESS_ARGUMENT_HELP_MESSAGE = 'Account address to get a balance by.'
1 change: 1 addition & 0 deletions cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Provide constants for command line interface.
"""
ADDRESS_REGEXP = r'^[0-9a-f]{70}$'
HEADER_SIGNATURE_REGEXP = r'^[0-9a-f]{128}$'
DOMAIN_NAME_REGEXP = r'(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]'

PASSED_EXIT_FROM_COMMAND_CODE = 0
Expand Down
Empty file added cli/generic/__init__.py
Empty file.
Empty file added cli/generic/forms/__init__.py
Empty file.
62 changes: 62 additions & 0 deletions cli/generic/forms/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Provide implementation of the custom fields.
"""
import re

from marshmallow import (
ValidationError,
fields,
)

from cli.constants import (
ADDRESS_REGEXP,
DOMAIN_NAME_REGEXP,
)


class AccountAddressField(fields.Field):
"""
Implements validation of the account address.
References:
- https://marshmallow.readthedocs.io/en/3.0/custom_fields.html
"""

def _deserialize(self, value, attr, obj, **kwargs):
"""
Validate data (account address) that was passed to field.
"""
address = value

if re.match(pattern=ADDRESS_REGEXP, string=address) is None:
raise ValidationError(f'The following address `{address}` is invalid.')

return address


class NodeURLField(fields.Field):
"""
Implements validation of the node URL.
If node URL is localhost, it means client didn't passed any URL, so nothing to validate.
References:
- https://marshmallow.readthedocs.io/en/3.0/custom_fields.html
"""

def _deserialize(self, value, attr, obj, **kwargs):
"""
Validate data (node URL) that was passed to field.
"""
node_url = value

if node_url == 'localhost':
return node_url

if 'http' in node_url or 'https' in node_url:
raise ValidationError(f'Pass the following node URL `{node_url}` without protocol (http, https, etc.).')

if re.match(pattern=DOMAIN_NAME_REGEXP, string=node_url) is None:
raise ValidationError(f'The following node URL `{node_url}` is invalid.')

return node_url
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ combine_as_imports=True
max-line-length=120
ignore=D200, D413, D107, D100
per-file-ignores=
*/__init__.py: D104, F401, D100,
*/__init__.py: D104, F401, D100
*/test_*: D205

[coverage:run]
Expand Down

0 comments on commit c9d82f2

Please sign in to comment.