Skip to content

Commit 24b9154

Browse files
datalogics-kamdatalogics-cgreen
authored andcommitted
models: Add _bool_to_on_off converter for boolean fields
- Introduced `_bool_to_on_off` function to convert boolean values to "on"/"off". - Applied the new `BeforeValidator` to `preserve_line_breaks`, `word_style`, and `word_coordinates` fields to handle boolean inputs properly. - Ensured consistency in serialization and validation of relevant model fields. Assisted-by: Codex
1 parent 39d9836 commit 24b9154

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/pdfrest/models/_internal.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ def _serialize_file_ids(value: list[PdfRestFile]) -> str:
120120
return ",".join(str(file.id) for file in value)
121121

122122

123+
def _bool_to_on_off(value: Any) -> Any:
124+
if isinstance(value, bool):
125+
return "on" if value else "off"
126+
return value
127+
128+
123129
def _serialize_page_ranges(value: list[str | int | tuple[str | int, ...]]) -> str:
124130
def join_tuple(value: str | int | tuple[str | int, ...]) -> str:
125131
if isinstance(value, tuple):
@@ -364,9 +370,15 @@ class ExtractTextPayload(BaseModel):
364370
PlainSerializer(_serialize_page_ranges),
365371
] = None
366372
full_text: Literal["off", "by_page", "document"] = "document"
367-
preserve_line_breaks: Literal["off", "on"] = "off"
368-
word_style: Literal["off", "on"] = "off"
369-
word_coordinates: Literal["off", "on"] = "off"
373+
preserve_line_breaks: Annotated[
374+
Literal["off", "on"], BeforeValidator(_bool_to_on_off)
375+
] = "off"
376+
word_style: Annotated[Literal["off", "on"], BeforeValidator(_bool_to_on_off)] = (
377+
"off"
378+
)
379+
word_coordinates: Annotated[
380+
Literal["off", "on"], BeforeValidator(_bool_to_on_off)
381+
] = "off"
370382
output_type: Literal["json", "file"] = "json"
371383
output: Annotated[
372384
str | None,

0 commit comments

Comments
 (0)