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 python str with the parsed values on success.

Inheritance:

Inheritance diagram of parasite.string.String

optional() String[source]

Makes the value optional, when parsing with _find_and_parse(). Has no effect on parse(). Inverse of required().

Warning

This function has no effect if the value is parsed as a standalone value.

Returns:

The updated instance of the class.

Return type:

String

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 on parse(). Inverse of optional(). 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:

String

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 on parse(). Inverse of not_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:

String

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 on parse(). Default behavior. Inverse of nullable().

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:

String

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
trim() String[source]

Trims the value.

Returns:

modified instance

Return type:

String

to_lower() String[source]

Converts the value to lowercase.

Returns:

modified instance

Return type:

String

to_upper() String[source]

Converts the value to uppercase.

Returns:

modified instance

Return type:

String

transform_before_parse() String[source]

Transforms the value before parsing.

Returns:

modified instance

Return type:

String

min(value: int) String[source]

Sets the lower length limit for the string value.

Parameters:

value (int) – lower limit

Returns:

modified instance

Return type:

String

max(value: int) String[source]

Sets the upper length limit for the string value.

Parameters:

value (int) – upper limit

Returns:

modified instance

Return type:

String

length(value: int) String[source]

Sets the lower and upper limit for the value.

Parameters:

value (int) – length of the value

Returns:

modified instance

Return type:

String

not_empty() String[source]

Sets the lower limit for the value. The value has to be not-empty.

Returns:

modified instance

Return type:

String

starts_with(value: str) String[source]

Sets the value that the string must start with.

Parameters:

value (str) – string that the value must start with

Returns:

modified instance

Return type:

String

ends_with(value: str) String[source]

Sets the value that the string must end with.

Parameters:

value (str) – string that the value must end with

Returns:

modified instance

Return type:

String

contains(value: str) String[source]

Sets the value that the string must contain.

Parameters:

value (str) – string that the value must contain

Returns:

modified instance

Return type:

String

email() String[source]

Sets the regex type to email.

Returns:

modified instance

Return type:

String

url() String[source]

Sets the regex type to URL.

Returns:

modified instance

Return type:

String

uuid() String[source]

Sets the regex type to UUID.

Returns:

modified instance

Return type:

String

cuid() String[source]

Sets the regex type to CUID.

Returns:

modified instance

Return type:

String

cuid2() String[source]

Sets the regex type to CUID2.

Returns:

modified instance

Return type:

String

ulid() String[source]

Sets the regex type to ULID.

Returns:

modified instance

Return type:

String

ipv4() String[source]

Sets the regex type to IPv4.

Returns:

modified instance

Return type:

String

ipv6() String[source]

Sets the regex type to IPv6.

Returns:

modified instance

Return type:

String

regex(value: Pattern) String[source]

Sets the regex pattern to use for validation.

Parameters:

value (Pattern) – compiled regex pattern

Returns:

modified instance

Return type:

String

match(value: str) String[source]

Sets the regex pattern to use for validation.

Parameters:

value (str) – regex pattern

Returns:

modified instance

Return type:

String

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 a rusttypes.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]