From c261fc274360711baf1033f12907971a96b13548 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Mon, 18 Jul 2022 19:17:04 -0500 Subject: [PATCH] Validate lowLimitProb < highLimitProb --- functions/src/place-bet.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 12589752..56007933 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -29,16 +29,26 @@ const bodySchema = z.object({ amount: z.number().gte(1), }) -const binarySchema = z.union([ - z.object({ - outcome: z.enum(['YES', 'NO']), - limitProb: z.number().gte(0.001).lte(0.999).optional(), - }), - z.object({ - lowLimitProb: z.number().gte(0.001).lte(0.999), - highLimitProb: z.number().gte(0.001).lte(0.999), - }), -]) +const binarySchema = z + .union([ + z.object({ + outcome: z.enum(['YES', 'NO']), + limitProb: z.number().gte(0.001).lte(0.999).optional(), + }), + z.object({ + lowLimitProb: z.number().gte(0.001).lte(0.999), + highLimitProb: z.number().gte(0.001).lte(0.999), + }), + ]) + .superRefine((data, ctx) => { + if ('lowLimitProb' in data && data.lowLimitProb >= data.highLimitProb) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ['lowLimitProb'], + message: 'lowLimitProb should be less than highLimitProb', + }) + } + }) const freeResponseSchema = z.object({ outcome: z.string(),