def test_nested_dict_with_invalid_prop_should_raise_error(self): schema = DictField(schema={ 'foo': BoolField(), 'bar': DictField(schema={'baz': BoolField()}) }, strict=True) value = {'foo': True, 'bar': {'baz': 123}} validator = SchemaValidator(schema=schema, value=value) try: validator.validate() self.fail() except SchemaValidationError as e: self.assertEqual(e.path, '$root.bar.baz') self.assertEqual(e.code, 'BOOL_TYPE')
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_optional_prop_should_be_valid_if_not_present(self): schema = DictField(schema={ 'foo': BoolField(), 'bar': BoolField() }, strict=True, optional_props=['bar']) value = {'foo': True} validator = SchemaValidator(schema=schema, value=value) validator.validate()
def test_invalid_type_should_raise_error(self): schema = DictField(schema={}) 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, 'DICT_TYPE')
def test_invalid_nested_list_item_value_should_raise_error(self): schema = ListField(item_schema=DictField( schema={'values': ListField(item_schema=BoolField())})) value = [{'values': [True, False, 'Link']}] validator = SchemaValidator(schema=schema, value=value) try: validator.validate() self.fail() except SchemaValidationError as e: self.assertEqual(e.path, '$root.$0.values.$2') self.assertEqual(e.code, 'BOOL_TYPE')
def test_missing_prop_should_raise_error(self): schema = DictField(schema={'foo': BoolField(), 'bar': BoolField()}) value = {'foo': True} validator = SchemaValidator(schema=schema, value=value) try: validator.validate() self.fail() except SchemaValidationError as e: self.assertEqual(e.path, '$root') self.assertEqual(e.code, 'DICT_PROP_MISSING') self.assertEqual(e.extra, {'prop': 'bar'})
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)
def test_not_allowed_prop_should_raise_error(self): schema = DictField(schema={ 'foo': BoolField(), 'bar': BoolField() }, strict=True) value = {'foo': True, 'bar': False, 'baz': 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, 'DICT_PROP_NOT_ALLOWED') self.assertEqual(e.extra, {'prop': 'baz'})