Available validators

pycerberus contains some basic validators already. You can use them as they are or use them as a basis for more specialized validators. Below you find a list of all included validators.

class pycerberus.validators.basic_numbers.IntegerValidator(self)

Bases: pycerberus.api.Validator

convert(value, context)

Convert the input value to a suitable Python instance which is returned. If the input is invalid, raise an InvalidDataError.

is_empty(value, context)

Decide if the value is considered an empty value.

keys()

Return all keys defined by this specific validator class.

message_for_key(key, context)

Return a message for a specific key. Implement this method if you want to avoid calls to messages() which might be costly (otherwise implementing this method is optional).

messages()

Return all messages which are defined by this validator as a key/message dictionary. Alternatively you can create a class-level dictionary which contains these keys/messages.

You must declare all your messages here so that all keys are known after this method was called.

Calling this method might be costly when you have a lot of messages and returning them is expensive. You can reduce the overhead in some situations by implementing message_for_key()

revert_conversion(value, context=None)

Undo the conversion of process() and return a “string-like” representation. This method is especially useful for widget libraries like ToscaWigets so they can render Python data types in a human readable way. The returned value does not have to be an actual Python string as long as it has a meaningful unicode() result. Generally the validator should accept the return value in its ‘.process()’ method.

validate(value, context)

Perform additional checks on the value which was processed successfully before (otherwise this method is not called). Raise an InvalidDataError if the input data is invalid.

You can implement only this method in your validator if you just want to add additional restrictions without touching the actual conversion.

This method must not modify the converted_value.

class pycerberus.validators.domain.DomainNameValidator(self)

Bases: pycerberus.validators.string.StringValidator

A validator to check if an domain name is syntactically correct.

keys()

Return all keys defined by this specific validator class.

message_for_key(key, context)

Return a message for a specific key. Implement this method if you want to avoid calls to messages() which might be costly (otherwise implementing this method is optional).

messages()

Return all messages which are defined by this validator as a key/message dictionary. Alternatively you can create a class-level dictionary which contains these keys/messages.

You must declare all your messages here so that all keys are known after this method was called.

Calling this method might be costly when you have a lot of messages and returning them is expensive. You can reduce the overhead in some situations by implementing message_for_key()

validate(value, context)

Perform additional checks on the value which was processed successfully before (otherwise this method is not called). Raise an InvalidDataError if the input data is invalid.

You can implement only this method in your validator if you just want to add additional restrictions without touching the actual conversion.

This method must not modify the converted_value.

class pycerberus.validators.email.EmailAddressValidator(self)

Bases: pycerberus.validators.domain.DomainNameValidator

A validator to check if an email address is syntactically correct.

Please note that there is no clear definition of an ‘email address’. Some parts are defined in consecutive RFCs, there is a notion of ‘string that is accepted by a MTA’ and last but not least a fuzzy ‘general expectation’ what an email address should be about.

Therefore this validator is currently extremly simple and does not handle internationalized local parts/domains.

For the future I envision some extensions here:
  • support internationalized domain names (possibly also encode to/ decode from idna) if specified by flag
  • More flexible structure if there must be a second-level domain
Something that should not happen in this validator:
  • Open SMTP connections to check if an account exists
  • specify default domains if missing

These things can be implemented in derived validators

keys()

Return all keys defined by this specific validator class.

message_for_key(key, context)

Return a message for a specific key. Implement this method if you want to avoid calls to messages() which might be costly (otherwise implementing this method is optional).

messages()

Return all messages which are defined by this validator as a key/message dictionary. Alternatively you can create a class-level dictionary which contains these keys/messages.

You must declare all your messages here so that all keys are known after this method was called.

Calling this method might be costly when you have a lot of messages and returning them is expensive. You can reduce the overhead in some situations by implementing message_for_key()

validate(emailaddress, context)

Perform additional checks on the value which was processed successfully before (otherwise this method is not called). Raise an InvalidDataError if the input data is invalid.

You can implement only this method in your validator if you just want to add additional restrictions without touching the actual conversion.

This method must not modify the converted_value.

class pycerberus.validators.foreach.ForEach(self)

Bases: pycerberus.api.Validator

Apply a validator to every item of an iterable (like map). Also you can specify the allowed min/max number of items in that iterable.

convert(values, context)

Convert the input value to a suitable Python instance which is returned. If the input is invalid, raise an InvalidDataError.

handle_validator_result(converted_value, result, context, errors=None, nr_new_errors=None)
keys()

Return all keys defined by this specific validator class.

message_for_key(key, context)

Return a message for a specific key. Implement this method if you want to avoid calls to messages() which might be costly (otherwise implementing this method is optional).

messages()

Return all messages which are defined by this validator as a key/message dictionary. Alternatively you can create a class-level dictionary which contains these keys/messages.

You must declare all your messages here so that all keys are known after this method was called.

Calling this method might be costly when you have a lot of messages and returning them is expensive. You can reduce the overhead in some situations by implementing message_for_key()

new_result(initial_value)
class pycerberus.validators.string.StringValidator(self)

Bases: pycerberus.api.Validator

convert(value, context)

Convert the input value to a suitable Python instance which is returned. If the input is invalid, raise an InvalidDataError.

is_empty(value, context)

Decide if the value is considered an empty value.

keys()

Return all keys defined by this specific validator class.

message_for_key(key, context)

Return a message for a specific key. Implement this method if you want to avoid calls to messages() which might be costly (otherwise implementing this method is optional).

messages()

Return all messages which are defined by this validator as a key/message dictionary. Alternatively you can create a class-level dictionary which contains these keys/messages.

You must declare all your messages here so that all keys are known after this method was called.

Calling this method might be costly when you have a lot of messages and returning them is expensive. You can reduce the overhead in some situations by implementing message_for_key()

validate(value, context)

Perform additional checks on the value which was processed successfully before (otherwise this method is not called). Raise an InvalidDataError if the input data is invalid.

You can implement only this method in your validator if you just want to add additional restrictions without touching the actual conversion.

This method must not modify the converted_value.