test(contracts): generate typed record samples from schemas
This commit is contained in:
1 parent
1475ffcce4
commit
f3d28f056b
1 file changed
+7
-2
@@ -14,6 +14,8 @@ interface Def {
|
|||||||
entries?: Record<string, string>;
|
entries?: Record<string, string>;
|
||||||
values?: unknown[];
|
values?: unknown[];
|
||||||
element?: z.ZodType;
|
element?: z.ZodType;
|
||||||
|
keyType?: z.ZodType;
|
||||||
|
valueType?: z.ZodType;
|
||||||
in?: z.ZodType;
|
in?: z.ZodType;
|
||||||
defaultValue?: unknown;
|
defaultValue?: unknown;
|
||||||
}
|
}
|
||||||
@@ -73,8 +75,11 @@ function sample(schema: z.ZodType): unknown {
|
|||||||
return sample(d.innerType!);
|
return sample(d.innerType!);
|
||||||
case "array":
|
case "array":
|
||||||
return [sample(d.element!)];
|
return [sample(d.element!)];
|
||||||
case "record":
|
case "record": {
|
||||||
return { k: "v" };
|
const key = def(d.keyType!);
|
||||||
|
const keys = key.type === "enum" ? Object.values(key.entries!) : [String(sample(d.keyType!))];
|
||||||
|
return Object.fromEntries(keys.map((k) => [k, sample(d.valueType!)]));
|
||||||
|
}
|
||||||
case "unknown":
|
case "unknown":
|
||||||
return { any: 1 };
|
return { any: 1 };
|
||||||
case "union":
|
case "union":
|
||||||
|
|||||||
Reference in new issue
Block a user