Fix logic bug in FR market validation

This commit is contained in:
Benjamin 2022-06-27 07:05:15 -07:00
parent 449a3b649c
commit d5800dfe87

View File

@ -26,10 +26,15 @@ const binarySchema = z.object({
probabilityInt: z.number().gte(0).lt(100).optional(), probabilityInt: z.number().gte(0).lt(100).optional(),
}) })
const freeResponseSchema = z.object({ const freeResponseSchema = z.union([
outcome: z.union([z.enum(['MKT', 'CANCEL']), z.string()]), z.object({
resolutions: z.map(z.string(), z.number()).optional(), outcome: z.union([z.enum(['CANCEL']), z.number()]),
}) }),
z.object({
outcome: z.enum(['MKT']),
resolutions: z.map(z.string(), z.number()),
}),
])
const numericSchema = z.object({ const numericSchema = z.object({
outcome: z.union([z.enum(['CANCEL']), z.string()]), outcome: z.union([z.enum(['CANCEL']), z.string()]),
@ -209,14 +214,16 @@ function getResolutionParams(outcomeType: string, body: string) {
probabilityInt: undefined, probabilityInt: undefined,
} }
} else if (outcomeType === 'FREE_RESPONSE') { } else if (outcomeType === 'FREE_RESPONSE') {
const { outcome, resolutions } = validate(freeResponseSchema, body) const freeResponseParams = validate(freeResponseSchema, body)
if (!(outcome === 'MKT' && resolutions)) const { outcome } = freeResponseParams
throw new APIError( const resolutions =
400, 'resolutions' in freeResponseParams
'Resolutions cannot be specified with MKT outcome' ? freeResponseParams.resolutions
) : undefined
return { return {
outcome, // Free Response outcome IDs are numbers by convention,
// but treated as strings everywhere else.
outcome: outcome.toString(),
resolutions, resolutions,
value: undefined, value: undefined,
probabilityInt: undefined, probabilityInt: undefined,