Starlord
Starlord3mo ago

https://github.com/get-convex/convex-

https://github.com/get-convex/convex-helpers/blob/main/packages/convex-helpers/README.md#validator-utilities in validator utils i need to define table again to validate parameters. is it possible to use existing from schema?
import { Table } from "convex-helpers/server";
import {
literals,
partial,
deprecated,
brandedString,
} from "convex-helpers/validators";
import { omit, pick } from "convex-helpers";
import { Infer } from "convex/values";

// Define a validator that requires an Email string type.
export const emailValidator = brandedString("email");
// Define the Email type based on the branded string.
export type Email = Infer<typeof emailValidator>;

export const Account = Table("accounts", {
balance: nullable(v.bigint()),
status: literals("active", "inactive"),
email: emailValidator,

oldField: deprecated,
});

// convex/schema.ts
export default defineSchema({
accounts: Account.table.index("status", ["status"]),
//...
});
import { Table } from "convex-helpers/server";
import {
literals,
partial,
deprecated,
brandedString,
} from "convex-helpers/validators";
import { omit, pick } from "convex-helpers";
import { Infer } from "convex/values";

// Define a validator that requires an Email string type.
export const emailValidator = brandedString("email");
// Define the Email type based on the branded string.
export type Email = Infer<typeof emailValidator>;

export const Account = Table("accounts", {
balance: nullable(v.bigint()),
status: literals("active", "inactive"),
email: emailValidator,

oldField: deprecated,
});

// convex/schema.ts
export default defineSchema({
accounts: Account.table.index("status", ["status"]),
//...
});
3 Replies
erquhart
erquhart3mo ago
You can use your schema validators:
schema.tables.accounts.validator.fields.email
schema.tables.accounts.validator.fields.email
erquhart
erquhart3mo ago
There's a stack post that covers validator reuse: https://stack.convex.dev/types-cookbook
Types and Validators in TypeScript: A Convex Cookbook
It can be tough to wrangle types to behave how you want them to. Thankfully, Convex was designed to make the experience with types perfect. Learn why ...
Starlord
StarlordOP3mo ago
thanks figured already out

Did you find this page helpful?