@alexify/metaschema 3.1.0 → 3.2.0
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 +193 -1
- package/README.md +459 -20
- package/SECURITY.md +26 -7
- package/index.d.ts +120 -2
- package/package.json +38 -5
- package/src/codec.js +77 -43
- package/src/compile/code.js +1 -1
- package/src/compile/emit.js +32 -7
- package/src/compile/index.d.ts +6 -0
- package/src/compile/index.js +12 -1
- package/src/context.js +5 -9
- package/src/errors.js +9 -1
- package/src/formats/index.d.ts +19 -0
- package/src/formats/index.js +146 -0
- package/src/generators/fromjsonschema.js +11 -14
- package/src/generators/index.js +2 -1
- package/src/generators/jsonschema.js +2 -0
- package/src/issues.js +37 -20
- package/src/locales/en.js +11 -2
- package/src/locales/uk.js +10 -2
- package/src/metadata.js +3 -2
- package/src/model/diff.js +1 -0
- package/src/model/index.d.ts +14 -2
- package/src/model/index.js +13 -10
- package/src/model/infer.js +483 -0
- package/src/model/merge.js +1 -0
- package/src/model/restore.js +3 -3
- package/src/model/sample.js +6 -2
- package/src/model.js +5 -1
- package/src/preprocessor.js +1 -0
- package/src/prototypes/collections.js +4 -3
- package/src/prototypes/scalars.js +4 -4
- package/src/prototypes/union.js +2 -2
- package/src/result.js +6 -0
- package/src/rules.js +53 -4
- package/src/schema.js +17 -5
- package/src/snapshot.js +1 -1
- package/src/stream/index.d.ts +68 -0
- package/src/stream/index.js +247 -0
- package/src/struct.js +1 -1
- package/src/types.js +14 -1
- package/src/util.js +18 -0
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,197 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [3.2.0] - 2026-10-12
|
|
11
|
+
|
|
12
|
+
3.2 takes a schema to the records around it. The new `@alexify/metaschema/stream` subpath runs
|
|
13
|
+
a schema over a sequence of records, lazily and in order, as generators and as a
|
|
14
|
+
`TransformStream`; `infer` in the model subpath reads a definition out of the values that
|
|
15
|
+
exist, the reverse of `sample`; and the two performance candidates the review of 3.0 left were
|
|
16
|
+
measured and done: independent async validators are in flight together, and the passes of
|
|
17
|
+
`parse` allocate nothing per container they visit. The review of the release before its tag
|
|
18
|
+
added the `format` rule of strings with the `@alexify/metaschema/formats` subpath, fixed a value
|
|
19
|
+
parsed from JSON that could throw out of `check`, made the ReDoS note true, and closed two
|
|
20
|
+
unhandled rejections. Nothing breaks; the changes in behaviour (the order of the issues of an
|
|
21
|
+
async check with a sync validator after an unrelated promise, a pattern no longer run past
|
|
22
|
+
`length.max`, the message of `unexpected` capped at ten keys) are listed under Changed.
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
|
|
26
|
+
- **The `format` rule of strings and the `@alexify/metaschema/formats` subpath.**
|
|
27
|
+
`{ type: 'string', format: 'email' }` is a rule beside `length` and `pattern`: the core knows
|
|
28
|
+
the rule and keeps an empty table (`FORMATS`), and `register()` of the subpath fills it with
|
|
29
|
+
`uuid`, `email`, `date-time`, `date`, `time`, `ipv4`, `ipv6`, `uri` and `hostname` (the
|
|
30
|
+
expressions of ajv-formats' fast mode, linear and anchored, with the date and time ranges
|
|
31
|
+
checked and the leap second as RFC 3339 has it) and with the program's own
|
|
32
|
+
(`register({ phone: { test, examples, infer } })`), once per name, process-wide; a name the
|
|
33
|
+
table lacks is `ERR_INVALID_RULE` with the hint when the schema is built, an entry that is
|
|
34
|
+
not a format `ERR_INVALID_FORMAT`, a name registered again with another test
|
|
35
|
+
`ERR_FORMAT_REGISTERED`. A failed test is the new `format` issue (`params.format`, rendered
|
|
36
|
+
by both locales); the rule runs after the pattern, under the bound of `length.max`, and the
|
|
37
|
+
JIT reaches the test through its scope. `toJSONSchema` writes `format` (the built-in names
|
|
38
|
+
are JSON Schema's; the strict profile drops it, a `$jsonSchema` has none) and
|
|
39
|
+
`fromJSONSchema` reads a registered one back; `sample` draws one of the format's examples;
|
|
40
|
+
`infer` writes the first registered format every string of a place passes (`formats: false`
|
|
41
|
+
turns it off; `hostname`, which every word passes, is never inferred); `merge` refuses two
|
|
42
|
+
formats and `diff` classifies one as a pattern. A rule, not a type: a type named `email`
|
|
43
|
+
would make `{ email: 'string', name: 'string' }` read as a shorthand. The subpath is 1.3 KB
|
|
44
|
+
min+gzip on top of the core, gated at 2 KB; the typings add `FormatDefinition`, the issue
|
|
45
|
+
code and the two error codes.
|
|
46
|
+
- **`compiled(target)` in the `@alexify/metaschema/compile` subpath**: whether a schema (every
|
|
47
|
+
entity of a model) runs generated code now, in place of the internal symbol the guide pointed
|
|
48
|
+
at.
|
|
49
|
+
- **`result.toJSON()`**: `JSON.stringify(result)` writes `{ valid, issues }` (the verdict was a
|
|
50
|
+
getter and was left out); `util.inspect(model)` prints the snapshot, as a schema prints its
|
|
51
|
+
definition.
|
|
52
|
+
- **`sideEffects: false` in package.json**: nothing registers on import, so a bundler may drop
|
|
53
|
+
a subpath a program imports and never calls.
|
|
54
|
+
- **`tests/unit/graph.test.js`**: the module graph of `src/` has no cycles (CLAUDE.md drew it
|
|
55
|
+
by hand and said nothing checked it).
|
|
56
|
+
- **`infer(samples, options)` in the `@alexify/metaschema/model` subpath, the reverse of
|
|
57
|
+
`sample`.** A definition (or a `Schema`, with `as: 'schema'`) that accepts every sample
|
|
58
|
+
given, to bootstrap a model from the documents that exist: one pass summarizes the kinds of
|
|
59
|
+
every place, the records of a place fuse into one struct with optional keys (absent or
|
|
60
|
+
`undefined`) and nullable keys (`null` beside a type), arrays fuse their elements, values of
|
|
61
|
+
several kinds are a `union` with the record last, whole numbers are `integer`, a string of
|
|
62
|
+
few recurring values an `enum`, records whose keys are not names (or many sparse keys of one
|
|
63
|
+
kind) a dictionary `{ object: { string: T } }`, `Set`, `Map`, `Date` and bigint their types,
|
|
64
|
+
and what no type describes `any`; no bounds, patterns, references or tuples. Every key of the
|
|
65
|
+
data is a key of the definition (`__proto__` through `define`, a struct wrapped as
|
|
66
|
+
`{ schema }` where its first key would read otherwise, the root under its `kind`). The
|
|
67
|
+
samples are input: a sample nested deeper than `maxDepth` (128) is `ERR_INFER_DEPTH` with its
|
|
68
|
+
index and place, and the definition is checked against every sample before it is returned
|
|
69
|
+
(`ERR_INFER_MISMATCH`, with `error.index`, names the id field a stored kind adds over a field
|
|
70
|
+
of that name, or a bug). `register()` installs `Schema.infer`; the typings add
|
|
71
|
+
`InferOptions` (`as`, `name`, `kind`, `enum`, `integer`, `map`, `maxDepth`) and the two codes.
|
|
72
|
+
- **The `@alexify/metaschema/stream` subpath: a schema over a sequence of records.**
|
|
73
|
+
`checkEach(schema, records, options)` and `parseEach` run `check` or `parse` on every record
|
|
74
|
+
of an iterable, lazily, and yield `{ index, record, result }` in the order of the records;
|
|
75
|
+
`checkEachAsync` and `parseEachAsync` do the same over a sync or async iterable with the
|
|
76
|
+
async methods, keeping `options.concurrency` records in flight (one by default), still in
|
|
77
|
+
order, and observing the promises in flight when the iteration ends early; `maxInvalid` ends
|
|
78
|
+
an iteration after that many invalid records. `transform(schema, options)` is a
|
|
79
|
+
`TransformStream` (a global since Node.js 18, and in browsers) that passes every valid record
|
|
80
|
+
on as its parsed value (or as it is with `mode: 'check'`), hands an invalid one to
|
|
81
|
+
`onInvalid(record, result, index)`, or ends the stream with a `TypeError` of the new runtime
|
|
82
|
+
code `ERR_INVALID_RECORD` (with `index`, `record` and `result` on it). `register()` installs
|
|
83
|
+
the five as methods of `Schema`. The subpath is 1.0 KB min+gzip on top of the core, gated at
|
|
84
|
+
1.5 KB; the typings add `EachOptions`, `ParseEachOptions`, `EachAsyncOptions`,
|
|
85
|
+
`TransformOptions` and `RecordEntry<T, R>` (`T` the type of the records of the source:
|
|
86
|
+
`checkEach<T>(schema, records: Iterable<T>)` and its kin).
|
|
87
|
+
- **Bench scenarios for the async path and for `strip`** (`pnpm bench`): `checkAsync` with
|
|
88
|
+
sync, resolved and event-loop-bound validators, `parseAsync` with an async decoder, and
|
|
89
|
+
`parse` under `unknown: 'strip'` over a nested value and a list of 100 records, each on both
|
|
90
|
+
backends, through a `benchAsync` twin of the harness (one `await` per call).
|
|
91
|
+
|
|
92
|
+
### Changed
|
|
93
|
+
|
|
94
|
+
- **Independent async validators are in flight together.** `checkAsync` and `parseAsync` set a
|
|
95
|
+
validator aside uncalled only when a promise is pending under its path (the `validate` of a
|
|
96
|
+
struct whose field is still being checked, the schema's own); every other validator runs
|
|
97
|
+
where `check` runs it, during the walk, and the promises are awaited together after it, so
|
|
98
|
+
the latency of a check is the longest of the waits rather than their sum (`pnpm bench`:
|
|
99
|
+
four independent validators awaiting `setImmediate` went from 18 K to 66 K checks a second,
|
|
100
|
+
32 records with one each from 2.2 K to 45 K, four awaiting `setTimeout(1)` from 190 to
|
|
101
|
+
845; the sync validators and the resolved promises are unchanged). What
|
|
102
|
+
changes besides the latency: a synchronous validator met after an unrelated promise runs
|
|
103
|
+
during the walk, so the issues of the walk come first, in walk order, and the promises after
|
|
104
|
+
them, in field order; under a low `maxErrors`, and in a branch of a plain union that fails, a
|
|
105
|
+
validator may be called whose result is dropped, as in `check`. The issues recorded are the
|
|
106
|
+
same, on both backends.
|
|
107
|
+
- **A `pattern` is never run over a string longer than `length.max`.** The ReDoS note promised
|
|
108
|
+
that the bound keeps a backtracking expression fast, but every rule of a field ran: a string
|
|
109
|
+
past the bound got its length issue and then the pattern (2.5 s for `^(a+)+$` on 27
|
|
110
|
+
characters). Both backends skip the pattern on such a string (twice the bound in UTF-16
|
|
111
|
+
units with `unicode: true`), which is reported for its length alone: one issue where there
|
|
112
|
+
were two. A field without `length.max` is unchanged. A `format` is run under the same bound.
|
|
113
|
+
- **The message of `unexpected` lists the first ten keys** and says how many more; `params.keys`
|
|
114
|
+
keeps them all. The unexpected keys are one issue, so `maxErrors` never bounded the message,
|
|
115
|
+
and a value with a million keys the schema does not have rendered a million of them.
|
|
116
|
+
- **The gate of the `./model` subpath is 12 KB** (was 10): `infer` costs 2.3 KB min+gzip and
|
|
117
|
+
the subpath measures 11.7 KB (12.0 KB with the formats); the subpath is tooling, not the
|
|
118
|
+
validation path, as for the move from 8.5 to 10 KB in 3.1. **The core gate is 17.75 KB**
|
|
119
|
+
(was 17.5): the `format` rule, its issue and the fixes of the review measure 17.7 KB
|
|
120
|
+
together, and a gate the next fix cannot fit under is no gate, as in 3.1.
|
|
121
|
+
|
|
122
|
+
### Performance
|
|
123
|
+
|
|
124
|
+
- **The passes of `parse` and `serialize` allocate nothing per container they visit.** The
|
|
125
|
+
walkers of `codec.js` kept a state object per struct, list, dictionary or tuple whose bits a
|
|
126
|
+
pass wanted (`{ context, key, value, clone, put, out, pending }`), even where nothing
|
|
127
|
+
changed; the output under construction and the parts still decoding are locals of the walker
|
|
128
|
+
now, written through two small helpers. `pnpm bench`: `parse` of 100 records under
|
|
129
|
+
`unknown: 'strip'` from 61 K to 71 K a second on the closures and from 81 K to 100 K on the
|
|
130
|
+
JIT, a nested struct under `strip` from 1.9 M to 2.2 M on the JIT, defaults and a decoder
|
|
131
|
+
about 5% faster on both; the rest within the run-to-run band.
|
|
132
|
+
|
|
133
|
+
### Fixed
|
|
134
|
+
|
|
135
|
+
- **A value parsed from JSON could throw out of `check`.** A built-in was recognised by the
|
|
136
|
+
name of its constructor alone, and `{ "constructor": { "name": "Map" } }` as the value of a
|
|
137
|
+
`map`, `set` or `date` field was iterated, or asked for `getTime`, and threw a `TypeError`
|
|
138
|
+
out of `check`, `parse`, a union branch and both backends, where the contract is that
|
|
139
|
+
nothing in the value makes the check throw. The name is still tested first, then the shape
|
|
140
|
+
(an iterator for a `Map` or a `Set`, `getTime` for a `Date`), which JSON cannot fake; such a
|
|
141
|
+
value is a type issue. Since 2.0.
|
|
142
|
+
- **A walk that threw out of `checkAsync` or `parseAsync` left its promises unhandled.** A
|
|
143
|
+
getter of the value that throws (or, before the fix above, a crafted value) ended the call
|
|
144
|
+
with that error while the promise a validator had returned before it rejected with no
|
|
145
|
+
handler. The walk of an async check releases what it queued on the way out.
|
|
146
|
+
- **A queued record of the async stream pair could reject unhandled.** With a window of
|
|
147
|
+
records in flight, a later record whose parse threw while an earlier one still waited for
|
|
148
|
+
its validator fired `unhandledRejection` before its turn, then rejected the iteration all
|
|
149
|
+
the same; the promise is observed when it is queued.
|
|
150
|
+
- **Keys of untrusted input are shortened in every message.** The place of an `infer` error
|
|
151
|
+
(a key of a sample), of a `fromJSON` error (a key of a snapshot) and of a `fromJSONSchema`
|
|
152
|
+
error (a property name in the pointer) went into the message whole; they are truncated to
|
|
153
|
+
100 characters as a key of a value under check is. A snapshot index named `__proto__` is
|
|
154
|
+
written as a key instead of vanishing, and the `validate` method of a schema reports a
|
|
155
|
+
thrown `null` as an exception instead of throwing on it.
|
|
156
|
+
- **A failed branch of a plain union observed none of the promises it dropped.** A validator
|
|
157
|
+
of the branch that returned a promise was set aside, and when the branch failed on a later
|
|
158
|
+
field the entry was cut from the list without a handler, so its rejection was an
|
|
159
|
+
`unhandledRejection` while the value went on to match the next branch. Both backends now
|
|
160
|
+
release such entries (`release` in `issues.js`, reached by the generated code through the
|
|
161
|
+
scope), as the trial of `parse` and a `settle` stopped by `maxErrors` already did.
|
|
162
|
+
- **An object with no key that starts with a letter is a struct.** `Schema.from({})`,
|
|
163
|
+
`{ _id: 'string' }` and a nested `{ a: {} }` threw a bare `TypeError` from the preprocessor
|
|
164
|
+
(the first key is the first one that starts with a letter, and there was none); they are
|
|
165
|
+
structs of those keys, or of none.
|
|
166
|
+
- **A nested struct may have a field named `required`.** `{ a: { required: 'boolean' } }`
|
|
167
|
+
gave the field `a` the `required` of its nested definition (`'boolean'`), which showed in
|
|
168
|
+
`toJSON()` and in the snapshot; the key and the parsers decide, as everywhere else.
|
|
169
|
+
- **`fromJSONSchema` wraps a nested struct whose first letter-led key reads as a type.** A
|
|
170
|
+
nested object with the properties `_id` and `string` came back bare and was read as a
|
|
171
|
+
`string` field; one with no properties came back as `{}` and could not be built. The rule
|
|
172
|
+
the generator asks (`readsAsStruct` in `types.js`) follows the preprocessor's `firstKey`
|
|
173
|
+
now, and the model subpath shares it.
|
|
174
|
+
|
|
175
|
+
- **`CheckContext` declares `renamed`.** The context has carried the slot (whether the check
|
|
176
|
+
read a field from one of its aliases) since 3.1, and the migration guide says the type declares
|
|
177
|
+
every slot; `tests/types/index.test-d.ts` pins it now.
|
|
178
|
+
|
|
179
|
+
### Tooling
|
|
180
|
+
|
|
181
|
+
- **A cross-library benchmark, charted.** `pnpm bench:compare` replaces `pnpm bench
|
|
182
|
+
--compare-libs` (`bench/libs.js` is gone): metaschema on both backends beside Metarhia's
|
|
183
|
+
metaschema 2.2.2, zod, Valibot, ArkType, Effect Schema, TypeBox (1.x, `Compile` and `Value`),
|
|
184
|
+
Ajv, Yup, Joi and Superstruct, one adapter per library, each pair in a fresh process that
|
|
185
|
+
checks the verdicts before it times anything, median of five one-second samples. The
|
|
186
|
+
scenarios are the moltar object in its four modes, every issue of an invalid value, building
|
|
187
|
+
a schema and a cold start; `pnpm size:compare` bundles the same schema per library and checks
|
|
188
|
+
the bundle; `bench/releases.js` measures every release since Metarhia's 2.2.2 with one
|
|
189
|
+
harness. `pnpm bench:report` turns the results into SVG charts for the README and the tables
|
|
190
|
+
of the README and `docs/guide/benchmarks.md`; the docs render the same models as interactive
|
|
191
|
+
charts. The libraries are devDependencies (`@sinclair/typebox` 0.34 gave way to `typebox`
|
|
192
|
+
1.x).
|
|
193
|
+
- **A feature matrix of twenty alternatives.** `docs/guide/comparison.md` and the README compare
|
|
194
|
+
metaschema with TypeScript-first validators, JSON Schema tools, IDLs and Mongoose in one
|
|
195
|
+
table, every cell checked against the source of the published package; `pnpm docs:comparison`
|
|
196
|
+
renders it from `docs/comparison.json`.
|
|
197
|
+
- **Docs.** New pages: Benchmarks, Compared with alternatives and Architecture (mermaid diagrams
|
|
198
|
+
of the pipeline); the home page and the Why page were rewritten around "fast, zero-dependency";
|
|
199
|
+
the package description says so too.
|
|
200
|
+
|
|
10
201
|
## [3.1.0] - 2026-10-11
|
|
11
202
|
|
|
12
203
|
3.1 makes the model data. `model.toJSON()` writes a snapshot of a model, the contract other
|
|
@@ -1083,7 +1274,8 @@ published.
|
|
|
1083
1274
|
|
|
1084
1275
|
### [metaschema 0.x][upstream-0.x] - First generation of metaschema
|
|
1085
1276
|
|
|
1086
|
-
[unreleased]: https://github.com/Alexis-Technologies/metaschema/compare/v3.
|
|
1277
|
+
[unreleased]: https://github.com/Alexis-Technologies/metaschema/compare/v3.2.0...HEAD
|
|
1278
|
+
[3.2.0]: https://github.com/Alexis-Technologies/metaschema/compare/v3.1.0...v3.2.0
|
|
1087
1279
|
[3.1.0]: https://github.com/Alexis-Technologies/metaschema/compare/v3.0.0...v3.1.0
|
|
1088
1280
|
[3.0.0]: https://github.com/Alexis-Technologies/metaschema/compare/v2.0.0...v3.0.0
|
|
1089
1281
|
[2.0.0]: https://github.com/Alexis-Technologies/metaschema/compare/v1.0.0...v2.0.0
|