You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Loro Mirror provides a declarative schema system that enables:
101
+
102
+
-**Type Inference**: Automatically infer TypeScript types for your application state from the schema
103
+
-**Runtime Validation**: Validate data structure and types during `setState` operations or synchronization
104
+
-**Default Value Generation**: Generate sensible default values based on the schema definition
105
+
106
+
#### Core Concepts
107
+
108
+
-**Root Schema**: The root object defined via `schema({...})`, containing only Loro container types (Map/List/Text/MovableList).
109
+
-**Field Schema**: A combination of primitive types (string, number, boolean), ignore fields, and Loro containers.
110
+
-**Schema Options (`SchemaOptions`)**:
111
+
-**`required?: boolean`**: Whether the field is required (default: `true`).
112
+
-**`defaultValue?: unknown`**: Default value for the field.
113
+
-**`description?: string`**: Description of the field.
114
+
-**`validate?: (value) => boolean | string`**: Custom validation function. Return `true` for valid values, or a string as error message for invalid ones.
115
+
116
+
#### Schema Definition API
117
+
118
+
-**Primitive Types**:
119
+
-`schema.String<T extends string = string>(options?)` - String type with optional generic constraint
120
+
-`schema.Number(options?)` - Number type
121
+
-`schema.Boolean(options?)` - Boolean type
122
+
-`schema.Ignore(options?)` - Field that won't sync with Loro, useful for local computed fields
123
+
124
+
-**Container Types**:
125
+
-`schema.LoroMap(definition, options?)` - Object container that can nest arbitrary field schemas
126
+
- Supports dynamic key-value definition with `catchall`: `schema.LoroMap({...}).catchall(valueSchema)`
127
+
-`schema.LoroMapRecord(valueSchema, options?)` - Equivalent to `LoroMap({}).catchall(valueSchema)` for homogeneous maps
128
+
-`schema.LoroList(itemSchema, idSelector?, options?)` - Ordered list container
> **Note**: If you need optional custom string types like `{ id?: UserId }`, you currently need to explicitly define it as `schema.String<UserId>({ required: false })`
- Fields defined with `schema.Ignore()` won't sync with Loro, commonly used for derived/cached fields. Runtime validation always passes for these fields.
0 commit comments