Skip to content

Commit 87101ec

Browse files
authored
feat: schema.Any() (#60)
1 parent 69146d2 commit 87101ec

File tree

15 files changed

+1245
-384
lines changed

15 files changed

+1245
-384
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ Loro Mirror provides a declarative schema system that enables:
110110
- `schema.String<T extends string = string>(options?)` - String type with optional generic constraint
111111
- `schema.Number(options?)` - Number type
112112
- `schema.Boolean(options?)` - Boolean type
113+
- `schema.Any(options?)` - Runtime-inferred value/container type for JSON-like dynamic fields
113114
- `schema.Ignore(options?)` - Field that won't sync with Loro, useful for local computed fields
114115

116+
`schema.Any` options:
117+
118+
- `defaultLoroText?: boolean` (defaults to `false` for `Any` when omitted)
119+
- `defaultMovableList?: boolean` (inherits from global inference options)
120+
115121
- **Container Types**:
116122
- `schema.LoroMap(definition, options?)` - Object container that can nest arbitrary field schemas
117123
- Supports dynamic key-value definition with `catchall`: `schema.LoroMap({...}).catchall(valueSchema)`
@@ -331,6 +337,7 @@ Loro Mirror uses a typed schema to map your app state to Loro containers. Common
331337
- `schema.String(options?)`: string
332338
- `schema.Number(options?)`: number
333339
- `schema.Boolean(options?)`: boolean
340+
- `schema.Any(options?)`: JSON-like unknown (runtime-inferred)
334341
- `schema.Ignore(options?)`: exclude from sync (app-only)
335342
- `schema.LoroText(options?)`: rich text (`LoroText`)
336343
- `schema.LoroMap(definition, options?)`: object (`LoroMap`)

api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ All schema builders live under the `schema` namespace and are exported at the pa
104104
- `schema.String<T = string>(options?)`
105105
- `schema.Number(options?)`
106106
- `schema.Boolean(options?)`
107+
- `schema.Any(options?)` — runtime-inferred value/container type (useful for dynamic JSON-like fields)
107108
- `schema.Ignore(options?)` — present in state, ignored for Loro diffs/validation
108109

109110
- Containers
@@ -123,6 +124,10 @@ All schema builders live under the `schema` namespace and are exported at the pa
123124
- `description?: string`
124125
- `validate?: (value) => boolean | string` — custom validator message when not true
125126

127+
- `schema.Any` options (per-Any inference overrides)
128+
- `defaultLoroText?: boolean` — default `false` for `Any` when omitted (primitive string), overriding the global `inferOptions.defaultLoroText`.
129+
- `defaultMovableList?: boolean` — inherits from the global inference options unless explicitly set.
130+
126131
- Type inference
127132
- `InferType<S>` — state type produced by a schema
128133
- `InferInputType<S>` — input type accepted by `setState` (map `$cid` optional)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"test": "vitest run",
99
"lint": "oxlint --type-aware",
1010
"format": "prettier --write .",
11-
"typecheck": "pnpm -r typecheck"
11+
"typecheck": "pnpm -r typecheck",
12+
"check": "pnpm build && pnpm test && pnpm lint && pnpm typecheck"
1213
},
1314
"keywords": [
1415
"loro",

packages/core/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Types: `SyncDirection`, `UpdateMetadata`, `SetStateOptions`.
9292
### Schema Builder
9393

9494
- Root: `schema({ ...fields })`
95-
- Primitives: `schema.String`, `schema.Number`, `schema.Boolean`, `schema.Ignore`
95+
- Primitives: `schema.String`, `schema.Number`, `schema.Boolean`, `schema.Any`, `schema.Ignore`
9696
- Containers (core):
9797
- `schema.LoroMap({ ...fields })`
9898
- `schema.LoroList(itemSchema, idSelector?)`
@@ -113,6 +113,12 @@ Signatures:
113113

114114
SchemaOptions for any field: `{ required?: boolean; defaultValue?: unknown; description?: string; validate?: (value) => boolean | string }`.
115115

116+
Any options:
117+
118+
- `schema.Any({ defaultLoroText?: boolean; defaultMovableList?: boolean })`
119+
- `defaultLoroText` defaults to `false` for Any when omitted (primitive string), overriding the global `inferOptions.defaultLoroText`.
120+
- `defaultMovableList` inherits from the global inference options unless specified.
121+
116122
Reserved key `$cid`:
117123

118124
- `$cid` is injected into mirrored state for all `LoroMap` schemas; it is never written back to Loro and is ignored by diffs/updates. It’s useful as a stable identifier (e.g., `schema.LoroList(map, x => x.$cid)`).

0 commit comments

Comments
 (0)