fix(dashboard): Allow float values for product prices (#9859)

This commit is contained in:
Kasper Fabricius Kristensen
2024-11-04 09:04:29 +01:00
committed by GitHub
parent 7b51204ef3
commit a0034a0fef
3 changed files with 29 additions and 7 deletions
+20 -1
View File
@@ -1,7 +1,7 @@
import i18next from "i18next"
import { FieldPath, FieldValues, UseFormReturn } from "react-hook-form"
import { z } from "zod"
import { castNumber } from "./cast-number"
import { FieldPath, FieldValues, UseFormReturn } from "react-hook-form"
/**
* Validates that an optional value is an integer.
@@ -34,6 +34,25 @@ export const optionalInt = z
}
)
/**
* Validates that an optional value is an number.
*/
export const optionalFloat = z
.union([z.string(), z.number()])
.optional()
.refine(
(value) => {
if (value === "" || value === undefined) {
return true
}
return castNumber(value) >= 0
},
{
message: i18next.t("validation.mustBePositive"),
}
)
/**
* Schema for metadata form.
*/
@@ -80,10 +80,13 @@ export const ProductCreateForm = ({
return {}
}
return regions.reduce((acc, reg) => {
acc[reg.id] = reg.currency_code
return acc
}, {} as Record<string, string>)
return regions.reduce(
(acc, reg) => {
acc[reg.id] = reg.currency_code
return acc
},
{} as Record<string, string>
)
}, [regions])
/**
@@ -1,6 +1,6 @@
import { z } from "zod"
import { i18n } from "../../../components/utilities/i18n/i18n.tsx"
import { optionalInt } from "../../../lib/validation.ts"
import { optionalFloat, optionalInt } from "../../../lib/validation.ts"
import { decorateVariantsWithDefaultValues } from "./utils.ts"
export const MediaSchema = z.object({
@@ -32,7 +32,7 @@ const ProductCreateVariantSchema = z.object({
inventory_kit: z.boolean().optional(),
options: z.record(z.string(), z.string()),
variant_rank: z.number(),
prices: z.record(z.string(), optionalInt).optional(),
prices: z.record(z.string(), optionalFloat).optional(),
inventory: z
.array(
z.object({