Skip to content

Commit

Permalink
fix: 🐛 Allow parsing of NH as a handicap value
Browse files Browse the repository at this point in the history
  • Loading branch information
n8io committed Dec 8, 2023
1 parent fcd722a commit cd83df6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-students-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ghin": patch
---

fix: 🐛 Allow parsing of `NH` as a handicap value
23 changes: 16 additions & 7 deletions src/client/ghin/models/golfers/search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { z } from "zod";
import { boolean, date, emptyStringToNull, float, gender, number, string } from "../../../../models";
import {
boolean,
date,
emptyStringToNull,
float,
gender,
handicap,
number,
string,
} from "../../../../models";

const schemaGolferStatus = z.enum(['Active', 'Inactive'])

Expand All @@ -25,7 +34,7 @@ const schemaGolfer = z.object({
last_name: string,
association_id: number,
association_name: string,
handicap_index: float.nullable(),
handicap_index: handicap,
club_affiliation_id: number,
club_id: number,
club_name: emptyStringToNull,
Expand All @@ -35,22 +44,22 @@ const schemaGolfer = z.object({
hard_cap: boolean,
has_digital_profile: boolean,
hi_display: string,
hi_value: float,
hi_value: handicap,
is_home_club: boolean,
low_hi_date: date.nullable(),
low_hi_display: string,
low_hi_value: float,
low_hi: float.nullable(),
low_hi_value: handicap,
low_hi: handicap,
message_club_authorized: string.nullable(),
middle_name: emptyStringToNull.nullable().optional(),
phone_number: emptyStringToNull.nullable().optional(),
prefix: emptyStringToNull.optional(),
rev_date: date.nullable(),
soft_cap: boolean,
state: emptyStringToNull,
status: z.enum(['Active', 'Inactive']),
status: z.enum(["Active", "Inactive"]),
suffix: emptyStringToNull.optional(),
})
});

type Golfer = z.infer<typeof schemaGolfer>

Expand Down
43 changes: 27 additions & 16 deletions src/client/ghin/models/handicaps/response.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { z } from "zod";
import { boolean, float, number, string } from "../../../../models";
import { boolean, handicap, number, string } from "../../../../models";
import { schemaCoursePercentPlayerHandicap } from "./course-player-handicap";

const schemaGolferHandicapResponse = z.object({
golfer: z.object({
clubs: z.array(z.object({
active: boolean,
association_id: number,
club_name: string,
id: number,
})),
handicap_index: float.nullable().default(null),
})
}).passthrough()
const schemaGolferHandicapResponse = z
.object({
golfer: z.object({
clubs: z.array(
z.object({
active: boolean,
association_id: number,
club_name: string,
id: number,
})
),
handicap_index: handicap,
}),
})
.passthrough();

type HandicapResponse = z.infer<typeof schemaGolferHandicapResponse>
type HandicapResponse = z.infer<typeof schemaGolferHandicapResponse>;

const schemaCoursePlayerHandicapsResponse = z.object({
100: schemaCoursePercentPlayerHandicap,
Expand All @@ -37,9 +41,16 @@ const schemaCoursePlayerHandicapsResponse = z.object({
15: schemaCoursePercentPlayerHandicap,
10: schemaCoursePercentPlayerHandicap,
5: schemaCoursePercentPlayerHandicap,
})
});

type CoursePlayerHandicapsResponse = z.infer<typeof schemaCoursePlayerHandicapsResponse>
type CoursePlayerHandicapsResponse = z.infer<
typeof schemaCoursePlayerHandicapsResponse
>;

export { HandicapResponse, CoursePlayerHandicapsResponse, schemaGolferHandicapResponse, schemaCoursePlayerHandicapsResponse };
export {
CoursePlayerHandicapsResponse,
HandicapResponse,
schemaCoursePlayerHandicapsResponse,
schemaGolferHandicapResponse,
};

34 changes: 33 additions & 1 deletion src/models/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,39 @@ const emptyString = z.string().trim();
const emptyStringToNull = emptyString.nullable().transform((value) => value || null);
const float = z.coerce.number();
const gender = z.enum(['M', 'F']);

const handicap = z
.union([z.number(), z.string(), z.null()])
.refine((value) => {
if (typeof value === "number") {
return true;
}

if (typeof value === "string" && value === "NH") {
return true;
}

return false;
})
.transform((value) => {
if (value === "NH") {
return null;
}

return value;
});

const number = float.int();
const string = emptyString.min(1);

export { boolean, date, emptyStringToNull, float, gender, monthDay, number, string }
export {
boolean,
date,
emptyStringToNull,
float,
gender,
handicap,
monthDay,
number,
string,
};

0 comments on commit cd83df6

Please sign in to comment.