Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/client/ghin/models/scores/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ const schemaScoreTypeWithTransform: z.ZodType<RawScoreType, z.ZodTypeDef, ScoreT
(value) => scoreTypesMap[value]
)

const scoreStatuses = ['VALIDATED', 'UNDER_REVIEW'] as const
const scoreStatuses = ['VALIDATED', 'UNDER_REVIEW', 'TEMPORARY'] as const
const schemaScoreStatus = z.enum(scoreStatuses)
type ScoreStatus = z.infer<typeof schemaScoreStatus>

const rawScoreStatuses = ['Validated', 'UnderReview'] as const
const rawScoreStatuses = ['Validated', 'UnderReview', 'Temporary'] as const
const schemaRawScoreStatus = z.enum(rawScoreStatuses)

const scoreStatusesMap = {
Validated: 'VALIDATED',
UnderReview: 'UNDER_REVIEW',
Temporary: 'TEMPORARY',
} as const

const schemaScoreStatusWithTransform = schemaRawScoreStatus.transform(
Expand Down
14 changes: 8 additions & 6 deletions src/client/ghin/models/scores/statistics.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { z } from 'zod'
import { date, emptyStringToNull, float, number } from '../../../../models'
import { date, emptyStringToNull, float, floatSafe, number } from '../../../../models'

// GHIN sends NaN (or "NaN" strings) for fairway/miss percents on rounds where
// the user didn't log that stat. Use floatSafe (nullable) for those fields.
const schemaStatistics = z.object({
birdies_or_better_percent: float,
bogeys_percent: float,
double_bogeys_percent: float,
fairway_hits_percent: float,
fairway_hits_percent: floatSafe,
gir_percent: float,
last_stats_update_date: date,
last_stats_update_type: emptyStringToNull,
missed_general_approach_shot_accuracy_percent: float,
missed_left_approach_shot_accuracy_percent: float,
missed_left_percent: float,
missed_left_percent: floatSafe,
missed_long_approach_shot_accuracy_percent: float,
missed_long_percent: float,
missed_long_percent: floatSafe,
missed_right_approach_shot_accuracy_percent: float,
missed_right_percent: float,
missed_right_percent: floatSafe,
missed_short_approach_shot_accuracy_percent: float,
missed_short_percent: float,
missed_short_percent: floatSafe,
one_putt_or_better_percent: float,
par3s_average: float,
par4s_average: float,
Expand Down
14 changes: 13 additions & 1 deletion src/models/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const date = z
const emptyString = z.string().trim()
const emptyStringToNull = emptyString.nullable().transform((value) => value || null)
const float = z.coerce.number()
// NaN-tolerant float: coerces NaN / "NaN" / empty string to null. GHIN returns
// these in statistics objects for rounds without tracked data.
const floatSafe = z.preprocess((v) => {
if (v === null || v === undefined) return null
if (typeof v === 'string') {
if (v === '' || v.toLowerCase() === 'nan') return null
const n = Number(v)
return Number.isFinite(n) ? n : null
}
if (typeof v === 'number') return Number.isFinite(v) ? v : null
return v
}, z.number().nullable())
const gender = z.enum(['M', 'F'])

const handicap = z
Expand Down Expand Up @@ -65,4 +77,4 @@ const shortDate = z
return new Date(`${year}-${month}-${day}T00:00Z`)
})

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