validation
check_if_field_has_choices(field)
Checks if given field has choices populated. A if it has one, a validator for this field needs to be attached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
BaseField
|
ormar field to check |
required |
Returns:
Type | Description |
---|---|
bool
|
result of the check |
Source code in ormar\models\helpers\validation.py
33 34 35 36 37 38 39 40 41 42 43 |
|
construct_modify_schema_function(fields_with_choices)
Modifies the schema to include fields with choices validator. Those fields will be displayed in schema as Enum types with available choices values listed next to them.
Note that schema extra has to be a function, otherwise it's called to soon before all the relations are expanded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fields_with_choices |
List
|
list of fields with choices validation |
required |
Returns:
Type | Description |
---|---|
Callable
|
callable that will be run by pydantic to modify the schema |
Source code in ormar\models\helpers\validation.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
construct_schema_function_without_choices()
Modifies model example and description if needed.
Note that schema extra has to be a function, otherwise it's called to soon before all the relations are expanded.
Returns:
Type | Description |
---|---|
Callable
|
callable that will be run by pydantic to modify the schema |
Source code in ormar\models\helpers\validation.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
|
convert_value_if_needed(field, value)
Converts dates to isoformat as fastapi can check this condition in routes and the fields are not yet parsed. Converts enums to list of it's values. Converts uuids to strings. Converts decimal to float with given scale.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
BaseField
|
ormar field to check with choices |
required |
value |
Any
|
current values of the model to verify |
required |
Returns:
Type | Description |
---|---|
Any
|
value, choices list |
Source code in ormar\models\helpers\validation.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
generate_model_example(model, relation_map=None)
Generates example to be included in schema in fastapi.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[Model]
|
ormar.Model |
required |
relation_map |
Dict
|
dict with relations to follow |
None
|
Returns:
Type | Description |
---|---|
Dict[str, int]
|
dict with example values |
Source code in ormar\models\helpers\validation.py
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 |
|
generate_pydantic_example(pydantic_model, exclude=None)
Generates dict with example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pydantic_model |
Type[pydantic.BaseModel]
|
model to parse |
required |
exclude |
Set
|
list of fields to exclude |
None
|
Returns:
Type | Description |
---|---|
Dict
|
dict with fields and sample values |
Source code in ormar\models\helpers\validation.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
get_nested_model_example(name, field, relation_map)
Gets representation of nested model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the field to follow |
required |
field |
BaseField
|
ormar field |
required |
relation_map |
Dict
|
dict with relation map |
required |
Returns:
Type | Description |
---|---|
Union[List, Dict]
|
nested model or list of nested model repr |
Source code in ormar\models\helpers\validation.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
get_pydantic_example_repr(type_)
Gets sample representation of pydantic field for example dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ |
Any
|
type of pydantic field |
required |
Returns:
Type | Description |
---|---|
Any
|
representation to include in example |
Source code in ormar\models\helpers\validation.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
overwrite_binary_format(schema, model)
Overwrites format of the field if it's a LargeBinary field with a flag to represent the field as base64 encoded string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
Dict[str, Any]
|
schema of current model |
required |
model |
Type[Model]
|
model class |
required |
Source code in ormar\models\helpers\validation.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
overwrite_example_and_description(schema, model)
Overwrites the example with properly nested children models. Overwrites the description if it's taken from ormar.Model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
Dict[str, Any]
|
schema of current model |
required |
model |
Type[Model]
|
model class |
required |
Source code in ormar\models\helpers\validation.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
populate_choices_validators(model)
Checks if Model has any fields with choices set. If yes it adds choices validation into pre root validators.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[Model]
|
newly constructed Model |
required |
Source code in ormar\models\helpers\validation.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
|
populates_sample_fields_values(example, name, field, relation_map=None)
Iterates the field and sets fields to sample values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
BaseField
|
ormar field |
required |
name |
str
|
name of the field |
required |
example |
Dict[str, Any]
|
example dict |
required |
relation_map |
Dict
|
dict with relations to follow |
None
|
Source code in ormar\models\helpers\validation.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|