oscklmO
Convex Community2y ago
15 replies
oscklm

Possibility of making a object validator all optional for use when patching fields

Say i have this:
// Schema
export const submissionFields = {
  userId: v.id('users'),
  profileIndex: v.number(),
  videoId: v.id('videos'),

  // Details
  title: v.optional(v.string()),
  storageId: v.id('_storage'),

  // Settings
  privacy: v.optional(v.union(v.literal('public'), v.literal('private'))),
  isApproved: v.boolean(),
}


// Mutation example
const { userId, ...fields } = submissionFields

export const update = mutation({
  args: {
    id: v.id('submissions'),
    ...fields,
  },
  handler: async (ctx, { id, ...fields }) => {
    const user = await authenticateUser(ctx)

    if (!user) {
      throw new Error('User not authenticated')
    }

    return await ctx.db.patch(id, {
      ...fields,
    })
  },
})


In this case, i cant just pass what i want but need to pass all values that aren't already optional
const handleChangePrivacy = (id: Id<'submissions'>, value: boolean) => {
  updateSubmission({
    id,
    privacy: value ? 'public' : 'private',
  })
}


I might have missed some smart way here. But this is a solid wall i hit very often.
Was this page helpful?