View source on GitHub |
Regular expression validator.
Inherits From: Validator
, expected_type
google.appengine.api.validation.Regex(
regex, string_type=six.text_type, default=None
)
Regular expression validator always converts value to string. Note that matches must be exact. Partial matches will not validate. For example:
class ClassDescr(Validated): ATTRIBUTES = { 'name': Regex(r'[a-zA-Z_][a-zA-Z_0-9]*'), 'parent': Type(type)` }
Alternatively, any attribute that is defined as a string is automatically interpreted to be of type Regex. It is possible to specify unicode regex strings as well. This approach is slightly less efficient, but usually is not significant unless parsing large amounts of data:
class ClassDescr(Validated):
ATTRIBUTES = { 'name': r'[a-zA-Z_][a-zA-Z_0-9]*',
'parent': Type(type),
}
# This will raise a ValidationError exception.
my_class(name='AName with space', parent=AnotherClass)
Args | |
---|---|
regex
|
Regular expression string to use for comparison. |
string_type
|
Type to be considered a string. |
default
|
Default value. |
Raises | |
---|---|
AttributeDefinitionError
|
if string_type is not a kind of string.
|
Child Classes
Methods
CheckFieldInitialized
CheckFieldInitialized(
value, key, obj
)
Check for missing fields or conflicts between fields.
Default behavior performs a simple None
-check, but this can be overridden.
If the intent is to allow optional fields, then use the Optional
validator
instead.
Args | |
---|---|
value
|
Value to validate. |
key
|
Name of the field being validated. |
obj
|
The object to validate against. |
Raises | |
---|---|
ValidationError
|
When there are missing or conflicting fields. |
GetWarnings
GetWarnings(
value, key, obj
)
Return any warnings on this attribute.
Validates the value with an eye towards things that aren't fatal problems.
Args | |
---|---|
value
|
Value to validate. |
key
|
Name of the field being validated. |
obj
|
The object to validate against. |
Returns | |
---|---|
A list of tuples (context, warning) where
|
ToValue
ToValue(
value
)
Convert value
to a simplified collection or basic type.
Subclasses of Validator
should override this method when the dumped
representation of value
is not a simple <type>(value)
(e.g., a regex).
Args | |
---|---|
value
|
An object of the same type that was returned from Validate() .
|
Returns | |
---|---|
An instance of a builtin type (e.g., int, str, dict, etc). By default
it returns value unmodified.
|
Validate
Validate(
value, key
)
Does validation of a string against a regular expression.
Args | |
---|---|
value
|
String to match against regular expression. |
key
|
Name of the field being validated. |
Raises | |
---|---|
ValidationError
|
When value does not match regular expression or when value does not match provided string type. |
__call__
__call__(
value, key='???'
)
Main interface to validator is call mechanism.