![]() |
Verifies property is of expected type.
Inherits From: Validator
, expected_type
google.appengine.api.validation.Type(
expected_type, convert=True, default=None
)
Can optionally convert value if it is not of the expected type.
It is possible to specify a required field of a specific type in shorthand by merely providing the type. This method is slightly less efficient than providing an explicit type but is not significant unless parsing a large amount of information:
class Person(Validated):
ATTRIBUTES = {'name': unicode,
'age': int,
}
However, in most instances it is best to use the type constants:
class Person(Validated):
ATTRIBUTES = {'name': TypeUnicode,
'age': TypeInt,
}
Args | |
---|---|
expected_type
|
Type that attribute should validate against. |
convert
|
Cause conversion if value is not the right type. Conversion is done by calling the constructor of the type with the value as its first parameter. |
default
|
Default assignment is made during initialization and will not pass through validation. |
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
)
Validate that value
has the correct type.
Args | |
---|---|
value
|
Value to validate. |
key
|
Name of the field being validated. |
Returns | |
---|---|
value if value is of the correct type. value is coverted to the
correct type if the Validator is configured to do so.
|
Raises | |
---|---|
ValidationError
|
If value is not of the right type and the validator
is either configured not to convert or cannot convert.
|
__call__
__call__(
value, key='???'
)
Main interface to validator is call mechanism.