Validators¶
validate_file_size(min=None, max=None, min_inclusive=True, max_inclusive=True, error=None)
¶
Validator which succeeds if the file passed to it is within the specified
size range. If min is not specified, or is specified as None,
no lower bound exists. If max is not specified, or is specified as None,
no upper bound exists. The inclusivity of the bounds (if they exist)
is configurable.
If min_inclusive is not specified, or is specified as True, then
the min bound is included in the range. If max_inclusive is not specified,
or is specified as True, then the max bound is included in the range.
Example: ::
class UploadFileModel(BaseModel):
file: t.Annotated[
UploadFile,
AfterValidator(validate_file_size(min="1 KiB", max="2 KiB"))
]
:param min: The minimum size (lower bound). If not provided, minimum
size will not be checked.
:param max: The maximum size (upper bound). If not provided, maximum
size will not be checked.
:param min_inclusive: Whether the min bound is included in the range.
:param max_inclusive: Whether the max bound is included in the range.
:param error: Error message to raise in case of a validation error.
Can be interpolated with {input}, {min} and {max}.
Version Added: 3.1.0
Source code in apiflask/validators.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
validate_file_type(accept, error=None)
¶
Validator which succeeds if the uploaded file is allowed by a given list of extensions.
Example: ::
class UploadFileModel(BaseModel):
file: t.Annotated[UploadFile, AfterValidator(validate_file_type(['.png']))]
:param accept: A sequence of allowed extensions.
:param error: Error message to raise in case of a validation error.
Can be interpolated with {input} and {extensions}.
Version Added: 3.1.0
Source code in apiflask/validators.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
External documentation¶
Check out the Validator API documentation for the validators from marshmallow.