From d8970acf4e45d3912d32c2c8b2d2f88d4512f0b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 2 Aug 2024 16:20:21 +0100 Subject: [PATCH] fix: Better type narrowing in calculationSchema --- app/schemas/calculationSchema.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/schemas/calculationSchema.ts b/app/schemas/calculationSchema.ts index 99d0319e..cfb5274b 100644 --- a/app/schemas/calculationSchema.ts +++ b/app/schemas/calculationSchema.ts @@ -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, { valid: true }>; + const HouseTypeEnum = z.enum(['D', 'S', 'T', 'F']); /** @@ -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), {