Skip to content

Commit 6388ec5

Browse files
committed
refactor(wizard): enhance page range with extra validation in book creation
- Introduced a new utility function to parse and validate positive integers for start and end page inputs, ensuring they are within a valid range. - Updated the configuration building logic to incorporate this validation, improving the robustness of the book creation process.
1 parent 386fda1 commit 6388ec5

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

apps/studio/src/components/wizard/bookCreationConfig.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import type { WizardFormValues } from "./wizardForm"
22
import { PRESETS } from "./constants"
33

4+
function parsePositiveInt(raw: string): number | undefined {
5+
const n = Number(raw.trim())
6+
return raw.trim() && Number.isInteger(n) && n >= 1 ? n : undefined
7+
}
8+
49
export function buildConfigOverrides(values: WizardFormValues): Record<string, unknown> {
5-
const parsedStartPage = values.startPage.trim() ? Number(values.startPage) : undefined
6-
const parsedEndPage = values.endPage.trim() ? Number(values.endPage) : undefined
10+
const parsedStartPage = parsePositiveInt(values.startPage)
11+
const parsedEndPage = parsePositiveInt(values.endPage)
12+
const validPageRange =
13+
parsedStartPage === undefined || parsedEndPage === undefined || parsedStartPage <= parsedEndPage
714

815
const preset = PRESETS.find((p) => p.id === values.selectedPreset)
916
const baseConfig = preset?.baseConfig ?? {}
@@ -32,8 +39,8 @@ export function buildConfigOverrides(values: WizardFormValues): Record<string, u
3239
if (values.styleguide.trim()) config.styleguide = values.styleguide.trim()
3340
if (values.editingLanguage.trim()) config.editing_language = values.editingLanguage.trim()
3441
if (values.outputLanguages.length > 0) config.output_languages = values.outputLanguages
35-
if (parsedStartPage !== undefined) config.start_page = parsedStartPage
36-
if (parsedEndPage !== undefined) config.end_page = parsedEndPage
42+
if (validPageRange && parsedStartPage !== undefined) config.start_page = parsedStartPage
43+
if (validPageRange && parsedEndPage !== undefined) config.end_page = parsedEndPage
3744
if (values.imageSegmentation && values.segmentationMinSide.trim()) {
3845
const n = Number(values.segmentationMinSide.trim())
3946
if (!isNaN(n)) config.image_segmentation = { min_side: n }

apps/studio/src/components/wizard/step1BasicInfo/PageRange.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// TODO: Add translations
21
import { useStore } from "@tanstack/react-form"
32
import { useWizardForm } from "@/components/wizard/wizardForm"
43
import { RangeSlider } from "@/components/wizard/shared/RangeSlider"

0 commit comments

Comments
 (0)