dict_validator.fields.regexp package¶
This package contains most frequently used subclasses of
dict_validator.fields.String
.
-
class
dict_validator.fields.regexp.
Email
(domain=None, **kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Make sure that the input is a valid email.
Parameters: domain – string representing a desired domain name. e.g. “gmail.com” if not present matches any domain name >>> from dict_validator import validate, deserialize
>>> class Schema: ... field = Email()
>>> list(validate(Schema, {"field": "test@example.com"})) []
>>> list(validate(Schema, {"field": "test.foo@example.bla.com"})) []
>>> list(validate(Schema, {"field": "test123@examp123e.com"})) []
>>> list(validate(Schema, {"field": "test-dff@example-ff.com"})) []
>>> list(validate(Schema, {"field": "test%%20dff@example-ff.com"})) []
>>> list(validate(Schema, {"field": "test+20dff@example-ff.com"})) []
Missing domain:
>>> list(validate(Schema, {"field": "test@"})) [(['field'], 'Did not match Regexp(email)')]
Missing beginning:
>>> list(validate(Schema, {"field": "@example-ff.com"})) [(['field'], 'Did not match Regexp(email)')]
Wrong beginning:
>>> list(validate(Schema, {"field": "~~~@example.bla.com"})) [(['field'], 'Did not match Regexp(email)')]
Wrong domain:
>>> list(validate(Schema, {"field": "test123@examp++e.com"})) [(['field'], 'Did not match Regexp(email)')]
No @ char:
>>> list(validate(Schema, {"field": "fdfdfdgdg"})) [(['field'], 'Did not match Regexp(email)')]
Specify a domain:
>>> class Schema: ... field = Email(domain="example.com")
>>> list(validate(Schema, {"field": "test@example.com"})) []
Wrong domain:
>>> list(validate(Schema, {"field": "test@not-example.com"})) [(['field'], 'Did not match Regexp(email)')]
>>> deserialize(Schema, {"field": "foobar@EXAMPLE.com"}).field 'foobar@example.com'
-
class
dict_validator.fields.regexp.
Phone
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Make sure that the input is a valid phone number.
>>> from dict_validator import validate, deserialize
>>> class Schema: ... field = Phone()
>>> list(validate(Schema, {"field": '+358 807 12'})) []
Has to start with a +
>>> list(validate(Schema, {"field": '358 807 12'})) [(['field'], 'Did not match Regexp(phone)')]
>>> deserialize(Schema, {"field": '+358 807 12'}).field '+35880712'
-
class
dict_validator.fields.regexp.
Url
(protocol=None, domain=None, port=None, path=None, **kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Simple pattern to match http or https URL.
Parameters: - protocol – optional protocol spec (defaults to http[s])
- domain – optional domain (defaults to a wildcard)
- port – optional port (defaults to any number from 0 to 65535)
- path – optional path (defaults to a wildcard)
>>> from dict_validator import validate
By default a wildcard URL is matched.
>>> class Schema: ... field = Url()
>>> list(validate(Schema, ... {"field": "http://www.example.com/path-to-resource" ... "?foo-bar=bar-foo&zoo=loo#fff-ggg"})) []
SSL:
>>> list(validate(Schema, ... {"field": "https://www.example.com?foo=bar#fff"})) []
With port:
>>> list(validate(Schema, ... {"field": "http://www.example.com:8080?foo=bar#fff"})) []
No protocol:
>>> list(validate(Schema, ... {"field": "www.example.com?foo=bar#fff"})) []
Wrong protocol:
>>> list(validate(Schema, ... {"field": "bla://www.example.com?foo=bar#fff"})) [(['field'], 'Did not match Regexp(url)')]
No domain:
>>> list(validate(Schema, ... {"field": "http://foo=bar#fff"})) [(['field'], 'Did not match Regexp(url)')]
It is possible to configure certain parts of the url to match specific values.
>>> class Schema: ... field = Url(protocol="ftp", domain="example.com", ... path="/foobar-zooloo", port=8080)
>>> list(validate(Schema, ... {"field": "ftp://example.com:8080/foobar-zooloo" ... "?foo-bar=bar-foo&zoo=loo#fff-ggg"})) []
Wrong protocol:
>>> list(validate(Schema, ... {"field": "http://example.com/foobar-zooloo?ffff#dffdf"})) [(['field'], 'Did not match Regexp(url)')]
Wrong domain:
>>> list(validate(Schema, ... {"field": "ftp://not-example.com/foobar-zooloo?ffff#dffdf"})) [(['field'], 'Did not match Regexp(url)')]
Wrong path:
>>> list(validate(Schema, ... {"field": "ftp://example.com/zooloo?ffff#dffdf"})) [(['field'], 'Did not match Regexp(url)')]
Wrong port:
>>> list(validate(Schema, ... {"field": "ftp://example.com:100000/foobar-zooloo?ffff#dffdf"})) [(['field'], 'Did not match Regexp(url)')]
-
class
dict_validator.fields.regexp.
Name
(lowercase_allowed=False, **kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Human name represented with ASCII characters - e.g. John Smith
Parameters: lowercase_allowed – if True - the name may contain lowercase parts >>> from dict_validator import validate
>>> class Schema: ... field = Name()
Expects one or more name parts delimited with space
>>> list(validate(Schema, {"field": 'John Smith'})) []
Regexp values are allowed as well
>>> list(validate(Schema, {"field": 'John Smith Äjil'})) []
>>> list(validate(Schema, {"field": 'John??Smith!!'})) [(['field'], 'Did not match Regexp(name)')]
Digits are not allowed
>>> list(validate(Schema, {"field": 'John Smith022'})) [(['field'], "Name can't contain digits or underscores")]
Underscores are not allowed either
>>> list(validate(Schema, {"field": 'John_Smith'})) [(['field'], "Name can't contain digits or underscores")]
By default each name part must be capitalized
>>> list(validate(Schema, {"field": 'John mcFault'})) [(['field'], 'One of the name parts is not capitalized')]
Non capitalized name parts can be enabled though
>>> class Schema: ... field = Name(lowercase_allowed=True)
>>> list(validate(Schema, {"field": 'John mcFault'})) []
-
class
dict_validator.fields.regexp.
Slug
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Lower case alphanumerics delimited with dashes.
>>> from dict_validator import validate
>>> class Schema: ... field = Slug()
>>> list(validate(Schema, {"field": 'title-of-web-page'})) []
Too many dashes
>>> list(validate(Schema, {"field": 'title--of-web-page'})) [(['field'], 'Did not match Regexp(slug)')]
Starts with a dash
>>> list(validate(Schema, {"field": '-title-of-web-page'})) [(['field'], 'Did not match Regexp(slug)')]
Ends with a dash
>>> list(validate(Schema, {"field": 'title-of-web-page-'})) [(['field'], 'Did not match Regexp(slug)')]
-
class
dict_validator.fields.regexp.
PascalCase
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
>>> from dict_validator import validate
>>> class Schema: ... field = PascalCase()
>>> list(validate(Schema, {"field": 'ValueCapitalized'})) []
Can contain digits
>>> list(validate(Schema, {"field": 'ValueCapitalizedWith012'})) []
Can’t contain non alphanumerics
>>> list(validate(Schema, {"field": 'Wrong_Value'})) [(['field'], 'Did not match Regexp(pascal-case)')]
Can’t start with an int
>>> list(validate(Schema, {"field": '012WrongValue'})) [(['field'], 'Did not match Regexp(pascal-case)')]
Cant’t start with a lowercase
>>> list(validate(Schema, {"field": 'wrongValue'})) [(['field'], 'Did not match Regexp(pascal-case)')]
Each part should be at least two symbols long (here - 1 - V)
>>> list(validate(Schema, {"field": 'WrongValueV'})) [(['field'], 'Did not match Regexp(pascal-case)')]
-
class
dict_validator.fields.regexp.
CamelCase
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
>>> from dict_validator import validate
>>> class Schema: ... field = CamelCase()
>>> list(validate(Schema, {"field": 'value'})) []
Can contain digits
>>> list(validate(Schema, {"field": 'valueCapitalizedWith012'})) []
Can’t contain non alphanumerics
>>> list(validate(Schema, {"field": 'wrong_Value'})) [(['field'], 'Did not match Regexp(camel-case)')]
Can’t start with an int
>>> list(validate(Schema, {"field": '012wrongValue'})) [(['field'], 'Did not match Regexp(camel-case)')]
Must start with a lowercase
>>> list(validate(Schema, {"field": 'WrongValue'})) [(['field'], 'Did not match Regexp(camel-case)')]
Each part should be at least two symbols long (here - 1 - V)
>>> list(validate(Schema, {"field": 'wrongValueV'})) [(['field'], 'Did not match Regexp(camel-case)')]
Even the first part should contain at least two symbols
>>> list(validate(Schema, {"field": 'wRongValueV'})) [(['field'], 'Did not match Regexp(camel-case)')]
-
class
dict_validator.fields.regexp.
UnderscoreCase
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
>>> from dict_validator import validate
>>> class Schema: ... field = UnderscoreCase()
>>> list(validate(Schema, {"field": 'value'})) []
>>> list(validate(Schema, {"field": 'value_with_parts'})) []
Can contain digits
>>> list(validate(Schema, {"field": 'value_with_12'})) []
Can’t contain upper case chars
>>> list(validate(Schema, {"field": 'wrong_Value'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Can’t start with an int
>>> list(validate(Schema, {"field": '012wrong_value'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Can’t start with an underscore
>>> list(validate(Schema, {"field": '_wrong_value'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Can’t end with an underscore
>>> list(validate(Schema, {"field": 'wrong_value_'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Each part should be at least two symbols long (here - 1 - V)
>>> list(validate(Schema, {"field": 'wrong_v'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Even the first part should contain at least two symbols
>>> list(validate(Schema, {"field": 'w_rong_value'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Submodules¶
-
class
dict_validator.fields.regexp.camel_case_field.
CamelCase
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
>>> from dict_validator import validate
>>> class Schema: ... field = CamelCase()
>>> list(validate(Schema, {"field": 'value'})) []
Can contain digits
>>> list(validate(Schema, {"field": 'valueCapitalizedWith012'})) []
Can’t contain non alphanumerics
>>> list(validate(Schema, {"field": 'wrong_Value'})) [(['field'], 'Did not match Regexp(camel-case)')]
Can’t start with an int
>>> list(validate(Schema, {"field": '012wrongValue'})) [(['field'], 'Did not match Regexp(camel-case)')]
Must start with a lowercase
>>> list(validate(Schema, {"field": 'WrongValue'})) [(['field'], 'Did not match Regexp(camel-case)')]
Each part should be at least two symbols long (here - 1 - V)
>>> list(validate(Schema, {"field": 'wrongValueV'})) [(['field'], 'Did not match Regexp(camel-case)')]
Even the first part should contain at least two symbols
>>> list(validate(Schema, {"field": 'wRongValueV'})) [(['field'], 'Did not match Regexp(camel-case)')]
-
class
dict_validator.fields.regexp.email_field.
Email
(domain=None, **kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Make sure that the input is a valid email.
Parameters: domain – string representing a desired domain name. e.g. “gmail.com” if not present matches any domain name >>> from dict_validator import validate, deserialize
>>> class Schema: ... field = Email()
>>> list(validate(Schema, {"field": "test@example.com"})) []
>>> list(validate(Schema, {"field": "test.foo@example.bla.com"})) []
>>> list(validate(Schema, {"field": "test123@examp123e.com"})) []
>>> list(validate(Schema, {"field": "test-dff@example-ff.com"})) []
>>> list(validate(Schema, {"field": "test%%20dff@example-ff.com"})) []
>>> list(validate(Schema, {"field": "test+20dff@example-ff.com"})) []
Missing domain:
>>> list(validate(Schema, {"field": "test@"})) [(['field'], 'Did not match Regexp(email)')]
Missing beginning:
>>> list(validate(Schema, {"field": "@example-ff.com"})) [(['field'], 'Did not match Regexp(email)')]
Wrong beginning:
>>> list(validate(Schema, {"field": "~~~@example.bla.com"})) [(['field'], 'Did not match Regexp(email)')]
Wrong domain:
>>> list(validate(Schema, {"field": "test123@examp++e.com"})) [(['field'], 'Did not match Regexp(email)')]
No @ char:
>>> list(validate(Schema, {"field": "fdfdfdgdg"})) [(['field'], 'Did not match Regexp(email)')]
Specify a domain:
>>> class Schema: ... field = Email(domain="example.com")
>>> list(validate(Schema, {"field": "test@example.com"})) []
Wrong domain:
>>> list(validate(Schema, {"field": "test@not-example.com"})) [(['field'], 'Did not match Regexp(email)')]
>>> deserialize(Schema, {"field": "foobar@EXAMPLE.com"}).field 'foobar@example.com'
-
class
dict_validator.fields.regexp.name_field.
Name
(lowercase_allowed=False, **kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Human name represented with ASCII characters - e.g. John Smith
Parameters: lowercase_allowed – if True - the name may contain lowercase parts >>> from dict_validator import validate
>>> class Schema: ... field = Name()
Expects one or more name parts delimited with space
>>> list(validate(Schema, {"field": 'John Smith'})) []
Regexp values are allowed as well
>>> list(validate(Schema, {"field": 'John Smith Äjil'})) []
>>> list(validate(Schema, {"field": 'John??Smith!!'})) [(['field'], 'Did not match Regexp(name)')]
Digits are not allowed
>>> list(validate(Schema, {"field": 'John Smith022'})) [(['field'], "Name can't contain digits or underscores")]
Underscores are not allowed either
>>> list(validate(Schema, {"field": 'John_Smith'})) [(['field'], "Name can't contain digits or underscores")]
By default each name part must be capitalized
>>> list(validate(Schema, {"field": 'John mcFault'})) [(['field'], 'One of the name parts is not capitalized')]
Non capitalized name parts can be enabled though
>>> class Schema: ... field = Name(lowercase_allowed=True)
>>> list(validate(Schema, {"field": 'John mcFault'})) []
-
class
dict_validator.fields.regexp.pascal_case_field.
PascalCase
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
>>> from dict_validator import validate
>>> class Schema: ... field = PascalCase()
>>> list(validate(Schema, {"field": 'ValueCapitalized'})) []
Can contain digits
>>> list(validate(Schema, {"field": 'ValueCapitalizedWith012'})) []
Can’t contain non alphanumerics
>>> list(validate(Schema, {"field": 'Wrong_Value'})) [(['field'], 'Did not match Regexp(pascal-case)')]
Can’t start with an int
>>> list(validate(Schema, {"field": '012WrongValue'})) [(['field'], 'Did not match Regexp(pascal-case)')]
Cant’t start with a lowercase
>>> list(validate(Schema, {"field": 'wrongValue'})) [(['field'], 'Did not match Regexp(pascal-case)')]
Each part should be at least two symbols long (here - 1 - V)
>>> list(validate(Schema, {"field": 'WrongValueV'})) [(['field'], 'Did not match Regexp(pascal-case)')]
-
class
dict_validator.fields.regexp.phone_field.
Phone
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Make sure that the input is a valid phone number.
>>> from dict_validator import validate, deserialize
>>> class Schema: ... field = Phone()
>>> list(validate(Schema, {"field": '+358 807 12'})) []
Has to start with a +
>>> list(validate(Schema, {"field": '358 807 12'})) [(['field'], 'Did not match Regexp(phone)')]
>>> deserialize(Schema, {"field": '+358 807 12'}).field '+35880712'
-
class
dict_validator.fields.regexp.slug_field.
Slug
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Lower case alphanumerics delimited with dashes.
>>> from dict_validator import validate
>>> class Schema: ... field = Slug()
>>> list(validate(Schema, {"field": 'title-of-web-page'})) []
Too many dashes
>>> list(validate(Schema, {"field": 'title--of-web-page'})) [(['field'], 'Did not match Regexp(slug)')]
Starts with a dash
>>> list(validate(Schema, {"field": '-title-of-web-page'})) [(['field'], 'Did not match Regexp(slug)')]
Ends with a dash
>>> list(validate(Schema, {"field": 'title-of-web-page-'})) [(['field'], 'Did not match Regexp(slug)')]
-
class
dict_validator.fields.regexp.underscore_case_field.
UnderscoreCase
(**kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
>>> from dict_validator import validate
>>> class Schema: ... field = UnderscoreCase()
>>> list(validate(Schema, {"field": 'value'})) []
>>> list(validate(Schema, {"field": 'value_with_parts'})) []
Can contain digits
>>> list(validate(Schema, {"field": 'value_with_12'})) []
Can’t contain upper case chars
>>> list(validate(Schema, {"field": 'wrong_Value'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Can’t start with an int
>>> list(validate(Schema, {"field": '012wrong_value'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Can’t start with an underscore
>>> list(validate(Schema, {"field": '_wrong_value'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Can’t end with an underscore
>>> list(validate(Schema, {"field": 'wrong_value_'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Each part should be at least two symbols long (here - 1 - V)
>>> list(validate(Schema, {"field": 'wrong_v'})) [(['field'], 'Did not match Regexp(underscore-case)')]
Even the first part should contain at least two symbols
>>> list(validate(Schema, {"field": 'w_rong_value'})) [(['field'], 'Did not match Regexp(underscore-case)')]
-
class
dict_validator.fields.regexp.url_field.
Url
(protocol=None, domain=None, port=None, path=None, **kwargs)[source]¶ Bases:
dict_validator.fields.string_field.String
Simple pattern to match http or https URL.
Parameters: - protocol – optional protocol spec (defaults to http[s])
- domain – optional domain (defaults to a wildcard)
- port – optional port (defaults to any number from 0 to 65535)
- path – optional path (defaults to a wildcard)
>>> from dict_validator import validate
By default a wildcard URL is matched.
>>> class Schema: ... field = Url()
>>> list(validate(Schema, ... {"field": "http://www.example.com/path-to-resource" ... "?foo-bar=bar-foo&zoo=loo#fff-ggg"})) []
SSL:
>>> list(validate(Schema, ... {"field": "https://www.example.com?foo=bar#fff"})) []
With port:
>>> list(validate(Schema, ... {"field": "http://www.example.com:8080?foo=bar#fff"})) []
No protocol:
>>> list(validate(Schema, ... {"field": "www.example.com?foo=bar#fff"})) []
Wrong protocol:
>>> list(validate(Schema, ... {"field": "bla://www.example.com?foo=bar#fff"})) [(['field'], 'Did not match Regexp(url)')]
No domain:
>>> list(validate(Schema, ... {"field": "http://foo=bar#fff"})) [(['field'], 'Did not match Regexp(url)')]
It is possible to configure certain parts of the url to match specific values.
>>> class Schema: ... field = Url(protocol="ftp", domain="example.com", ... path="/foobar-zooloo", port=8080)
>>> list(validate(Schema, ... {"field": "ftp://example.com:8080/foobar-zooloo" ... "?foo-bar=bar-foo&zoo=loo#fff-ggg"})) []
Wrong protocol:
>>> list(validate(Schema, ... {"field": "http://example.com/foobar-zooloo?ffff#dffdf"})) [(['field'], 'Did not match Regexp(url)')]
Wrong domain:
>>> list(validate(Schema, ... {"field": "ftp://not-example.com/foobar-zooloo?ffff#dffdf"})) [(['field'], 'Did not match Regexp(url)')]
Wrong path:
>>> list(validate(Schema, ... {"field": "ftp://example.com/zooloo?ffff#dffdf"})) [(['field'], 'Did not match Regexp(url)')]
Wrong port:
>>> list(validate(Schema, ... {"field": "ftp://example.com:100000/foobar-zooloo?ffff#dffdf"})) [(['field'], 'Did not match Regexp(url)')]