API documentation¶
Configuring Flask-Validation¶
-
class
flask_validation.validator.
Validator
(app=None)[source]¶ Bases:
object
Create the Validator instance to register config. You can either pass a flask application in directly here to register this extension with the flask app, or call init_app after creating this object (in a factory pattern). :param app: A flask application
Decorators¶
-
flask_validation.decorators.
json_required
()[source]¶ A decorator to check header type is
application/json
if you decorate endpoint with this, it will ensure that the request has a valid payload type before access endpoint if header’s content type is not
application/json
, abort theinvalid_content_type_abort_code
-
flask_validation.decorators.
validate_common
(key_type_mapping: dict)[source]¶ A decorator to check request payload keys and type
If the request payload does not include the key in
key_type_mapping
, abort thekey_missing_code
, and if the type is not correct, abort theinvalid_type_code
.Nested JSON processing is possible by inserting the dictionary in the
required_keys
like this{'a': str, 'b': int, 'c': {'d': int, 'e': str}}
Parameters: key_type_mapping – A dictionary for payload check with this form {<key name>: <type class>}
-
flask_validation.decorators.
validate_keys
(required_keys)[source]¶ A decorator to check request payload keys
if you decorate endpoint with this, it will ensure that the request’s json body includes ‘required_keys’. if request body didn’t includes
required_keys
, abort withkey_missing_abort_code
Nested JSON processing is possible by inserting the dictionary in the
required_keys
like this['a', 'b', {'c': ['q' ,'z']}]
Parameters: required_keys – key list to check request body’s JSON
-
flask_validation.decorators.
validate_with_fields
(key_field_mapping: dict)[source]¶ A decorator to check request payload with Field classes in fields.py
If the request payload does not include the key in key_type_mapping, abort
key_missing_code
and abortvalidation_failure_code
if field validation fails.Nested JSON processing is possible by inserting the dictionary in the
required_keys
like this{'a': StringField(allow_empty=False), 'b': IntField(min_value=0), 'c': {'d': BooleanField()}}
Parameters: key_field_mapping – A dictionary for payload check with this form {<key name>: <field class>}
Fields¶
-
class
flask_validation.fields.
BooleanField
(validator_function=None, enum=None, required: bool = True, allow_null: bool = False)[source]¶ Bases:
flask_validation.fields._BaseField
Boolean field class
-
class
flask_validation.fields.
FloatField
(min_value=None, max_value=None, **kwargs)[source]¶ Bases:
flask_validation.fields.NumberField
Float field class
-
class
flask_validation.fields.
IntField
(min_value=None, max_value=None, **kwargs)[source]¶ Bases:
flask_validation.fields.NumberField
Int field class
-
class
flask_validation.fields.
ListField
(min_length: int = None, max_length: int = None, **kwargs)[source]¶ Bases:
flask_validation.fields._BaseField
List field class