Tiger šŸ…
Tiger šŸ…ā€¢4mo ago

lets try it

lets try it
17 Replies
jamalsoueidan
jamalsoueidanā€¢4mo ago
show your code and the problem
Tiger šŸ…
Tiger šŸ…OPā€¢4mo ago
i just want a simple update user function take a user schema
jamalsoueidan
jamalsoueidanā€¢4mo ago
import { partial } from "convex-helpers/validators;
Tiger šŸ…
Tiger šŸ…OPā€¢4mo ago
and update it one thats partial
jamalsoueidan
jamalsoueidanā€¢4mo ago
show me code i will go toilet in mean time add code
Tiger šŸ…
Tiger šŸ…OPā€¢4mo ago
installed it, partial doesnt exist the code is dead simple export const updateUser = mutation({ args: { data: v.optional(), }, handler: async (ctx, args) => { const userId = await getAuthUserId(ctx) if (userId === null) { throw new ConvexError('Unauthorized') } if (!args.data) { return } await ctx.db.patch(userId, { ...args.data, }) }, }) its not even about my code here lol ok this works now weirdly
export const updateUser = mutation({
args: {
data: v.optional(userSchema),
},
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx)
if (userId === null) {
throw new ConvexError('Unauthorized')
}

if (!args.data) {
return
}

await ctx.db.patch(userId, {
...args.data,
})
},
})
export const updateUser = mutation({
args: {
data: v.optional(userSchema),
},
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx)
if (userId === null) {
throw new ConvexError('Unauthorized')
}

if (!args.data) {
return
}

await ctx.db.patch(userId, {
...args.data,
})
},
})
actually no all fields are now required
jamalsoueidan
jamalsoueidanā€¢4mo ago
show me also your userSchema
Tiger šŸ…
Tiger šŸ…OPā€¢4mo ago
its just a v.object
jamalsoueidan
jamalsoueidanā€¢4mo ago
if you make args partial, and the userschema is required, it wouldnt work just to let you know
Tiger šŸ…
Tiger šŸ…OPā€¢4mo ago
cc @Wayne @Michal Srb i guess more of a reason to fully support zod for schemas, or improving the v one ill just create a schema thats all optional cant waste my time on this actually its even better bc not all fields are updatable so its ok
jamalsoueidan
jamalsoueidanā€¢4mo ago
import { partial } from "convex-helpers/validators"
Tiger šŸ…
Tiger šŸ…OPā€¢4mo ago
that looks right nice
/**
* partial helps you define an object of optional validators more concisely.
*
* e.g. `partial({a: v.string(), b: v.number()})` is equivalent to
* `{a: v.optional(v.string()), b: v.optional(v.number())}`
*
* @param obj The object of validators to make optional. e.g. {a: v.string()}
* @returns A new object of validators that can be the value or undefined.
*/
export const partial = (obj) => {
return Object.fromEntries(Object.entries(obj).map(([k, vv]) => [
k,
vv.isOptional === "optional" ? vv : v.optional(vv),
]));
};
/**
* partial helps you define an object of optional validators more concisely.
*
* e.g. `partial({a: v.string(), b: v.number()})` is equivalent to
* `{a: v.optional(v.string()), b: v.optional(v.number())}`
*
* @param obj The object of validators to make optional. e.g. {a: v.string()}
* @returns A new object of validators that can be the value or undefined.
*/
export const partial = (obj) => {
return Object.fromEntries(Object.entries(obj).map(([k, vv]) => [
k,
vv.isOptional === "optional" ? vv : v.optional(vv),
]));
};
jamalsoueidan
jamalsoueidanā€¢4mo ago
Avoid tagging staff unless specifically instructed.
Tiger šŸ…
Tiger šŸ…OPā€¢4mo ago
still getting an error:
Type '{ name: VString<string | undefined, "optional">; email: VString<string | undefined, "optional">; username: VString<string | undefined, "optional">; ... 7 more ...; lang: VUnion<...>; }' is not assignable to type 'Validator<any, OptionalProperty, any>'.ts(2322)
Type '{ name: VString<string | undefined, "optional">; email: VString<string | undefined, "optional">; username: VString<string | undefined, "optional">; ... 7 more ...; lang: VUnion<...>; }' is not assignable to type 'Validator<any, OptionalProperty, any>'.ts(2322)
god dammit ill just go with my previous approach
jamalsoueidan
jamalsoueidanā€¢4mo ago
relax šŸ™‚ where do you get this error i need your userschema,,,your functions, and where is your error it shouldnt be so hard šŸ™‚
Tiger šŸ…
Tiger šŸ…OPā€¢4mo ago
fucking exactly šŸ˜­ its ok tho its just three fields for now
jamalsoueidan
jamalsoueidanā€¢4mo ago
what you mean three fields

Did you find this page helpful?