cyremur
cyremur2y ago

Alllso if you can infer a schema from a

Alllso if you can infer a schema from a couple instantiated objects, can you also do it from a typescript typedef? Would love if there's a page in convex dev Portal where I can copy paste my typedef and get a working schema
6 Replies
ballingt
ballingt2y ago
We've prototyped this too! It's hard to do for all TypeScript types but we could loudly fail when it doesn't work.
cyremur
cyremurOP2y ago
I think simple is super fine, it's just this has been one of the most annoying bits that kept me from embracing zod likes earlier
ballingt
ballingt2y ago
If we go further this direction we could let you specify argument validators with TypeScript types, but there are a lot of details to get right to make it not too confusing
cyremur
cyremurOP2y ago
like all I want is something like an online json prettifier interface where I can enter this
export interface Card {
id: number;
name: string;
cost: string;
type: "Avatar" | "Creature" | "Spell";
descriptions: string[];
power: number;
toughness: number;
image: string;
theme: { primary: string; light: string; dark: string };
}
export interface Card {
id: number;
name: string;
cost: string;
type: "Avatar" | "Creature" | "Spell";
descriptions: string[];
power: number;
toughness: number;
image: string;
theme: { primary: string; light: string; dark: string };
}
and get this
export const vCard = v.object({
cost: v.string(),
descriptions: v.array(v.string()),
id: v.float64(),
image: v.string(),
name: v.string(),
power: v.float64(),
theme: v.object({
dark: v.string(),
light: v.string(),
primary: v.string(),
}),
toughness: v.float64(),
type: v.union(v.literal("Avatar"), v.literal("Creature"), v.literal("Spell")),
});
export const vCard = v.object({
cost: v.string(),
descriptions: v.array(v.string()),
id: v.float64(),
image: v.string(),
name: v.string(),
power: v.float64(),
theme: v.object({
dark: v.string(),
light: v.string(),
primary: v.string(),
}),
toughness: v.float64(),
type: v.union(v.literal("Avatar"), v.literal("Creature"), v.literal("Spell")),
});
btw this is actually what I already got from your tech (except the union literal thing) by loading up the db and using the schema generator
philipsman
philipsman2y ago
@cyremur what stack did u port ur game from
cyremur
cyremurOP2y ago
I used a redux / liveblocks state sync for multiplayer before

Did you find this page helpful?