Skip to content

Commit 49da79b

Browse files
authored
Merge pull request #6 from udecode/fix-beforecreate-validation
2 parents 20dad8c + bdb2151 commit 49da79b

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"better-auth-convex": patch
3+
---
4+
5+
Fix beforeCreate hook validation error
6+
7+
Previously, the `create` mutation's input validator required all schema fields to be present, causing validation errors when `beforeCreate` hooks tried to add required fields like `username`.
8+
9+
This fix makes all input fields optional during validation, allowing `beforeCreate` hooks to add or modify any required fields. The actual schema validation still occurs when inserting into the database, ensuring data integrity.

src/api.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ export const createHandler = async (
6868
beforeCreateHandle?: string;
6969
select?: string[];
7070
onCreateHandle?: string;
71+
skipBeforeHooks?: boolean;
7172
},
7273
schema: Schema,
7374
betterAuthSchema: any
7475
) => {
7576
let data = args.input.data;
7677

77-
if (args.beforeCreateHandle) {
78+
if (!args.skipBeforeHooks && args.beforeCreateHandle) {
7879
const transformedData = await ctx.runMutation(
7980
args.beforeCreateHandle as FunctionHandle<'mutation'>,
8081
{
@@ -300,6 +301,7 @@ export const deleteOneHandler = async (
300301
};
301302
beforeDeleteHandle?: string;
302303
onDeleteHandle?: string;
304+
skipBeforeHooks?: boolean;
303305
},
304306
schema: Schema,
305307
betterAuthSchema: any
@@ -312,7 +314,7 @@ export const deleteOneHandler = async (
312314

313315
let hookDoc = doc;
314316

315-
if (args.beforeDeleteHandle) {
317+
if (!args.skipBeforeHooks && args.beforeDeleteHandle) {
316318
const transformedDoc = await ctx.runMutation(
317319
args.beforeDeleteHandle as FunctionHandle<'mutation'>,
318320
{
@@ -348,6 +350,7 @@ export const deleteManyHandler = async (
348350
paginationOpts: any;
349351
beforeDeleteHandle?: string;
350352
onDeleteHandle?: string;
353+
skipBeforeHooks?: boolean;
351354
},
352355
schema: Schema,
353356
betterAuthSchema: any
@@ -359,7 +362,7 @@ export const deleteManyHandler = async (
359362
await asyncMap(page, async (doc: any) => {
360363
let hookDoc = doc;
361364

362-
if (args.beforeDeleteHandle) {
365+
if (!args.skipBeforeHooks && args.beforeDeleteHandle) {
363366
const transformedDoc = await ctx.runMutation(
364367
args.beforeDeleteHandle as FunctionHandle<'mutation'>,
365368
{
@@ -401,12 +404,14 @@ export const createApi = <Schema extends SchemaDefinition<any, any>>(
401404
args: {
402405
beforeCreateHandle: v.optional(v.string()),
403406
input: v.union(
404-
...Object.entries(schema.tables).map(([model, table]) =>
405-
v.object({
406-
data: v.object((table as any).validator.fields),
407+
...Object.entries(schema.tables).map(([model, table]) => {
408+
const fields = partial((table as any).validator.fields);
409+
410+
return v.object({
411+
data: v.object(fields),
407412
model: v.literal(model),
408-
})
409-
)
413+
});
414+
})
410415
),
411416
select: v.optional(v.array(v.string())),
412417
onCreateHandle: v.optional(v.string()),

0 commit comments

Comments
 (0)