Files
AFFiNE-Mirror/blocksuite/framework/std/src/inline/utils/dynamic-schema.ts
T
L-Sun a66096cdf9 refactor(editor): support dynamic text attribute key (#12947)
#### PR Dependency Tree


* **PR #12946**
  * **PR #12947** 👈
    * **PR #12948**

This tree was auto-generated by
[Charcoal](https://github.com/danerwilliams/charcoal)
2025-07-02 08:09:01 +00:00

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)
);
}