6.4 KiB
models.helpers.validation
check_if_field_has_choices
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(BaseField): ormar field to check
Returns:
bool: result of the check
convert_choices_if_needed
convert_choices_if_needed(field: "BaseField", value: Any) -> Tuple[Any, List]
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(BaseField): ormar field to check with choicesvalues(Dict): current values of the model to verify
Returns:
Tuple[Any, List]: value, choices list
validate_choices
validate_choices(field: "BaseField", value: Any) -> None
Validates if given value is in provided choices.
Raises:
ValueError: If value is not in choices.
Arguments:
field(BaseField): field to validatevalue(Any): value of the field
choices_validator
choices_validator(cls: Type["Model"], values: Dict[str, Any]) -> Dict[str, Any]
Validator that is attached to pydantic model pre root validators. Validator checks if field value is in field.choices list.
Raises:
ValueError: if field value is outside of allowed choices.
Arguments:
cls(Model class): constructed classvalues(Dict[str, Any]): dictionary of field values (pydantic side)
Returns:
Dict[str, Any]: values if pass validation, otherwise exception is raised
generate_model_example
generate_model_example(model: Type["Model"], relation_map: Dict = None) -> Dict
Generates example to be included in schema in fastapi.
Arguments:
model(Type["Model"]): ormar.Modelrelation_map(Optional[Dict]): dict with relations to follow
Returns:
Dict[str, int]: dict with example values
populates_sample_fields_values
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(BaseField): ormar fieldname(str): name of the fieldexample(Dict[str, Any]): example dictrelation_map(Optional[Dict]): dict with relations to follow
get_nested_model_example
get_nested_model_example(name: str, field: "BaseField", relation_map: Dict) -> Union[List, Dict]
Gets representation of nested model.
Arguments:
name(str): name of the field to followfield(BaseField): ormar fieldrelation_map(Dict): dict with relation map
Returns:
Union[List, Dict]: nested model or list of nested model repr
generate_pydantic_example
generate_pydantic_example(pydantic_model: Type[pydantic.BaseModel], exclude: Set = None) -> Dict
Generates dict with example.
Arguments:
pydantic_model(Type[pydantic.BaseModel]): model to parseexclude(Optional[Set]): list of fields to exclude
Returns:
Dict: dict with fields and sample values
get_pydantic_example_repr
get_pydantic_example_repr(type_: Any) -> Any
Gets sample representation of pydantic field for example dict.
Arguments:
type_(Any): type of pydantic field
Returns:
Any: representation to include in example
overwrite_example_and_description
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(Dict[str, Any]): schema of current modelmodel(Type["Model"]): model class
overwrite_binary_format
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(Dict[str, Any]): schema of current modelmodel(Type["Model"]): model class
construct_modify_schema_function
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): list of fields with choices validation
Returns:
Callable: callable that will be run by pydantic to modify the schema
construct_schema_function_without_choices
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: callable that will be run by pydantic to modify the schema
populate_choices_validators
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(Model class): newly constructed Model