def test_one_valid_schema_should_pass(self): schema = OrField(schemas=[StrField(), BoolField(), IntField()]) value = True validator = SchemaValidator(schema, value) validator.validate()
def test_valid_value_should_pass(self): schema = IntField(min=1, max=10) value = 5 validator = SchemaValidator(schema=schema, value=value) validator.validate()
def test_full_schema_should_pass(self): schema = ListField( min_items=1, max_items=3, item_schema=DictField( schema={ 'name': StrField( min_length=2, max_length=50, ), 'age': IntField(min=0, max=120), 'money': FloatField(min=0.0, max=999.9), 'alive': BoolField(), 'gender': EnumField(accept=['M', 'F', 'O']), 'code': RegexField(regex='^([0-9]{3})'), 'doc': OrField(schemas=[ RegexField( regex='[0-9]{3}\\.?[0-9]{3}\\.?[0-9]{3}\\-?[0-9]{2}' ), # cpf RegexField( regex= '[0-9]{2}\\.?[0-9]{3}\\.?[0-9]{3}\\/?[0-9]{4}\\-?[0-9]{2}' ) # cnpj ]) }, strict=True, optional_props=['gender', 'code', 'doc'])) value = [{ 'name': 'Batman', 'age': 31, 'money': 999.0, 'alive': True, 'gender': 'M', 'code': '468', 'doc': '759.425.730-85' }, { 'name': 'Superman', 'age': 29, 'money': 0.0, 'alive': True, 'doc': '31.035.254/0001-79' }] validator = SchemaValidator(schema=schema, value=value) validator.validate()
def test_not_type_int_should_raise_error(self): schema = IntField(required=True) value = 'abc' validator = SchemaValidator(schema=schema, value=value) try: validator.validate() self.fail() except SchemaValidationError as e: self.assertEqual(e.path, '$root') self.assertEqual(e.code, 'INT_TYPE')
def test_required_with_none_should_raise_error(self): schema = IntField(required=True) value = None validator = SchemaValidator(schema=schema, value=value) try: validator.validate() self.fail() except SchemaValidationError as e: self.assertEqual(e.path, '$root') self.assertEqual(e.code, 'REQUIRED_VALUE')
def test_value_greater_should_raise_error(self): schema = IntField(max=1) value = 2 validator = SchemaValidator(schema=schema, value=value) try: validator.validate() self.fail() except SchemaValidationError as e: self.assertEqual(e.path, '$root') self.assertEqual(e.code, 'INT_MAX')
def test_multiple_schema_error_should_raise_error(self): schema = OrField(schemas=[StrField(), IntField()]) value = True validator = SchemaValidator(schema, value) try: validator.validate() self.fail() except SchemaValidationError as e: self.assertEqual(e.code, 'OR_NO_MATCHING_SCHEMA') self.assertEqual(2, len(e.extra['errors']))
def test_raised_error_should_contain_node(self): age_field = IntField(min=18) schema = DictField(schema={'age': age_field}) value = {'age': 12} try: validator = SchemaValidator(schema, value) validator.validate() self.fail() except SchemaValidationError as err: self.assertEqual(err.node, age_field)