def test_custom_validate(self): class MyField(fields.Raw): __schema_type__ = 'string' def validate(self, value): if value != 'correct': raise fields.MarshallingError('Invalid Value') return True model = Model('MyModel', {'field': MyField()}) bad_data = {'field': 'incorrect'} good_data = {'field': 'correct'} assert model.validate(good_data) is None with pytest.raises(Exception): model.validate(bad_data)
def test_validate(self): from jsonschema import FormatChecker from werkzeug.exceptions import BadRequest class IPAddress(fields.Raw): __schema_type__ = 'string' __schema_format__ = 'ipv4' data = {'ip': '192.168.1'} model = Model('MyModel', {'ip': IPAddress()}) # Test that validate without a FormatChecker does not check if a # primitive type conforms to the defined format property self.assertIsNone(model.validate(data)) # Test that validate with a FormatChecker enforces the check of the # format property and throws an error if invalid with self.assertRaises(BadRequest): model.validate(data, format_checker=FormatChecker())
def test_validate(self): from jsonschema import FormatChecker from werkzeug.exceptions import BadRequest class IPAddress(fields.Raw): __schema_type__ = 'string' __schema_format__ = 'ipv4' data = {'ip': '192.168.1'} model = Model('MyModel', {'ip': IPAddress()}) # Test that validate without a FormatChecker does not check if a # primitive type conforms to the defined format property assert model.validate(data) is None # Test that validate with a FormatChecker enforces the check of the # format property and throws an error if invalid with pytest.raises(BadRequest): model.validate(data, format_checker=FormatChecker())