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>;
|
||||
values?: unknown[];
|
||||
element?: z.ZodType;
|
||||
keyType?: z.ZodType;
|
||||
valueType?: z.ZodType;
|
||||
in?: z.ZodType;
|
||||
defaultValue?: unknown;
|
||||
}
|
||||
@@ -73,8 +75,11 @@ function sample(schema: z.ZodType): unknown {
|
||||
return sample(d.innerType!);
|
||||
case "array":
|
||||
return [sample(d.element!)];
|
||||
case "record":
|
||||
return { k: "v" };
|
||||
case "record": {
|
||||
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":
|
||||
return { any: 1 };
|
||||
case "union":
|
||||
|
||||
Reference in new issue
Block a user