|
| 1 | +Declarative API |
| 2 | +=============== |
| 3 | + |
| 4 | +Overview |
| 5 | +-------- |
| 6 | + |
| 7 | +The Declarative API is the modern, recommended way to define InputFilters in flask-inputfilter. |
| 8 | +It uses Python decorators and class-level declarations to create clean, readable, and maintainable |
| 9 | +input validation definitions. |
| 10 | + |
| 11 | +Key Features |
| 12 | +------------ |
| 13 | + |
| 14 | +- **Clean Syntax**: Define fields, conditions, and global components directly in class definition |
| 15 | +- **Type Safety**: Integrates well with type hints and IDEs |
| 16 | +- **Inheritance Support**: Full support for class inheritance and MRO |
| 17 | +- **Model Integration**: Automatic serialization to dataclasses, Pydantic models, and more |
| 18 | +- **Multiple Element Support**: Register multiple components at once for concise definitions |
| 19 | + |
| 20 | +Core Components |
| 21 | +--------------- |
| 22 | + |
| 23 | +The Declarative API consists of four main decorators: |
| 24 | + |
| 25 | ++----------------------+--------------------------------------------+ |
| 26 | +| Decorator | Purpose | |
| 27 | ++======================+============================================+ |
| 28 | +| ``field()`` | Define individual input fields | |
| 29 | ++----------------------+--------------------------------------------+ |
| 30 | +| ``condition()`` | Add cross-field validation conditions | |
| 31 | ++----------------------+--------------------------------------------+ |
| 32 | +| ``global_filter()`` | Add filters applied to all fields | |
| 33 | ++----------------------+--------------------------------------------+ |
| 34 | +| ``global_validator()``| Add validators applied to all fields | |
| 35 | ++----------------------+--------------------------------------------+ |
| 36 | +| ``model()`` | Associate with a model class | |
| 37 | ++----------------------+--------------------------------------------+ |
| 38 | + |
| 39 | +Quick Example |
| 40 | +------------- |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + from flask_inputfilter import InputFilter |
| 45 | + from flask_inputfilter.declarative import field, condition, global_filter, global_validator, model |
| 46 | + from flask_inputfilter.filters import StringTrimFilter, ToLowerFilter |
| 47 | + from flask_inputfilter.validators import IsStringValidator, LengthValidator, EmailValidator |
| 48 | + from flask_inputfilter.conditions import EqualCondition |
| 49 | + from dataclasses import dataclass |
| 50 | +
|
| 51 | + @dataclass |
| 52 | + class User: |
| 53 | + username: str |
| 54 | + email: str |
| 55 | + password: str |
| 56 | +
|
| 57 | + class UserRegistrationFilter(InputFilter): |
| 58 | + # Field definitions with individual configuration |
| 59 | + username = field( |
| 60 | + required=True, |
| 61 | + validators=[LengthValidator(min_length=3, max_length=20)] |
| 62 | + ) |
| 63 | +
|
| 64 | + email = field( |
| 65 | + required=True, |
| 66 | + validators=[EmailValidator()] |
| 67 | + ) |
| 68 | +
|
| 69 | + password = field(required=True, validators=[LengthValidator(min_length=8)]) |
| 70 | + password_confirmation = field(required=True) |
| 71 | +
|
| 72 | + # Global components - applied to all fields |
| 73 | + global_filter(StringTrimFilter(), ToLowerFilter()) |
| 74 | + global_validator(IsStringValidator()) |
| 75 | +
|
| 76 | + # Cross-field validation |
| 77 | + condition(EqualCondition('password', 'password_confirmation')) |
| 78 | +
|
| 79 | + # Model association |
| 80 | + model(User) |
| 81 | +
|
| 82 | + Declarative API |
| 83 | +---------------- |
| 84 | + |
| 85 | +.. code-block:: python |
| 86 | +
|
| 87 | + class MyFilter(InputFilter): |
| 88 | + name = field(required=True, validators=[IsStringValidator()]) |
| 89 | + email = field(required=True, validators=[EmailValidator()]) |
| 90 | +
|
| 91 | + global_filter(StringTrimFilter()) |
| 92 | + condition(RequiredCondition('name')) |
| 93 | +
|
| 94 | +Inheritance and MRO |
| 95 | +------------------- |
| 96 | + |
| 97 | +The Declarative API fully supports Python's inheritance and Method Resolution Order (MRO): |
| 98 | + |
| 99 | +.. code-block:: python |
| 100 | +
|
| 101 | + class BaseUserFilter(InputFilter): |
| 102 | + # Base fields |
| 103 | + name = field(required=True, validators=[IsStringValidator()]) |
| 104 | +
|
| 105 | + # Base global components |
| 106 | + global_filter(StringTrimFilter()) |
| 107 | +
|
| 108 | + class ExtendedUserFilter(BaseUserFilter): |
| 109 | + # Additional fields |
| 110 | + email = field(required=True, validators=[EmailValidator()]) |
| 111 | + age = field(required=False, validators=[IsIntegerValidator()]) |
| 112 | +
|
| 113 | + # Additional global components (inherited ones are preserved) |
| 114 | + global_validator(LengthValidator(min_length=1)) |
| 115 | +
|
| 116 | + # Conditions |
| 117 | + condition(RequiredCondition('email')) |
| 118 | +
|
| 119 | +Field Override |
| 120 | +~~~~~~~~~~~~~~ |
| 121 | + |
| 122 | +You can override fields from parent classes: |
| 123 | + |
| 124 | +.. code-block:: python |
| 125 | +
|
| 126 | + class BaseFilter(InputFilter): |
| 127 | + name = field(required=False) # Optional in base |
| 128 | +
|
| 129 | + class StrictFilter(BaseFilter): |
| 130 | + name = field(required=True, validators=[LengthValidator(min_length=2)]) # Override |
| 131 | +
|
| 132 | +Multiple Element Registration |
| 133 | +----------------------------- |
| 134 | + |
| 135 | +You can register multiple components at once for cleaner definitions: |
| 136 | + |
| 137 | +.. code-block:: python |
| 138 | +
|
| 139 | + class CompactFilter(InputFilter): |
| 140 | + name = field(required=True) |
| 141 | + email = field(required=True) |
| 142 | +
|
| 143 | + # Multiple global filters |
| 144 | + global_filter(StringTrimFilter(), ToLowerFilter(), RemoveExtraSpacesFilter()) |
| 145 | +
|
| 146 | + # Multiple global validators |
| 147 | + global_validator(IsStringValidator(), NotEmptyValidator()) |
| 148 | +
|
| 149 | + # Multiple conditions |
| 150 | + condition( |
| 151 | + RequiredCondition('name'), |
| 152 | + RequiredCondition('email'), |
| 153 | + EqualCondition('password', 'password_confirmation') |
| 154 | + ) |
| 155 | +
|
| 156 | +Model Integration |
| 157 | +----------------- |
| 158 | + |
| 159 | +The Declarative API seamlessly integrates with various model types: |
| 160 | + |
| 161 | +Dataclasses |
| 162 | +~~~~~~~~~~~ |
| 163 | + |
| 164 | +.. code-block:: python |
| 165 | +
|
| 166 | + from dataclasses import dataclass |
| 167 | +
|
| 168 | + @dataclass |
| 169 | + class User: |
| 170 | + name: str |
| 171 | + email: str |
| 172 | +
|
| 173 | + class UserFilter(InputFilter): |
| 174 | + name = field(required=True, validators=[IsStringValidator()]) |
| 175 | + email = field(required=True, validators=[EmailValidator()]) |
| 176 | +
|
| 177 | + model(User) |
| 178 | +
|
| 179 | + # Usage |
| 180 | + filter_instance = UserFilter() |
| 181 | + user = filter_instance.validate_data({'name': 'John', 'email': '[email protected]'}) |
| 182 | + # user is a User dataclass instance |
| 183 | +
|
| 184 | +Pydantic Models |
| 185 | +~~~~~~~~~~~~~~~ |
| 186 | + |
| 187 | +.. code-block:: python |
| 188 | +
|
| 189 | + from pydantic import BaseModel |
| 190 | +
|
| 191 | + class User(BaseModel): |
| 192 | + name: str |
| 193 | + email: str |
| 194 | +
|
| 195 | + class UserFilter(InputFilter): |
| 196 | + name = field(required=True, validators=[IsStringValidator()]) |
| 197 | + email = field(required=True, validators=[EmailValidator()]) |
| 198 | +
|
| 199 | + model(User) |
| 200 | +
|
| 201 | +TypedDict |
| 202 | +~~~~~~~~~ |
| 203 | + |
| 204 | +.. code-block:: python |
| 205 | +
|
| 206 | + from typing import TypedDict |
| 207 | +
|
| 208 | + class UserDict(TypedDict): |
| 209 | + name: str |
| 210 | + email: str |
| 211 | +
|
| 212 | + class UserFilter(InputFilter): |
| 213 | + name = field(required=True, validators=[IsStringValidator()]) |
| 214 | + email = field(required=True, validators=[EmailValidator()]) |
| 215 | +
|
| 216 | + model(UserDict) |
| 217 | +
|
| 218 | +Next Steps |
| 219 | +---------- |
| 220 | + |
| 221 | +For detailed information about each component, see: |
| 222 | + |
| 223 | +- :doc:`Field Decorator <field_decorator>` - Complete field configuration options |
| 224 | +- :doc:`Global Decorators <global_decorators>` - Global filters, validators, and conditions |
| 225 | +- :doc:`Conditions <condition>` - Cross-field validation conditions |
| 226 | +- :doc:`Filters <filter>` - Available filters and custom filter creation |
| 227 | +- :doc:`Validators <validator>` - Available validators and custom validator creation |
0 commit comments