mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 18:46:19 +08:00
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)
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
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)
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './attribute-renderer.js';
|
||||
export * from './delta-convert.js';
|
||||
export * from './dynamic-schema.js';
|
||||
export * from './embed.js';
|
||||
export * from './guard.js';
|
||||
export * from './point-conversion.js';
|
||||
|
||||
Reference in New Issue
Block a user