Skip to content

Commit

Permalink
🐛 [open-formulieren/open-forms#3536] Manually parse product amount field
Browse files Browse the repository at this point in the history
Using zod parse or parseSafe (to not crash) can't properly handle invalid
amount values for a single product. This happens when clearing the
input and causing a crash of the entire form.

Since we only want to pass down the (valid) data for subsequent product
selections, we need some additional robustness on data that is
potentially invalid.

This is to hold us over until we can properly do this in typescript
and get type safety through a compiler instead of chasing after
the facts through propTypes.
  • Loading branch information
sergei-maertens committed Nov 1, 2023
1 parent e4b95ae commit 71f1aa1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/components/appointments/steps/ChooseProductStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const productSchema = z
.array(
z.object({
productId: z.string(),
amount: z.number().int().gte(1).finite(),
amount: z.union([z.number().int().gte(1).finite(), z.literal('').transform(() => 0)]),

Check warning on line 27 in src/components/appointments/steps/ChooseProductStep.js

View check run for this annotation

Codecov / codecov/patch

src/components/appointments/steps/ChooseProductStep.js#L27

Added line #L27 was not covered by tests
})
)
.nonempty();
Expand All @@ -35,7 +35,7 @@ const chooseMultiProductSchema = z.object({products: productSchema});
const ChooseProductStepFields = ({values: {products = []}, validateForm}) => {
const intl = useIntl();
const {supportsMultipleProducts} = useContext(AppointmentConfigContext);
products = productSchema.parse(products);
products = products.map(product => ({...product, amount: Number(product.amount || '0')}));

/**
* Decorate an arrayHelper callback to invoke the validate function.
Expand Down

0 comments on commit 71f1aa1

Please sign in to comment.