Validators
Interface that must be implemented by file validators.
File validators get executed before a file is stored on the database using one of the supported fields. Can be used to add additional data to file object or change it.
Source code in sqlalchemy_file/validators.py
process(file, attr_key)
abstractmethod
Should be overridden in inherited class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
File
|
File object |
required |
attr_key |
str
|
current SQLAlchemy column key. Can be passed to ValidationError |
required |
Source code in sqlalchemy_file/validators.py
Bases: Validator
Validate file maximum size.
Attributes:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
max_size |
If set, the size of the underlying file must be below this file size in order to be valid. The size of the file can be given in one of the following formats:
For more information, view Wikipedia: Binary prefix |
Example
Raises:
Type | Description |
---|---|
SizeValidationError
|
When file |
Source code in sqlalchemy_file/validators.py
Bases: Validator
Validate file mimetype
Attributes:
allowed_content_types: If set, file content_type
must be one of the provided list.
Example
Raises:
Type | Description |
---|---|
ContentTypeValidationError
|
When file |
Source code in sqlalchemy_file/validators.py
Bases: ContentTypeValidator
Default Validator for ImageField.
Attributes:
Name | Type | Description |
---|---|---|
min_wh |
Minimum allowed dimension (w, h). |
|
max_wh |
Maximum allowed dimension (w, h). |
|
allowed_content_types |
An iterable whose items are
allowed content types. Default is |
|
min_aspect_ratio |
Minimum allowed image aspect ratio. |
|
max_aspect_ratio |
Maximum allowed image aspect ratio. |
Example
class Book(Base):
__tablename__ = "book"
id = Column(Integer, autoincrement=True, primary_key=True)
title = Column(String(100), unique=True)
cover = Column(
ImageField(
image_validator=ImageValidator(
allowed_content_types=["image/x-icon", "image/tiff", "image/jpeg"],
min_wh=(200, 200),
max_wh=(400, 400),
min_aspect_ratio=1,
max_aspect_ratio=16/9,
)
)
)
Raises:
Type | Description |
---|---|
ContentTypeValidationError
|
When file |
InvalidImageError
|
When file is not a valid image |
DimensionValidationError
|
When image width and height constraints fail. |
Will add width
and height
properties to the file object
Source code in sqlalchemy_file/validators.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|