parasite.string
¶
Brief¶
Reference for the string
submodule of the parasite
package. This submodule
contains the parasite.string.String
class, which is a generic container for a string
Python object.
Usage¶
from parasite import p
schema = p.string().min(3).max(10)
schema.parse("hello") # -> "hello"
...
Member Reference¶
- class parasite.string.K¶
Template type for the key in a dictionary.
alias of TypeVar(‘K’)
- class parasite.string.String[source]¶
parasite
type for creating and parsing string based schemas. Will return a pythonstr
with the parsed values on success.- Inheritance:
- optional() String [source]¶
Makes the value optional, when parsing with
_find_and_parse()
. Has no effect onparse()
. Inverse ofrequired()
.Warning
This function has no effect if the value is parsed as a standalone value.
- Returns:
The updated instance of the class.
- Return type:
- Example usage:
Lets assume we have the following schemas:
from parasite import p schema = p.obj({ "name": p.string().optional() }) schema2 = p.obj({ "name": p.string() })
The resulting schemas will parse the following objects:
>>> schema.parse({ "name": "John" }) { "name": "John" } >>> schema.parse({ }) { } >>> schema2.parse({ "name": "John" }) { "name": "John" } >>> schema2.parse({ }) ValidationError: key "name" not found, but is required
- required() String [source]¶
Makes the value required, when parsing with
_find_and_parse()
. Has no effect onparse()
. Inverse ofoptional()
. Default behavior.Note
This function is default behavior for the class and therefore only has an effect if the function
optional()
may have been called before.Warning
This function has no effect if the value is parsed as a standalone value.
- Returns:
The updated instance of the class.
- Return type:
- Example usage:
Lets assume we have the following schemas:
from parasite import p schema = p.obj({ "name": p.string().optional().required() }) schema2 = p.obj({ "name": p.string() })
The resulting schemas will parse the following objects:
>>> schema.parse({ "name": "John" }) { "name": "John" } >>> schema.parse({ }) ValidationError: key "name" not found, but is required >>> schema2.parse({ "name": "John" }) { "name": "John" } >>> schema2.parse({ }) ValidationError: key "name" not found, but is required
- nullable() String [source]¶
Makes the value nullable, when parsing with
_find_and_parse()
. Has no effect onparse()
. Inverse ofnot_nullable()
.Warning
This function has no effect if the value is parsed as a standalone value.
- Returns:
The updated instance of the class.
- Return type:
- Example usage:
Lets assume we have the following schemas:
from parasite import p schema = p.obj({ "name": p.string().nullable() }) schema2 = p.obj({ "name": p.string() })
The resulting schemas will parse the following objects:
>>> schema.parse({ "name": "John" }) { "name": "John" } >>> schema.parse({ "name": None }) { "name": None } >>> schema2.parse({ "name": "John" }) { "name": "John" } >>> schema2.parse({ "name": None }) ValidationError: key "name" is not nullable, but is None
- not_nullable() String [source]¶
Makes the value not-nullable, when parsing with
_find_and_parse()
. Has no effect onparse()
. Default behavior. Inverse ofnullable()
.Note
This function is default behavior for the class and therefore only has an effect if the function
nullable()
may have been called before.Warning
This function has no effect if the value is parsed as a standalone value.
- Returns:
The updated instance of the class.
- Return type:
- Example usage:
Lets assume we have the following schemas:
from parasite import p schema = p.obj({ "name": p.string().nullable().not_nullable() }) schema2 = p.obj({ "name": p.string() })
The resulting schemas will parse the following objects:
>>> schema.parse({ "name": "John" }) { "name": "John" } >>> schema.parse({ "name": None }) ValidationError: key "name" is not nullable, but is None >>> schema2.parse({ "name": "John" }) { "name": "John" } >>> schema2.parse({ "name": None }) ValidationError: key "name" is not nullable, but is None
- to_lower() String [source]¶
Converts the value to lowercase.
- Returns:
modified instance
- Return type:
- to_upper() String [source]¶
Converts the value to uppercase.
- Returns:
modified instance
- Return type:
- transform_before_parse() String [source]¶
Transforms the value before parsing.
- Returns:
modified instance
- Return type:
- not_empty() String [source]¶
Sets the lower limit for the value. The value has to be not-empty.
- Returns:
modified instance
- Return type:
- regex(value: Pattern) String [source]¶
Sets the regex pattern to use for validation.
- Parameters:
value (Pattern) – compiled regex pattern
- Returns:
modified instance
- Return type:
- parse(obj: Any) str [source]¶
Default method for parsing a value. This method should be overridden by subclasses.
- Throws:
ValidationError: if the value could not be parsed or was invalid
- Parameters:
obj (Any) – value to parse
- Returns:
parsed destination value
- Return type:
T
- parse_safe(obj: Any) Result[T, ValidationError] ¶
Converts the result of
parse()
into arusttypes.result.Result
type. Should be used when safe parsing is required.Note
Will only catch
parasite.errors.ValidationError
exceptions!!!- Parameters:
obj (Any) – value to parse
- Returns:
parsed destination value or an error
- Return type:
Result[T, ValidationError]