From 0d101d42370c16405294980d377cc1b348801e20 Mon Sep 17 00:00:00 2001 From: Sinclair Chen Date: Tue, 28 Jun 2022 18:08:57 -0500 Subject: [PATCH] wip Fix API validation --- functions/package.json | 2 ++ functions/src/create-contract.ts | 29 ++++++++++++++++++++++++++--- functions/src/on-create-contract.ts | 2 +- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/functions/package.json b/functions/package.json index eb6c7151..57d0b37e 100644 --- a/functions/package.json +++ b/functions/package.json @@ -23,6 +23,8 @@ "main": "functions/src/index.js", "dependencies": { "@amplitude/node": "1.10.0", + "@tiptap/core": "^2.0.0-beta.181", + "@tiptap/starter-kit": "^2.0.0-beta.190", "fetch": "1.1.0", "firebase-admin": "10.0.0", "firebase-functions": "3.21.2", diff --git a/functions/src/create-contract.ts b/functions/src/create-contract.ts index 71d778b3..b2250d7b 100644 --- a/functions/src/create-contract.ts +++ b/functions/src/create-contract.ts @@ -5,7 +5,6 @@ import { CPMMBinaryContract, Contract, FreeResponseContract, - MAX_DESCRIPTION_LENGTH, MAX_QUESTION_LENGTH, MAX_TAG_LENGTH, NumericContract, @@ -28,10 +27,34 @@ import { getNewContract } from '../../common/new-contract' import { NUMERIC_BUCKET_COUNT } from '../../common/numeric-constants' import { User } from '../../common/user' import { Group, MAX_ID_LENGTH } from '../../common/group' +import { JSONContent } from '@tiptap/core' + +const descScehma: z.ZodType = z.lazy(() => + z.intersection( + z.record(z.any()), + z.object({ + type: z.string().optional(), + attrs: z.record(z.any()).optional(), + content: z.array(descScehma).optional(), + marks: z + .array( + z.intersection( + z.record(z.any()), + z.object({ + type: z.string(), + attrs: z.record(z.any()).optional(), + }) + ) + ) + .optional(), + text: z.string().optional(), + }) + ) +) const bodySchema = z.object({ question: z.string().min(1).max(MAX_QUESTION_LENGTH), - description: z.string().max(MAX_DESCRIPTION_LENGTH), + description: descScehma.optional(), tags: z.array(z.string().min(1).max(MAX_TAG_LENGTH)).optional(), closeTime: zTimestamp().refine( (date) => date.getTime() > new Date().getTime(), @@ -114,7 +137,7 @@ export const createmarket = newEndpoint(['POST'], async (req, auth) => { user, question, outcomeType, - description, + description ?? {}, initialProb ?? 0, ante, closeTime.getTime(), diff --git a/functions/src/on-create-contract.ts b/functions/src/on-create-contract.ts index 875260c4..614a5c2a 100644 --- a/functions/src/on-create-contract.ts +++ b/functions/src/on-create-contract.ts @@ -2,7 +2,7 @@ import * as functions from 'firebase-functions' import { getUser } from './utils' import { createNotification } from './create-notification' import { Contract } from '../../common/contract' -import { richTextToString } from 'common/util/parse' +import { richTextToString } from '../../common/util/parse' export const onCreateContract = functions.firestore .document('contracts/{contractId}')