Skip to main content

request_body_to_args

Converts a request body into a dictionary of arguments and a list of validation errors. It handles different body types like dictionaries, FormData, and bytes, and processes fields based on whether they are embedded or not.

def request_body_to_args(
body_fields: list[ModelField],
received_body: dict[str, Any] | FormData | bytes | None,
embed_body_fields: bool
) - > tuple[dict[str, Any], list[dict[str, Any]]]

Processes a raw request body, extracting and validating fields against a list of expected model fields. This function is used to convert incoming HTTP request data into a structured format suitable for further processing or validation within the application.

Parameters

NameTypeDescription
body_fieldslist[ModelField]A list of ModelField objects defining the expected structure and validation rules for the fields within the request body. This list guides how the incoming data should be parsed and validated.
received_body`dict[str, Any]FormData
embed_body_fieldsboolA boolean flag indicating whether the fields from the request body should be embedded directly into the argument dictionary or if the entire body should be treated as a single field's value. When True, individual fields are extracted; when False and there's only one body_field, the whole body is assigned to that field.

Returns

TypeDescription
tuple[dict[str, Any], list[dict[str, Any]]]A tuple containing two elements: a dictionary of validated argument values where keys are field names and values are the processed data, and a list of dictionaries representing any validation errors encountered during processing.