Skip to content

Commit

Permalink
use zod to do schema validation on incoming requests /user/:userId/im…
Browse files Browse the repository at this point in the history
…age and /user/:userId/bio (#911)
  • Loading branch information
tak-hntlabs authored Aug 28, 2024
1 parent fb4a611 commit b0fb141
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
18 changes: 12 additions & 6 deletions packages/stream-metadata/src/routes/profileImage.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { FastifyReply, FastifyRequest } from 'fastify'
import { ChunkedMedia } from '@river-build/proto'
import { StreamPrefix, StreamStateView, makeStreamId } from '@river-build/sdk'
import { z } from 'zod'

import { StreamIdHex } from '../types'
import { getMediaStreamContent, getStream } from '../riverStreamRpcClient'
import { isBytes32String, isValidEthereumAddress } from '../validators'
import { getMediaEncryption } from '../media-encryption'

const paramsSchema = z.object({
userId: z.string().min(1, 'userId parameter is required'),
})

export async function fetchUserProfileImage(request: FastifyRequest, reply: FastifyReply) {
const logger = request.log.child({ name: fetchUserProfileImage.name })
const { userId } = request.params as { userId?: string }
const parseResult = paramsSchema.safeParse(request.params)

if (!userId) {
logger.info('userId parameter is required')
return reply
.code(400)
.send({ error: 'Bad Request', message: 'userId parameter is required' })
if (!parseResult.success) {
const errorMessage = parseResult.error.errors[0]?.message || 'Invalid parameters'
logger.info(errorMessage)
return reply.code(400).send({ error: 'Bad Request', message: errorMessage })
}

const { userId } = parseResult.data

if (!isValidEthereumAddress(userId)) {
logger.info({ userId }, 'Invalid userId')
return reply.code(400).send({ error: 'Bad Request', message: 'Invalid userId' })
Expand Down
18 changes: 12 additions & 6 deletions packages/stream-metadata/src/routes/userBio.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { FastifyReply, FastifyRequest } from 'fastify'
import { StreamPrefix, StreamStateView, makeStreamId } from '@river-build/sdk'
import { z } from 'zod'

import { getStream } from '../riverStreamRpcClient'
import { isValidEthereumAddress } from '../validators'

const paramsSchema = z.object({
userId: z.string().min(1, 'userId parameter is required'),
})

export async function fetchUserBio(request: FastifyRequest, reply: FastifyReply) {
const logger = request.log.child({ name: fetchUserBio.name })
const { userId } = request.params as { userId?: string }
const parseResult = paramsSchema.safeParse(request.params)

if (!userId) {
logger.info('userId parameter is required')
return reply
.code(400)
.send({ error: 'Bad Request', message: 'userId parameter is required' })
if (!parseResult.success) {
const errorMessage = parseResult.error.errors[0]?.message || 'Invalid parameters'
logger.info(errorMessage)
return reply.code(400).send({ error: 'Bad Request', message: errorMessage })
}

const { userId } = parseResult.data

if (!isValidEthereumAddress(userId)) {
logger.info({ userId }, 'Invalid userId')
return reply.code(400).send({ error: 'Bad Request', message: 'Invalid userId' })
Expand Down

0 comments on commit b0fb141

Please sign in to comment.