Skip to content

Commit

Permalink
fix(elation): looser validation in settings rateLimitDurationSchema i…
Browse files Browse the repository at this point in the history
…n order to successfully validate empty strings
  • Loading branch information
bejoinka committed Feb 12, 2025
1 parent 7164dfd commit 085955b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
16 changes: 5 additions & 11 deletions extensions/elation/actions/getPatient/getPatient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ export const getPatient: Action<
previewable: true,
dataPoints,
onEvent: async ({ payload, onComplete }): Promise<void> => {
const { fields, settings } = validate({
schema: z.object({
fields: FieldsValidationSchema,
settings: SettingsValidationSchema,
}),
payload,
})
const fields = FieldsValidationSchema.parse(payload.fields)
// API Call should produce AuthError or something dif.
const api = makeAPIClient(settings)
const api = makeAPIClient(payload.settings)

const patientInfo = await api.getPatient(fields.patientId)

Expand All @@ -40,10 +34,10 @@ export const getPatient: Action<
primaryPhysicianId: String(patientInfo.primary_physician),
caregiverPracticeId: String(patientInfo.caregiver_practice),
mainPhone: elationMobilePhoneToE164(
patientInfo.phones?.find((p) => p.phone_type === 'Main')?.phone
patientInfo.phones?.find((p) => p.phone_type === 'Main')?.phone,
),
mobilePhone: elationMobilePhoneToE164(
patientInfo.phones?.find((p) => p.phone_type === 'Mobile')?.phone
patientInfo.phones?.find((p) => p.phone_type === 'Mobile')?.phone,
),
email: getLastEmail(patientInfo.emails),
middleName: patientInfo.middle_name,
Expand All @@ -61,7 +55,7 @@ export const getPatient: Action<
previousLastName: patientInfo.previous_last_name,
status: patientInfo.patient_status.status,
preferredServiceLocationId: String(
patientInfo.preferred_service_location
patientInfo.preferred_service_location,
),
patientObject: JSON.stringify(patientInfo),
},
Expand Down
40 changes: 20 additions & 20 deletions extensions/elation/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RateLimitConfig, type Setting } from '@awell-health/extensions-core'
import { isFinite, isNil } from 'lodash'
import { isEmpty, isFinite, isNil } from 'lodash'
import { z, type ZodTypeAny } from 'zod'

export const settings = {
Expand Down Expand Up @@ -57,24 +57,24 @@ export const settings = {
},
} satisfies Record<string, Setting>

export const rateLimitDurationSchema = z
.string()
.refine(
(val) => {
try {
const [number, unit] = val.split(' ')
const parsedUnit = parseDurationUnit(unit)
return isFinite(Number(number)) && !isNil(parsedUnit)
} catch (error) {
return false
}
},
{
message:
'Duration must be in format {number} {unit} where unit is seconds, minutes, hours or days',
},
)
.optional()
export const rateLimitDurationSchema = z.string().refine(
(val) => {
if (isNil(val) || isEmpty(val)) {
return true
}
try {
const [number, unit] = val.split(' ')
const parsedUnit = parseDurationUnit(unit)
return isFinite(Number(number)) && !isNil(parsedUnit)
} catch (error) {
return false
}
},
{
message:
'Duration must be in format {number} {unit} where unit is seconds, minutes, hours or days',
},
)

export const SettingsValidationSchema = z.object({
base_url: z.string().min(1),
Expand All @@ -88,7 +88,7 @@ export const SettingsValidationSchema = z.object({
*/
username: z.string().optional(),
password: z.string().optional(),
rateLimitDuration: rateLimitDurationSchema,
rateLimitDuration: rateLimitDurationSchema.optional(),
} satisfies Record<keyof typeof settings, ZodTypeAny>)

export type SettingsType = z.infer<typeof SettingsValidationSchema>
Expand Down

0 comments on commit 085955b

Please sign in to comment.