arktypea
arktype10mo ago
Stadly

Schemas with cyclic references

In Zod, I can create circular schemas like this:

const typeABaseSchema = z.strictObject({
  type: z.literal("a"),
});
const typeASchema = typeABaseSchema.extend({
  nested: z.lazy(() => typeSchema).optional(),
});
type TypeADef = z.infer<typeof typeABaseSchema> & {
  nested?: TypeDef | undefined;
};

const typeBBaseSchema = z.strictObject({
  type: z.literal("b"),
});
const typeBSchema = typeBBaseSchema.extend({
  nested: z.lazy(() => typeSchema).optional(),
});
type TypeBDef = z.infer<typeof typeBBaseSchema> & {
  nested?: TypeDef | undefined;
};

const typeA: z.ZodType<TypeADef> = typeASchema;
const typeB: z.ZodType<TypeBDef> = typeBSchema;
const typeSchema = z.union([typeA, typeB]);
type TypeDef = z.infer<typeof typeSchema>;

typeSchema.parse({ type: "a", nested: { type: "b" } });
typeSchema.parse({ type: "a", nested: { type: "a", nested: { type: "b" } } });
typeSchema.parse({ type: "b", nested: { type: "b", nested: { type: "a" } } });


I've not been able to achieve the same thing using ArkType. Can anyone help? I need both typeASchema, typeBSchema and typeSchema, so cannot just create a single large scope.
Was this page helpful?