Latest Version: 0.9.6.2

FancyValidator

FancyValidator is the (abstract) superclass for various validators and converters. A subclass can validate, convert, or do both. There is no formal distinction made here.

Validators have two important external methods:

  • .to_python(value, state): Attempts to convert the value. If there is a problem, or the value is not valid, an Invalid exception is raised. The argument for this exception is the (potentially HTML-formatted) error message to give the user.
  • .from_python(value, state): Reverses to_python.

There are five important methods for subclasses to override, however none of these have to be overridden, only the ones that are appropriate for the validator:

  • __init__(): if the declarative.Declarative model doesn't work for this.
  • .validate_python(value, state): This should raise an error if necessary. The value is a Python object, either the result of to_python, or the input to from_python.
  • .validate_other(value, state): Validates the source, before to_python, or after from_python. It's more common to use .validate_python() however.
  • ._to_python(value, state): This returns the converted value, or raises an Invalid exception if there is an error. The argument to this exception should be the error message.
  • ._from_python(value, state): Should undo .to_python() in some reasonable way, returning a string.

Validators should have no internal state besides the values given at instantiation. They should be reusable and reentrant.

All subclasses can take the arguments/instance variables:

  • if_empty: If set, then this value will be returned if the input evaluates to false (empty list, empty string, None, etc), but not the 0 or False objects. This only applies to .to_python().
  • not_empty: If true, then if an empty value is given raise an error. (Both with .to_python() and also .from_python() if .validate_python is true).
  • strip: If true and the input is a string, strip it (occurs before empty tests).
  • if_invalid: If set, then when this validator would raise Invalid during .to_python(), instead return this value.
  • if_invalid_python: If set, when the Python value (converted with .from_python()) is invalid, this value will be returned.
  • accept_python: If True (the default), then .validate_python() and .validate_other() will not be called when .from_python() is used.

Messages

badType:
The input must be a string (not a %(type)s: %(value)r)
empty:
Please enter a value
noneType:
The input must be a string (not None)

Attributes

a __singletonmethods__

('to_python', 'from_python')

a accept_python

True

a compound

False

a not_empty

False

a repeating

False

a strip

False

Methods

f __call__(self, *args, **kw) ...

f __classinit__(cls, new_attrs) ...

f __classsourcerepr__(cls, source, binding=None) ...

f __init__(self, *args, **kw) ...

f __initargs__(self, new_attrs) ...

f __sourcerepr__(self, source, binding=None) ...

f all_messages(self) ...

Return a dictionary of all the messages of this validator, and any subvalidators if present. Keys are message names, values may be a message or list of messages. This is really just intended for documentation purposes, to show someone all the messages that a validator or compound validator (like Schemas) can produce.

@@: Should this produce a more structured set of messages, so that messages could be unpacked into a rendered form to see the placement of all the messages? Well, probably so.

f assert_string(self, value, state) ...

f base64encode(self, value) ...

Encode a string in base64, stripping whitespace and removing newlines.

f empty_value(self, value) ...

f from_python(self, value, state=None) ...

f is_empty(self, value) ...

f message(self, msgName, state, **kw) ...

f subvalidators(self) ...

Return any validators that this validator contains. This is not useful for functional, except to inspect what values are available. Specifically the .all_messages() method uses this to accumulate all possible messages.

f to_python(self, value, state=None) ...

f validate_other(self, value, state) ...

A validation method that doesn't do anything.

f validate_python(self, value, state) ...

A validation method that doesn't do anything.

See the source for more information.

Top