Skip to content

Commit

Permalink
update spinners package
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele Granello committed Aug 23, 2024
1 parent 0b65c42 commit 7237d4a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
10 changes: 8 additions & 2 deletions app/api/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { NextResponse } from "next/server";
import { Calculation, calculationSchema } from "../schemas/calculationSchema";
import { api, apiSchema } from "../schemas/apiSchema";
import { calculationSchema } from "../schemas/calculationSchema";
import * as calculationService from "../services/calculationService";

export async function POST(req: Request) {
try {
// Parse and validate user input
const data = await req.json();
const input: Calculation = calculationSchema.parse(data);
let input: api;
if (!apiSchema.safeParse(data).success) {
input = calculationSchema.parse(data);
} else {
input = data;
}
const householdData = await calculationService.getHouseholdData(input);
return NextResponse.json(householdData);
} catch (err) {
Expand Down
28 changes: 28 additions & 0 deletions app/schemas/apiSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { z } from "zod";
import { parse as parsePostcode } from "postcode";
import { HOUSE_TYPES } from "../models/Property";

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

const HouseTypeEnum = z.enum(HOUSE_TYPES);

export const apiSchema = z.object({
housePostcode: z.custom<ValidPostcode>(),
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("houseBedrooms must be a positive integer"),
houseType: HouseTypeEnum.refine(
(value) => HouseTypeEnum.options.includes(value),
{
message: `houseType is required and must be one of ${HOUSE_TYPES}`,
}
),
});

export type api = z.infer<typeof apiSchema>;
19 changes: 6 additions & 13 deletions app/schemas/calculationSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@ const HouseTypeEnum = z.enum(HOUSE_TYPES);
* Describes the form the user will interact with in the frontend
*/
export const calculationSchema = z.object({
housePostcode: z.union([
z
.string()
.min(1, "housePostcode is required")
.refine(fixPostcode, "Invalid postcode")
.transform(parsePostcode)
.refine(
(postcode): postcode is ValidPostcode => postcode.valid,
"Invalid postcode"
),
z.custom<ValidPostcode>(),
]),

housePostcode: z
.string()
.min(1, "housePostcode is required")
.refine(fixPostcode, "Invalid postcode")
.transform(parsePostcode)
.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
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"react": "^18",
"react-dom": "^18",
"react-hook-form": "^7.52.2",
"react-spinners": "^0.14.1",
"ts-node": "^10.9.2",
"zod": "^3.23.8"
},
Expand Down

0 comments on commit 7237d4a

Please sign in to comment.