Skip to content

Commit

Permalink
fix: Better type narrowing in calculationSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Aug 2, 2024
1 parent 05f038c commit d8970ac
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/schemas/calculationSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { z } from "zod";
import { parse as parsePostcode, fix as fixPostcode } from "postcode";

// Type not exported by postcode lib directly
type ValidPostcode = Extract<ReturnType<typeof parsePostcode>, { valid: true }>;

const HouseTypeEnum = z.enum(['D', 'S', 'T', 'F']);

/**
Expand All @@ -12,12 +15,12 @@ export const calculationSchema = z.object({
.min(1, "housePostcode is required")
.refine(fixPostcode, "Invalid postcode")
.transform(parsePostcode)
.refine((postcode) => postcode.valid, { message: "Invalid postcode" }),
.refine((postcode): postcode is ValidPostcode => postcode.valid),
houseSize: z.coerce.number().positive("houseSize must be a positive integer"),
houseAge: z.coerce.number().positive("houseAge must be a positive integer"),
houseBedrooms: z.coerce
.number()
.positive("houseBedroomsmust be a positive integer"),
.positive("houseBedrooms must be a positive integer"),
houseType: HouseTypeEnum.refine(
(value) => HouseTypeEnum.options.includes(value),
{
Expand Down

0 comments on commit d8970ac

Please sign in to comment.