FlyForms reference¶
Forms¶
Form
is the root class of FlyForms that provide the highest level API for
validation and mapping of data structures. All user-defined forms must inherit this class.
Form class¶
-
class
flyforms.form.
Form
[source]¶ Base class for all user-defined Forms
Construction
-
__init__
(**data)[source]¶ Parameters: data (dict) – additional data to form When a Form is instantiated you can access given data via instance attributes or get everything at once using
form.data
Note
Even if you do not pass some keys in
kwargs
, you can use instance attributes, but will receiveUNSET
valueProperties
-
is_bound
¶ Checks is Form instance bound. Returns True if all fields is bound. Otherwise, False.
-
is_valid
¶ Checks is Form instance valid. Returns there are no errors. Otherwise, False.
-
data
¶ Returns
dict
with bound data (if you need all data evenUNSET
values - useform.raw_data
)
Attributes
-
_fields
¶ Python
set
contains all defined fields names
-
Defining Forms¶
Forms defining is quite simply process. All you need to do is to make a subclass of Form
and define fields as class attributes.
If you need to extend Forms, inheritance is available. New Form will contain all fields of the parent form as well as it’s own.
Using Forms¶
Todo
Write usage
In flight data validation¶
If you already have defined Form and you want to just validate some data structure via it you can use
validate_schema()
function from flyforms.form
module.
Usage¶
Todo
Write usage
Signature for validate_schema¶
-
flyforms.form.
validate_schema
(form_cls, **data)[source]¶ This function validates given data via given Form subclass without defined Form instantiation.
Parameters: - form_cls (Form subclass) – defined Form class
- data – data to validate
Returns: boolean flag is data valid for given
form_cls
Raises TypeError: if given
form_cls
is not class or notForm
subclass
Fields¶
Fields represent a set of rules to data validation via set of Validator
instances. When you define yours
custom Form
you define Fields as its class attributes.
This is the most common usage of Fields, but nothing prevents you to use them standalone.
Field class¶
-
class
flyforms.fields.
Field
[source]¶ Construction
-
__init__
(required=True, choices=(), validators=(), **kwargs)[source]¶ Parameters: - required (bool) – boolean flag is this field required or can be empty
- choices (iterable) – iterable object contains possible values of this field
- validators (list of callable) – the additional validators for field
- default (instance of value_types) – the default value of the field
Raises TypeError: if passed arguments invalid
Methods
-
validate
(value)[source]¶ Validates given value via defined set of
Validators
Parameters: value – the value to validate
-
is_valid
(value)[source]¶ The ‘silent’ variant of value validation.
Parameters: value – the value to validate Returns: True if given value is valid, otherwise - False
Attributes
-
required
¶ Boolean flag passed to constructor
-
base_validators
¶ List of attached by processing construction arguments
Validators
-
custom_validators
¶ Iterable object contains
custom_validators
passed to constructor
Property
-
validators
¶ Chain, contains
base_validators
andcustom_validators
-
UNSET value¶
-
flyforms.fields.
UNSET
¶ frequently used constant, it is a reflection of unidentified values
Basic Fields¶
StringField¶
-
class
flyforms.fields.
StringField
(min_length=None, max_length=None, regex='', **kwargs)[source]¶ Reflects Python strings
-
__init__
(min_length=None, max_length=None, regex='', **kwargs)[source]¶ Parameters: - required (bool) – boolean flag is this field required
- min_length (int or None) – the minimum length of the string
- max_length (int or None) – the maximum length of the string
- regex (str or regexp) – the regular expression to validate
- choices (iterable) – iterable object contains possible values of this field
- validators (list of callable) – the additional validators for field
- default (instance of value_types) – the default value of the field
-
EmailField¶
-
class
flyforms.fields.
EmailField
(**kwargs)[source]¶ Reflects Python string corresponding to an email
-
__init__
(**kwargs)[source]¶ Parameters: - required (bool) – boolean flag is this field required
- choices (iterable) – iterable object contains possible values of this field
- validators (list of callable) – the additional validators for field
- default (instance of value_types) – the default value of the field
-
IntField¶
-
class
flyforms.fields.
IntField
(min_value=None, max_value=None, **kwargs)[source]¶ Reflects Python integer (
int
)-
__init__
(min_value=None, max_value=None, **kwargs)[source]¶ Parameters: - required (bool) – boolean flag is this field required
- min_value – the minimum valid value
- max_value – the maximum valid value
- choices (iterable) – iterable object contains possible values of this field
- validators (list of callable) – the additional validators for field
- default (instance of value_types) – the default value of the field
-
FloatField¶
-
class
flyforms.fields.
FloatField
(min_value=None, max_value=None, **kwargs)[source]¶ Reflects Python float (
float
)-
__init__
(min_value=None, max_value=None, **kwargs)¶ Parameters: - required (bool) – boolean flag is this field required
- min_value – the minimum valid value
- max_value – the maximum valid value
- choices (iterable) – iterable object contains possible values of this field
- validators (list of callable) – the additional validators for field
- default (instance of value_types) – the default value of the field
-
Ip4Field¶
-
class
flyforms.fields.
Ip4Field
(**kwargs)[source]¶ Reflects Python string corresponding to an IPv4 address
-
__init__
(**kwargs)[source]¶ Parameters: - required (bool) – boolean flag is this field required
- choices (iterable) – iterable object contains possible values of this field
- validators (list of callable) – the additional validators for field
- default (instance of value_types) – the default value of the field
-
Validators¶
Validators validate given value via their internal logic. If value is invalid ValidationError
will be raised.
ValidationError¶
Builtin validators¶
RequiredValidator¶
TypedValidator¶
EntryValidator¶
MinValueValidator¶
MaxValueValidator¶
MinLengthValidator¶
MaxLengthValidator¶
RegexValidator¶
EmailValidator¶
Custom Validators¶
If you need to make additional data validation you can use your custom validators.
There is one requirement: validator should be an initialized and callable object.
If you want, you can use and extend one of this classes: Validator
or SimpleValidator
.