Fix variable type check and docstrings

This commit is contained in:
kansi 2017-11-03 18:37:19 +05:30
parent 99aa38b217
commit a29fd7e84f
3 changed files with 9 additions and 14 deletions

View File

@ -108,14 +108,14 @@ def init_database(connection=None, dbname=None):
create_indexes(connection, dbname) create_indexes(connection, dbname)
def validate_if_exists_language(obj, key): def validate_language_key(obj, key):
"""Validate all nested "language" key in `obj`. """Validate all nested "language" key in `obj`.
Args: Args:
obj (dict): dictonary whose "language" key is to be validated. obj (dict): dictonary whose "language" key is to be validated.
Returns: Returns:
None: validation successfull None: validation successful
Raises: Raises:
ValidationError: raises execption incase language is not valid. ValidationError: raises execption incase language is not valid.
@ -135,7 +135,7 @@ def validate_language(value):
value (str): language to validated value (str): language to validated
Returns: Returns:
None: validation successfull None: validation successful
Raises: Raises:
ValidationError: raises execption incase language is not valid. ValidationError: raises execption incase language is not valid.
@ -145,4 +145,4 @@ def validate_language(value):
'language "{}". If you do not understand this error ' 'language "{}". If you do not understand this error '
'message then please rename key/field "language" to ' 'message then please rename key/field "language" to '
'something else like "lang".').format(value) 'something else like "lang".').format(value)
raise ValidationError(error_str) from ValueError() raise ValidationError(error_str)

View File

@ -92,9 +92,8 @@ def validate_all_keys(obj_name, obj, validation_fun):
""" """
for key, value in obj.items(): for key, value in obj.items():
validation_fun(obj_name, key) validation_fun(obj_name, key)
if type(value) is dict: if isinstance(value, dict):
validate_all_keys(obj_name, value, validation_fun) validate_all_keys(obj_name, value, validation_fun)
return
def validate_all_value_for_key(obj, key, validation_fun): def validate_all_value_for_key(obj, key, validation_fun):
@ -107,18 +106,14 @@ def validate_all_value_for_key(obj, key, validation_fun):
validation_fun (function): function used to validate the value validation_fun (function): function used to validate the value
of `key`. of `key`.
Returns:
None: indicates validation successfull
Raises: Raises:
ValidationError: `validation_fun` will raise this error on failure ValidationError: `validation_fun` will raise this error on failure
""" """
for vkey, value in obj.items(): for vkey, value in obj.items():
if vkey == key: if vkey == key:
validation_fun(value) validation_fun(value)
elif type(value) is dict: elif isinstance(value, dict):
validate_all_value_for_key(value, key, validation_fun) validate_all_value_for_key(value, key, validation_fun)
return
def validate_key(obj_name, key): def validate_key(obj_name, key):
@ -139,4 +134,4 @@ def validate_key(obj_name, key):
error_str = ('Invalid key name "{}" in {} object. The ' error_str = ('Invalid key name "{}" in {} object. The '
'key name cannot contain characters ' 'key name cannot contain characters '
'".", "$" or null characters').format(key, obj_name) '".", "$" or null characters').format(key, obj_name)
raise ValidationError(error_str) from ValueError() raise ValidationError(error_str)

View File

@ -11,7 +11,7 @@ from bigchaindb.common.transaction import Transaction
from bigchaindb.common.utils import (gen_timestamp, serialize, from bigchaindb.common.utils import (gen_timestamp, serialize,
validate_txn_obj, validate_key) validate_txn_obj, validate_key)
from bigchaindb.common.schema import validate_transaction_schema from bigchaindb.common.schema import validate_transaction_schema
from bigchaindb.backend.schema import validate_if_exists_language from bigchaindb.backend.schema import validate_language_key
class Transaction(Transaction): class Transaction(Transaction):
@ -88,7 +88,7 @@ class Transaction(Transaction):
validate_transaction_schema(tx_body) validate_transaction_schema(tx_body)
validate_txn_obj('asset', tx_body['asset'], 'data', validate_key) validate_txn_obj('asset', tx_body['asset'], 'data', validate_key)
validate_txn_obj('metadata', tx_body, 'metadata', validate_key) validate_txn_obj('metadata', tx_body, 'metadata', validate_key)
validate_if_exists_language(tx_body['asset'], 'data') validate_language_key(tx_body['asset'], 'data')
return super().from_dict(tx_body) return super().from_dict(tx_body)
@classmethod @classmethod