@gialicus/smart-object 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +13 -8
- package/dist/smart-object/factory.d.ts +2 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
11
11
|
|
|
12
12
|
- Support for `z.date()` and `z.coerce.date()` with ISO 8601 operation values and `Date` getters
|
|
13
13
|
- Support for `z.bigint()`, `z.map` (string keys), and `z.set` via explicit JSON-safe codecs
|
|
14
|
-
- Per-entry API for `z.record` fields: `get{Field}Entry`, `set{Field}Entry`, `delete{Field}Entry`
|
|
14
|
+
- Per-entry API for `z.record` and string-key `z.map` fields: `get{Field}Entry`, `set{Field}Entry`, `delete{Field}Entry`
|
|
15
15
|
- Union variant switching: `switchVariant` and generated `switchTo{Variant}` for discriminated unions
|
|
16
16
|
- Root schema variants: `z.intersection` and `z.lazy`
|
|
17
17
|
- Zod schema introspection module (`src/zod-introspect.ts`) and codec layer (`src/smart-object/codecs.ts`)
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Typed TypeScript objects backed by Zod schemas, with an RFC 6902 operation log f
|
|
|
8
8
|
npm install @gialicus/smart-object zod
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
Dependencies: [Zod](https://zod.dev) (peer dependency — schema validation) and [fast-json-patch](https://github.com/Starcounter-Jack/JSON-Patch) (RFC 6902 patch application, bundled).
|
|
11
|
+
Dependencies: [Zod](https://zod.dev) `^4.0.0` (peer dependency — schema validation) and [fast-json-patch](https://github.com/Starcounter-Jack/JSON-Patch) (RFC 6902 patch application, bundled).
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
@@ -99,13 +99,13 @@ Factory that accepts a Zod schema and returns an instantiable class.
|
|
|
99
99
|
| `switchVariant(value)` | `(variant) => void` | Replaces the entire active variant after full schema validation |
|
|
100
100
|
| `switchTo{Variant}(fields)` | `(fields) => void` | Discriminated unions only — switches to a variant without repeating the discriminator (e.g. `switchToScroll({ delta: 5 })`) |
|
|
101
101
|
|
|
102
|
-
**Record field extras** (for each `z.record(...)` field `tags`):
|
|
102
|
+
**Record and Map field extras** (for each `z.record(...)` field or string-key `z.map(...)` field `tags`):
|
|
103
103
|
|
|
104
104
|
| Member | Type | Description |
|
|
105
105
|
|--------|------|-------------|
|
|
106
|
-
| `getTagsEntry(key)` | `(key: string) => V \| undefined` | Reads a single
|
|
106
|
+
| `getTagsEntry(key)` | `(key: string) => V \| undefined` | Reads a single entry |
|
|
107
107
|
| `setTagsEntry(key, value)` | `(key: string, value: V) => void` | Validates and patches a single entry (`/tags/{key}`) |
|
|
108
|
-
| `deleteTagsEntry(key)` | `(key: string) => void` | Removes
|
|
108
|
+
| `deleteTagsEntry(key)` | `(key: string) => void` | Removes an entry |
|
|
109
109
|
|
|
110
110
|
**Instance members:**
|
|
111
111
|
|
|
@@ -143,13 +143,15 @@ RFC 6902 operation emitted by [fast-json-patch](https://github.com/Starcounter-J
|
|
|
143
143
|
### Exported types
|
|
144
144
|
|
|
145
145
|
- `Operation` — JSON Patch operation (re-export from `fast-json-patch`)
|
|
146
|
+
- `SmartObjectSchema` — union of Zod schema types accepted by `SmartObject()`
|
|
147
|
+
- `SmartObjectErrorCode` — `"InvalidValue"` \| `"InvalidUnionField"` \| `"InvalidReplay"` \| `"UnsupportedSchema"`
|
|
146
148
|
- `SetMethods<T>` — mapped type of inferred `set*` methods for shape `T`
|
|
147
149
|
- `SetMethodsUnion<T>` — `set*` methods for union root schemas
|
|
148
150
|
- `AllKeys<T>` — all keys across union members
|
|
149
151
|
- `UnionDataShape<U>` — flattened data shape for union roots
|
|
150
152
|
- `VariantSwitchMethods<T>` — `switchVariant` for union roots
|
|
151
153
|
- `DiscriminatedVariantSwitchMethods<T, D>` — `switchVariant` plus generated `switchTo*` methods
|
|
152
|
-
- `RecordFieldMethods<T>` — dynamic entry accessors for `z.record` fields
|
|
154
|
+
- `RecordFieldMethods<T>` — dynamic entry accessors for `z.record` and string-key `z.map` fields
|
|
153
155
|
- `OperationsAccessor` — `operations` and `clearOperations()`
|
|
154
156
|
- `SnapshotAccessor<T>` — `toJSON()`
|
|
155
157
|
- `SmartObjectConstructor<T>` — constructor type including `fromOperations`
|
|
@@ -160,7 +162,7 @@ RFC 6902 operation emitted by [fast-json-patch](https://github.com/Starcounter-J
|
|
|
160
162
|
- **Partial discriminator write** — Changing a discriminated union discriminator alone via `setType(...)` without providing the new variant fields throws `SmartObjectError`. Use `switchVariant(...)` or `switchTo{Variant}(...)` instead.
|
|
161
163
|
- **Union field on wrong variant** — Setting a field that does not exist on the active variant throws `SmartObjectError`.
|
|
162
164
|
- **Date fields** — `z.date()` and `z.coerce.date()` are supported; operations store ISO 8601 strings while getters return `Date` instances.
|
|
163
|
-
- **Map, Set, and bigint** — `z.map` (string keys), `z.set`, and `z.bigint()` are supported with explicit codecs; operations use JSON-safe plain objects, arrays, and decimal strings respectively.
|
|
165
|
+
- **Map, Set, and bigint** — `z.map` (string keys), `z.set`, and `z.bigint()` are supported with explicit codecs; operations use JSON-safe plain objects, arrays, and decimal strings respectively. String-key `z.map` fields also expose per-entry `get/set/delete*Entry` methods; whole-field `set*` is used for `z.set` and for map fields with non-string keys (not supported). Non-string map keys are not supported.
|
|
164
166
|
- **Transforms** — `z.transform` / `z.pipe` with preprocessing work at runtime; operations store the **output** value after validation. TypeScript setter input types may not reflect transforms.
|
|
165
167
|
|
|
166
168
|
## Design rationale
|
|
@@ -177,6 +179,7 @@ RFC 6902 operation emitted by [fast-json-patch](https://github.com/Starcounter-J
|
|
|
177
179
|
- [`examples/person.ts`](examples/person.ts) — primitives, nested objects, and arrays
|
|
178
180
|
- [`examples/event.ts`](examples/event.ts) — discriminated union root
|
|
179
181
|
- [`examples/profile.ts`](examples/profile.ts) — generic union root
|
|
182
|
+
- [`examples/record.ts`](examples/record.ts) — `z.record` and string-key `z.map` entry API
|
|
180
183
|
|
|
181
184
|
## Project structure
|
|
182
185
|
|
|
@@ -194,7 +197,7 @@ smart-object/
|
|
|
194
197
|
│ ├── instance-state.ts # WeakMap-backed instance storage
|
|
195
198
|
│ ├── read-field.ts # Defensive getter reads
|
|
196
199
|
│ ├── json-patch.ts # fast-json-patch wrapper + Date-safe deepClone
|
|
197
|
-
│ ├── codecs.ts #
|
|
200
|
+
│ ├── codecs.ts # JSON-safe codecs for Date, Map, Set, and bigint
|
|
198
201
|
│ ├── apply-operations.ts # Replay and rollback
|
|
199
202
|
│ ├── union-variant.ts # Union variant matching
|
|
200
203
|
│ ├── define-prototype.ts # Getter/setter prototype setup
|
|
@@ -206,7 +209,8 @@ smart-object/
|
|
|
206
209
|
├── examples/
|
|
207
210
|
│ ├── person.ts
|
|
208
211
|
│ ├── event.ts
|
|
209
|
-
│
|
|
212
|
+
│ ├── profile.ts
|
|
213
|
+
│ └── record.ts
|
|
210
214
|
├── tests/
|
|
211
215
|
│ ├── fixtures/
|
|
212
216
|
│ │ ├── person.ts
|
|
@@ -228,6 +232,7 @@ smart-object/
|
|
|
228
232
|
│ │ ├── schema-variants.test.ts
|
|
229
233
|
│ │ ├── record-fields.test.ts
|
|
230
234
|
│ │ ├── date-codec.test.ts
|
|
235
|
+
│ │ ├── complex-schema-types.test.ts
|
|
231
236
|
│ │ ├── intersection-lazy.test.ts
|
|
232
237
|
│ │ └── types.test.ts
|
|
233
238
|
│ └── zod-introspect.test.ts
|
|
@@ -13,8 +13,8 @@ import type { SmartObjectConstructor } from "../types.js";
|
|
|
13
13
|
* Initial construction does not emit operations because that state is the baseline
|
|
14
14
|
* every subsequent patch is measured against.
|
|
15
15
|
*
|
|
16
|
-
* @typeParam T - Zod
|
|
17
|
-
* @param zodSchema - A `z.object({ ... })`, `z.union([...])`, or `z.
|
|
16
|
+
* @typeParam T - Zod schema that defines the instance shape (object, union, intersection, or lazy root)
|
|
17
|
+
* @param zodSchema - A `z.object({ ... })`, `z.union([...])`, `z.discriminatedUnion(...)`, `z.intersection(...)`, or `z.lazy(...)` schema
|
|
18
18
|
* @returns An instantiable class with types inferred from the schema
|
|
19
19
|
*
|
|
20
20
|
* @example
|
package/package.json
CHANGED