#Help with better-auth + convex
17 messages · Page 1 of 1 (latest)
The error you are encountering, Validator error: Expected one of object..., usually indicates a mismatch between the data being passed to the database and the schema defined for that table. In Convex, the internalAdapter.createUser method eventually triggers a database insert, and if the object contains extra fields or types that don't match your users table definition, it will throw this validation error [Convex test does not validate schemas].
Based on the knowledge sources, here are the likely causes and how to resolve them:
1. Schema Inconsistency
The error message shows you are passing fields like banned and role within the data object:
{"data":{"banned":false, ... "role":"user"},"model":"user"}
If your schema (either the default Better Auth schema or your custom one) does not explicitly define these fields (e.g., banned, role), the validation will fail [Issues when adding additional fields to the user schema in Convex + Better Auth].
To fix this:
- Ensure your
schema.tsincludes these fields in theusertable. - If you are using a "Local Install" and generating your schema via the Better Auth CLI, verify that the
generatedSchema.tsis correctly imported and thatnpx convex devhas successfully deployed those changes to your local test environment [Issues when adding additional fields to the user schema in Convex + Better Auth].
2. Using t.run for Setup
In Convex testing, while you can use t.run to modify data outside of functions, the internalAdapter methods are designed to work within the Convex environment [Using convex-test].
If you are trying to simulate a full user identity for your service functions, the recommended pattern is to:
- Insert the user document directly into the mock database using
ctx.db.insert("user", { ... })inside at.runblock. - Use
t.withIdentityto provide thesubject(theidfrom the created user) andissuerthat your functions expect [Authentication].
3. Alternative for "Full User Object"
If your functions rely on ctx.auth.getUserIdentity() and then fetch the full user from the database, you can satisfy this in tests by ensuring the subject in t.withIdentity matches the id of the user you manually inserted:
const user = await t.run(async (ctx) => {
const id = await ctx.db.insert("user", {
email: "[email protected]",
name: "Hello World",
emailVerified: true,
// ensure all fields match your schema exactly
});
return await ctx.db.get(id);
});
const asUser = t.withIdentity({ subject: user.id });
// Now call your service function
await asUser.query(api.myService.getUserProfile);
The knowledge sources do not explicitly mention the @convex-dev/better-auth/test helper's specific internal implementation for createUser, but the error is a standard Convex Validator error indicating your schema is more restrictive than the data being provided by the adapter.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@honest pendant Kapa.ai is still learning and improving, please let me know how I did by reacting below.
const id = await ctx.db.insert("user", {
email: "[email protected]",
name: "Hello World",
emailVerified: true,
// ensure all fields match your schema exactly
});
Wouldn't work, as I don't have a "user" table. I have better-auth installed as a local component. The auto generated api does not contain a "user" table.
@formal crypt
Let me check the knowledge base for you again. One moment! :books:
Sorry @honest pendant, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?
@formal crypt
Let me check the knowledge base for you again. One moment! :books:
Sorry @honest pendant, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?
It seems to be the admin plugin.
Somehow, the schema is not applied properly, even though generating the schema through the CLI generates the role.
@formal crypt
Let me check the knowledge base for you again. One moment! :books:
Sorry @honest pendant, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?
@formal crypt
Let me check the knowledge base for you again. One moment! :books: