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:
L-Sun
2025-07-02 16:09:01 +08:00
committed by GitHub
parent facf6ee28b
commit a66096cdf9
11 changed files with 101 additions and 49 deletions
@@ -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';