mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 18:46:19 +08:00
a66096cdf9
#### PR Dependency Tree * **PR #12946** * **PR #12947** 👈 * **PR #12948** This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal)
27 lines
687 B
TypeScript
27 lines
687 B
TypeScript
import { z, type ZodTypeAny } from 'zod';
|
|
|
|
export function dynamicSchema<Key extends string, Value extends ZodTypeAny>(
|
|
keyValidator: (key: string) => key is Key,
|
|
valueType: Value
|
|
) {
|
|
return z.preprocess(
|
|
record => {
|
|
// check it is a record
|
|
if (typeof record !== 'object' || record === null) {
|
|
return {};
|
|
}
|
|
|
|
return Object.entries(record)
|
|
.filter((data): data is [Key, unknown] => keyValidator(data[0]))
|
|
.reduce(
|
|
(acc, [key, value]) => {
|
|
acc[key] = value;
|
|
return acc;
|
|
},
|
|
{} as Record<Key, unknown>
|
|
);
|
|
},
|
|
z.record(z.custom<Key>(keyValidator), valueType)
|
|
);
|
|
}
|