Skip to content

models.helpers.validation

check_if_field_has_choices

1
check_if_field_has_choices(field: "BaseField") -> bool

Checks if given field has choices populated. A if it has one, a validator for this field needs to be attached.

Arguments:

  • field: ormar field to check :type field: BaseField

Returns:

result of the check :rtype: bool

convert_value_if_needed

1
convert_value_if_needed(field: "BaseField", value: Any) -> Any

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.

Arguments:

  • field: ormar field to check with choices :type field: BaseField
  • value: current values of the model to verify :type value: Any

Returns:

value, choices list :rtype: Any

generate_model_example

1
generate_model_example(model: Type["Model"], relation_map: Dict = None) -> Dict

Generates example to be included in schema in fastapi.

Arguments:

  • model: ormar.Model :type model: Type["Model"]
  • relation_map: dict with relations to follow :type relation_map: Optional[Dict]

Returns:

dict with example values :rtype: Dict[str, int]

populates_sample_fields_values

1
populates_sample_fields_values(example: Dict[str, Any], name: str, field: "BaseField", relation_map: Dict = None) -> None

Iterates the field and sets fields to sample values

Arguments:

  • field: ormar field :type field: BaseField
  • name: name of the field :type name: str
  • example: example dict :type example: Dict[str, Any]
  • relation_map: dict with relations to follow :type relation_map: Optional[Dict]

get_nested_model_example

1
get_nested_model_example(name: str, field: "BaseField", relation_map: Dict) -> Union[List, Dict]

Gets representation of nested model.

Arguments:

  • name: name of the field to follow :type name: str
  • field: ormar field :type field: BaseField
  • relation_map: dict with relation map :type relation_map: Dict

Returns:

nested model or list of nested model repr :rtype: Union[List, Dict]

generate_pydantic_example

1
generate_pydantic_example(pydantic_model: Type[pydantic.BaseModel], exclude: Set = None) -> Dict

Generates dict with example.

Arguments:

  • pydantic_model: model to parse :type pydantic_model: Type[pydantic.BaseModel]
  • exclude: list of fields to exclude :type exclude: Optional[Set]

Returns:

dict with fields and sample values :rtype: Dict

get_pydantic_example_repr

1
get_pydantic_example_repr(type_: Any) -> Any

Gets sample representation of pydantic field for example dict.

Arguments:

  • type_: type of pydantic field :type type_: Any

Returns:

representation to include in example :rtype: Any

overwrite_example_and_description

1
overwrite_example_and_description(schema: Dict[str, Any], model: Type["Model"]) -> None

Overwrites the example with properly nested children models. Overwrites the description if it's taken from ormar.Model.

Arguments:

  • schema: schema of current model :type schema: Dict[str, Any]
  • model: model class :type model: Type["Model"]

overwrite_binary_format

1
overwrite_binary_format(schema: Dict[str, Any], model: Type["Model"]) -> None

Overwrites format of the field if it's a LargeBinary field with a flag to represent the field as base64 encoded string.

Arguments:

  • schema: schema of current model :type schema: Dict[str, Any]
  • model: model class :type model: Type["Model"]

construct_modify_schema_function

1
construct_modify_schema_function(fields_with_choices: List) -> SchemaExtraCallable

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.

Arguments:

  • fields_with_choices: list of fields with choices validation :type fields_with_choices: List

Returns:

callable that will be run by pydantic to modify the schema :rtype: Callable

construct_schema_function_without_choices

1
construct_schema_function_without_choices() -> SchemaExtraCallable

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:

callable that will be run by pydantic to modify the schema :rtype: Callable

populate_choices_validators

1
populate_choices_validators(model: Type["Model"]) -> None

Checks if Model has any fields with choices set. If yes it adds choices validation into pre root validators.

Arguments:

  • model: newly constructed Model :type model: Model class