def update_invoice(self, params): address_schema = Schema({ 'line1': {'type': basestring, 'required': True}, 'city': {'type': basestring, 'required': False}, 'state': {'type': basestring, 'required': False}, 'postal_code': {'type': basestring, 'required': False}, 'country': {'type': basestring, 'required': False}, }) recipient_schema = Schema({ 'dob': {'type': basestring, 'required': True}, 'entity_type': {'type': basestring, 'required': True, 'validates': one_of('INDIVIDUAL', 'COMPANY')}, 'first_name': {'type': basestring, 'required': True}, 'last_name': {'type': basestring, 'required': True}, 'document_type': {'type': basestring, 'required': True}, 'document_number': {'type': basestring, 'required': True}, 'email': {'type': basestring, 'required': False}, 'mobile': {'type': basestring, 'required': False}, 'address': {'type': address_schema, 'required': False}, 'bank_account_number': {'type': basestring, 'required': False}, 'bank_name': {'type': basestring, 'required': False}, }, strict=False) unit_price_schema = Schema({ 'currency': {'type': basestring, 'required': True}, 'amount': {'type': numbers.Number, 'required': True}, }) products_schema = Schema({ 'name': {'type': basestring, 'required': True}, 'quantity': {'type': int, 'required': True}, 'unit_price': {'type': unit_price_schema, 'required': True} }) param_schema = Schema({ 'recipient': {'type': recipient_schema, 'required': True}, 'products': {'type': Array(products_schema), 'required': True}, 'memo': {'type': basestring, 'required': False}, 'out_trade_id': {'type': basestring, 'required': False}, 'currency': {'type': basestring, 'required': True}, 'amount': {'type': numbers.Number, 'required': True}, 'ex_invoice_no': {'type': basestring, 'required': True}, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=json.dumps(params), url='invoice', method='PUT', )
def test_type_can_be_another_schema(self): Schema({ "author": { 'type': Schema({ 'first': { 'type': str }, 'last': { 'type': str } }) } })
def setUp(self): self.schema = Schema({ 'somefield': {'type': str}, 'otherfield': {'type': str}, 'anotherfield': {'type': str}, }, validates=requires_all_or_none_of('somefield', 'otherfield'))
def test_array_of_strings_with_default(self): Schema({ "fruit": { 'type': Array(basestring), "default": ['apple', 'orange'] } })
def setUp(self): self.schema = Schema({ 'somefield': {'type': str}, 'otherfield': {'type': str}, 'anotherfield': {'type': str}, }, validates=requires_at_least_one_of('somefield', ['otherfield', 'anotherfield']))
def setUp(self): self.schema = Schema({ 'somefield': {'type': str}, 'otherfield': {'type': str}, 'anotherfield': {'type': str}, }, validates=mutually_exclusive('somefield', ['otherfield', 'anotherfield']))
def test_default_schema_value_correct_2(self): Schema({ 'wheel': { 'type': Schema({ 'size': { 'type': int, 'default': 32 }, 'brand': { 'type': basestring, 'default': 'firestone' } }), 'default': {} } })
def create_transfer(self, params): beneficiary_schema = Schema({ 'bank_account_number': {'type': basestring, 'required': True}, 'amount': {'type': numbers.Number, 'required': True}, 'currency': {'type': basestring, 'required': True}, }, strict=False) param_schema = Schema({ 'send_amount': {'type': numbers.Number, 'required': True}, 'send_currency': {'type': basestring, 'required': True}, 'beneficiaries': {'type': Array(beneficiary_schema), 'required': True}, 'purpose': {'type': basestring, 'required': True}, 'memo': {'type': basestring, 'required': False}, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=json.dumps(params), url='transfer', method='POST', )
def test_default_array_schema_value_correct_1(self): Schema({ 'wheel': { 'type': Array( Schema({ 'size': { 'type': int, 'default': 32 }, 'brand': { 'type': basestring, 'default': 'firestone' } })), 'default': [{ 'wrong': True }] } })
def __individual_identification_schema(self, params): param_schema = Schema( { 'name': { 'type': basestring, 'required': True }, 'country': { 'type': basestring, 'required': True }, 'mobile': { 'type': basestring, 'required': True }, 'doc_type': { 'type': basestring, 'required': True, 'validates': one_of('ID', 'PASSPORT') }, 'doc_number': { 'type': basestring, 'required': True }, 'address': { 'type': basestring, 'required': True }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值
def get_bacc(self, params): param_schema = Schema( { 'bank_account_number': { 'type': basestring, 'required': True }, 'purpose': { 'type': basestring, 'required': True, 'validates': one_of('PAYER', 'BENEFICIARY') }, 'currency': { 'type': basestring, 'required': True }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 return KeyVariables( params=params, url='account/bank_account', method='GET', )
def get_iats(self, params): param_schema = Schema( { 'from_created_at': { 'type': basestring, 'required': False }, 'to_created_at': { 'type': basestring, 'required': False }, 'send_currency': { 'type': basestring, 'required': False }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='iats', method='GET', )
def get_deposits(self, params): param_schema = Schema( { 'from_created_at': { 'type': basestring, 'required': True }, 'to_created_at': { 'type': basestring, 'required': True }, 'currency': { 'type': basestring, 'required': True }, 'status': { 'type': basestring, 'required': True, 'validates': one_of('CREDITED', 'PROCESSING', 'CANCELED') }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 return KeyVariables( params=params, url='deposits', method='GET', )
def create_exchange(self, params): param_schema = Schema( { 'sell_amount': { 'type': numbers.Number, 'required': True }, 'sell_currency': { 'type': basestring, 'required': True }, 'buy_amount': { 'type': numbers.Number, 'required': True }, 'buy_currency': { 'type': basestring, 'required': True }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=json.dumps(params), url='exchange', method='POST', )
def __init__(self, **kwargs): """ :param author: 作者 :param title: 标题 :param content: 内容 :param label: 标签 """ param_schema = Schema({ 'id': { 'type': basestring, 'required': False }, 'no': { 'type': basestring, 'required': False }, 'author': { 'type': basestring, 'required': True }, 'title': { 'type': basestring, 'required': True }, 'intro': { 'type': basestring, 'required': False }, 'content': { 'type': basestring, 'required': True }, 'category': { 'type': basestring, 'required': False }, 'views': { 'type': basestring, 'required': False }, 'modified': { 'type': basestring, 'required': False }, }) param_schema.validate(kwargs) # 检测不通过直接抛异常 param_schema.apply_defaults(kwargs) # attach 默认值 self.id = kwargs.get('id') self.no = kwargs.get('no') self.author = kwargs.get('author') self.title = kwargs.get('title') self.intro = kwargs.get('intro') self.content = kwargs.get('content') self.category = kwargs.get('category') self.views = kwargs.get('views') self.modified = kwargs.get('modified')
def test_nested_schema_cannot_have_validation(self): def some_func(): pass self.assert_spec_invalid( { "content": { 'type': Schema({"somefield": { "type": int }}), "validates": some_func } }, 'content')
def setUp(self): self.base_schema = Schema({ 'street': { 'type': basestring }, 'house_number': { 'type': int, 'default': 1 } }) self.bigger_schema = Schema({ 'address': { 'type': self.base_schema, 'default': {} }, 'first_name': { 'type': basestring, 'required': True }, 'last_name': { 'type': basestring, 'default': 'johnson' } })
def test_default_array_schema_value_of_incorrect_type_2(self): self.assert_spec_invalid( { 'wheel': { 'type': Array( Schema({ 'size': { 'type': int, 'default': 32 }, 'brand': { 'type': basestring, 'default': 'firestone' } })), 'default': {} } }, 'wheel')
def get_transfer(self, params): param_schema = Schema({ 'no': {'type': basestring, 'required': True}, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='transfer', method='GET', )
def cancel_invoice(self, params): param_schema = Schema({ 'no': {'type': basestring, 'required': True}, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='invoice', method='DELETE', )
def get_rate(self, params): param_schema = Schema({ 'sell_currency': {'type': basestring, 'required': True}, 'buy_currency': {'type': basestring, 'required': True}, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='rate', method='GET', )
class TestRequiresAllOrNoneOf(unittest.TestCase): def setUp(self): self.schema = Schema({ 'somefield': {'type': str}, 'otherfield': {'type': str}, 'anotherfield': {'type': str}, }, validates=requires_all_or_none_of('somefield', 'otherfield')) def test_valid(self): self.schema.validate({}) self.schema.validate({'somefield': 'somevalue', 'otherfield': 'othervalue'}) def test_invalid(self): with self.assertRaises(ValidationException): self.schema.validate({'otherfield': 'partialvalue'})
def __init__(self, **kwargs): # 先对参数做校验,校验不通过不生成News对象 params_schema = Schema({ 'no': { 'type': basestring, 'required': False, 'default': shortuuid.uuid().upper() }, 'title': { 'type': basestring, 'required': True }, 'author_name': { 'type': basestring, 'required': True }, 'url': { 'type': basestring, 'required': True }, 'date': { 'type': basestring, 'required': False }, 'pictures': { 'type': dict, 'required': False }, 'category': { 'type': basestring, 'required': False, 'default': 'default' }, # 这里要限制类型吗? }) params_schema.validate(kwargs) params_schema.apply_defaults(kwargs) self.no = kwargs.get('no') self.title = kwargs.get('title') self.author_name = kwargs.get('author_name') self.url = kwargs.get('url') self.date = kwargs.get('date') self.pictures = kwargs.get('pictures') self.category = get_category(kwargs.get('category'))
def get_transfers(self, params): param_schema = Schema({ 'from_created_at': {'type': basestring, 'required': False}, 'to_created_at': {'type': basestring, 'required': False}, 'send_currency': {'type': basestring, 'required': False}, 'receive_currency': {'type': basestring, 'required': False}, 'status': {'type': basestring, 'required': False, 'validates': one_of('RISK', 'PAYMENT', 'CONVERSION', 'DELIVERY')}, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='transfers', method='GET', )
def payment(self, params): param_schema = Schema({ 'no': { 'type': basestring, 'required': True }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='exchange/payment', method='POST', )
def create_acc(self, params): param_schema = Schema( { 'account': { 'type': basestring, 'required': False }, 'password': { 'type': basestring, 'required': False }, 'entity_type': { 'type': basestring, 'required': True, 'validates': one_of('COMPANY', 'INDIVIDUAL') }, 'secret': { 'type': basestring, 'required': False }, 'partner': { 'type': basestring, 'required': True }, 'callback_url': { 'type': basestring, 'required': False }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='account', method='POST', )
def create_deposit(self, params): param_schema = Schema( { 'payer_bank_acc_number': { 'type': basestring, 'required': False }, 'payer_bank_acc_no': { 'type': basestring, 'required': False }, 'escrow_bank_acc_number': { 'type': basestring, 'required': False }, 'currency': { 'type': basestring, 'required': True }, 'amount': { 'type': basestring, 'required': True }, 'out_trade_id': { 'type': basestring, 'required': False }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='deposit', method='POST', )
def get_baccs(self, params): param_schema = Schema( { 'purpose': { 'type': basestring, 'required': False, 'validates': one_of('PAYER', 'BENEFICIARY') }, 'status': { 'type': basestring, 'required': False, 'validates': one_of('NEW', 'APPROVED', 'PENDING', 'DENIED') }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 return KeyVariables( params=params, url='account/bank_accounts', method='GET', )
def escrow_accounts(self, params): param_schema = Schema( { 'currency': { 'type': basestring, 'required': True }, 'country': { 'type': basestring, 'required': True }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='escrow_accounts', method='GET', )
def download(self, params): param_schema = Schema( { 'from_created_at': { 'type': basestring, 'required': True }, 'to_created_at': { 'type': basestring, 'required': True }, }, strict=False) param_schema.validate(params) # 检测不通过直接抛异常 param_schema.apply_defaults(params) # attach 默认值 # 校验数据 return KeyVariables( params=params, url='statement', method='GET', )
def test_multiple_validation_functions(self): Schema({'some_field': {'type': int, "validates": [gte(1), lte(10)]}})
def test_single_validation_function(self): Schema({'some_field': {'type': int, "validates": one_of(['a', 'b'])}})
def test_array_validation(self): Schema( {"fruit": { 'type': Array(basestring), "validates": length(1, 2) }})
def test_array_of_ints(self): Schema({"numbers": {"type": Array(int)}})
class TestMutuallyExclusive(unittest.TestCase): def setUp(self): self.schema = Schema({ 'somefield': {'type': str}, 'otherfield': {'type': str}, 'anotherfield': {'type': str}, }, validates=mutually_exclusive('somefield', ['otherfield', 'anotherfield'])) def test_valid(self): self.schema.validate({}) self.schema.validate({'otherfield': 'partialvalue'}) self.schema.validate({'somefield': 'somevalue'}) self.schema.validate({'otherfield': 'othervalue', 'anotherfield': 'anothervalue'}) self.schema.validate({'somefield': 'somevalue', 'otherfield': 'partialvalue'}) def test_invalid(self): with self.assertRaises(ValidationException): self.schema.validate({'somefield': 'somevalue', 'otherfield': 'othervalue', 'anotherfield': 'anothervalue'})