def test_validation_ignorecase(): validator = validators.Regex('^some$') validator.validate('some') with pytest.raises(errors.ValidationError): validator.validate('sOmE') validator = validators.Regex('^some$', ignorecase=True) validator.validate('some') validator.validate('SoMe')
def test_validation_multiline(): validator = validators.Regex('^s.*e$') validator.validate('some') with pytest.raises(errors.ValidationError): validator.validate('some\nso more') validator = validators.Regex('^s.*e$', multiline=True) validator.validate('some') validator.validate('some\nso more')
def test_regex_validation_for_wrong_type(): validator = validators.Regex('some') assert 'some' == validator.pattern with pytest.raises(errors.ValidationError): validator.validate(1)
def test_regex_validation_flags(): # Invalid flags ignored validator = validators.Regex("foo", bla=True, ble=False, ignorecase=True) assert validator.flags == [validators.Regex.FLAGS["ignorecase"]] # Flag kwargs must be True-y validator = validators.Regex("foo", ignorecase=False, multiline=True) assert validator.flags == [validators.Regex.FLAGS["multiline"]] # ECMA pattern flags recognized validator = validators.Regex("/foo/im") assert sorted(validator.flags) == sorted([ validators.Regex.FLAGS["multiline"], validators.Regex.FLAGS["ignorecase"], ]) # ECMA pattern overrides flags kwargs validator = validators.Regex("/foo/", ignorecase=True, multiline=True) assert validator.flags == []
def test_regex_validation(): validator = validators.Regex('some') assert 'some' == validator.pattern validator.validate('some string') validator.validate('get some chips') with pytest.raises(errors.ValidationError): validator.validate('asdf') with pytest.raises(errors.ValidationError): validator.validate('trololo')
class People(models.Base): min_max_len = fields.ListField(str, validators=validators.Length(2, 4)) min_len = fields.ListField(str, validators=validators.Length(2)) max_len = fields.ListField(str, validators=validators.Length(4)) item_validator_int = fields.ListField( int, item_validators=[validators.Min(10), validators.Max(20)] ) item_validator_str = fields.ListField( str, item_validators=[validators.Length(10, 20), validators.Regex(r"\w+")], validators=[validators.Length(1, 2)], ) surname = fields.StringField()
def test_validation_2(): validator = validators.Regex('^some[0-9]$') assert '^some[0-9]$' == validator.pattern validator.validate('some0') with pytest.raises(errors.ValidationError): validator.validate('some') with pytest.raises(errors.ValidationError): validator.validate(' some') with pytest.raises(errors.ValidationError): validator.validate('asdf') with pytest.raises(errors.ValidationError): validator.validate('trololo')
def test_regex_validation_custom_error(): custom_error = errors.ValidationError("error") validator = validators.Regex('some', custom_error=custom_error) assert 'some' == validator.pattern validator.validate('some string') validator.validate('get some chips') expected_error = "error" with pytest.raises(errors.ValidationError) as exc_info: validator.validate('asdf') assert expected_error == str(exc_info.value) with pytest.raises(errors.ValidationError) as exc_info: validator.validate('trololo') assert expected_error == str(exc_info.value)
class Person(models.Base): name = fields.StringField( validators=validators.Regex( '^some pattern$', ignorecase=True))
class Person(models.Base): name = fields.StringField( validators=validators.Regex('/^some pattern$/'))
class Person(models.Base): name = fields.StringField( validators=validators.Regex('/^[a-z]+$/i', ignorecase=False))
def __init__(self): super(NameField, self).__init__(required=True, validators=validators.Regex("[A-Z][a-z]+"))