@alexify/metaschema 3.2.0 → 3.3.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 +122 -1
- package/README.md +155 -79
- package/index.d.ts +377 -1
- package/package.json +17 -2
- package/src/codec.js +164 -33
- package/src/coerce/index.d.ts +34 -0
- package/src/coerce/index.js +352 -0
- package/src/context.js +6 -1
- package/src/derive/index.d.ts +86 -0
- package/src/derive/index.js +266 -0
- package/src/errors.js +23 -4
- package/src/formats/index.d.ts +20 -8
- package/src/formats/index.js +450 -39
- package/src/generators/fromjsonschema.js +7 -2
- package/src/generators/jsonschema.js +46 -14
- package/src/kinds.js +11 -2
- package/src/lint.js +24 -0
- package/src/metadata.js +4 -0
- package/src/model/data.js +17 -0
- package/src/model/merge.js +9 -6
- package/src/model/sample.js +2 -3
- package/src/model.js +29 -3
- package/src/mongodb/bson.js +118 -0
- package/src/mongodb/document.js +209 -0
- package/src/mongodb/index.d.ts +133 -0
- package/src/mongodb/index.js +24 -0
- package/src/mongodb/indexes.js +79 -0
- package/src/mongodb/update.js +258 -0
- package/src/prototypes/abstract.js +9 -3
- package/src/schema.js +36 -23
- package/src/types.js +20 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,126 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [3.3.0] - 2026-10-14
|
|
11
|
+
|
|
12
|
+
3.3 closes the gaps the comparison with the alternatives showed. `parse` converts the text of a
|
|
13
|
+
query string, a form or a file to the types of the fields when asked (coercion), schemas are
|
|
14
|
+
derived from schemas (`pick`, `omit`, `partial`, `required`, `extend`, `keyof`), the string
|
|
15
|
+
formats grow from nine to 35, and the `@alexify/metaschema/mongodb` subpath is the schema side
|
|
16
|
+
of a MongoDB program without a dependency on the driver. The review of the release before its
|
|
17
|
+
tag made the converters and five formats linear in the stack as well as in time on strings of
|
|
18
|
+
megabytes, capped the `regex` format, and closed the ways a document could carry a foreign
|
|
19
|
+
`_id` or throw on `null`. The changes in behaviour (`coerce` is a reserved key, `infer` writes
|
|
20
|
+
the more specific formats, a format of the program under the name of a new built-in takes its
|
|
21
|
+
place) are listed under Changed.
|
|
22
|
+
|
|
23
|
+
### Added
|
|
24
|
+
|
|
25
|
+
- **Coercion.** `parse(value, { coerce: true })`, the kind metadata
|
|
26
|
+
`{ Form: { coerce: true } }` (so `~standard.validate` too) or a field's `coerce: true|false`
|
|
27
|
+
converts the input to the types of the fields before the check: decimal strings to numbers
|
|
28
|
+
and bigints, `'true'`/`'false'`/`'1'`/`'0'` to booleans, ISO dates with their zone (and epoch
|
|
29
|
+
milliseconds) to `Date`, a single value to a list, an array to a `Set`, an object to a `Map`,
|
|
30
|
+
the written form of an enum member to the member. The converters are strict and never touch
|
|
31
|
+
the empty string; a value a union branch accepts as it is is not converted; `check` never
|
|
32
|
+
converts, and `Infer` does not change. The core has the policy and a step in the pass before
|
|
33
|
+
the check; the converters are the new `@alexify/metaschema/coerce` subpath's
|
|
34
|
+
(`register(custom)` adds the program's own by type name, an alias type converts as its base),
|
|
35
|
+
and `coerce(schema, value)` is that pass alone. A parse that coerces before `register()` is
|
|
36
|
+
`ERR_NOT_REGISTERED` as it enters the schema, whatever the value holds (an empty body does
|
|
37
|
+
not hide a missing `register()`); `register(custom)` checks every entry before it writes
|
|
38
|
+
any; `ERR_INVALID_COERCION` and `ERR_COERCION_REGISTERED` are new codes. The converters are
|
|
39
|
+
linear on hostile strings (a run of a hundred thousand digits that is not a number costs
|
|
40
|
+
nothing), and `toMap` converts a plain object or a list of pairs only.
|
|
41
|
+
`register(custom, { onError })` is told of a converter that threw (the value is left to the
|
|
42
|
+
check either way), the policy is read from own keys only (a `coerce` set on
|
|
43
|
+
`Object.prototype` elsewhere turns nothing on), and `coerce` on a reference checked as an id
|
|
44
|
+
or on a field of `any`, `unknown`, `json` or `null` is a `dead-coerce` warning. The converter
|
|
45
|
+
of a field is resolved once and kept with the field (a JIT parse that coerces is 11% faster
|
|
46
|
+
than with a lookup per value, a parse of enums of 32 members 2.3 times: the members and their
|
|
47
|
+
written forms are indexed once). `pnpm bench` has rows for a nested struct, plain unions,
|
|
48
|
+
enums and a plain and a coercing parse in turn, and for `toDocument`/`fromDocument`.
|
|
49
|
+
`coerce(schema, value, { report })` fills the array given with the path of every key the
|
|
50
|
+
pass changed; `new Model(types, entities, database, { coercions })` gives a model converters
|
|
51
|
+
of its own type names, read before the process-wide table for the fields of its entities
|
|
52
|
+
(what an isolated registry is to the types); the branches of a plain union are prepared
|
|
53
|
+
first and tried only when one of them changes the value, so a parse over a union of records
|
|
54
|
+
that nest unions is linear where nothing applies (4 times faster at a depth of 12).
|
|
55
|
+
- **`@alexify/metaschema/derive`.** `pick`, `omit`, `partial` (`keys`, `deep`, `defaults`),
|
|
56
|
+
`required`, `extend` and `keyof` build a new Schema from the definition a schema was built
|
|
57
|
+
from: a stored kind or a projection becomes a struct without the id of the kind (unless
|
|
58
|
+
named), `partial` leaves the defaults out, `pick`, `omit` and `partial` drop the schema's own
|
|
59
|
+
functions, `extend` replaces fields in place (where `merge` unifies), indexes stay while their
|
|
60
|
+
columns do. `register()` installs them as methods (`schema.omit(['id']).partial()`), typed by
|
|
61
|
+
the definition. `ERR_UNKNOWN_FIELD` names a key the schema has no field of.
|
|
62
|
+
- **26 string formats.** `ulid`, `jwt`, `nanoid`, `cuid2`, `iso-date-time`, `iso-time`,
|
|
63
|
+
`duration`, `cidrv4`, `cidrv6`, `mac`, `url`, `uri-reference`, `uri-template`,
|
|
64
|
+
`json-pointer`, `relative-json-pointer`, `regex`, `e164`, `iban`, `hex-color`, `base64`,
|
|
65
|
+
`base64url`, `hex`, `emoji`, `semver`, `slug` and `credit-card` (the expressions of
|
|
66
|
+
ajv-formats, zod, valibot and semver.org, held to them value by value). A format entry may
|
|
67
|
+
carry a `pattern`, which `toJSONSchema` writes beside a name JSON Schema does not know (the
|
|
68
|
+
one assertion of the mongodb target) and `fromJSONSchema` drops again; OpenAPI's `byte` is
|
|
69
|
+
`base64`. No built-in test overflows the stack of the regex engine on a string of
|
|
70
|
+
megabytes: `email`, `json-pointer`, `relative-json-pointer`, `uri-template`, `slug` and
|
|
71
|
+
`semver` are read by a scanner with the same verdicts, and a `regex` longer than 4096
|
|
72
|
+
characters is not one (compiling it costs memory in proportion). `register()` returns the
|
|
73
|
+
names of the built-in formats the call replaced with the program's own.
|
|
74
|
+
- **`@alexify/metaschema/mongodb`.** The BSON types `objectId`, `decimal128` and `binary`
|
|
75
|
+
(recognized without importing bson), `register({ bson })` with the program's driver,
|
|
76
|
+
`toDocument`/`fromDocument` (the id of a stored record as `_id`, ids of references as
|
|
77
|
+
ObjectIds, and back), `indexesOf` for `createIndexes`, `checkUpdate`/`toUpdate` for the
|
|
78
|
+
driver's updates by dotted path. No I/O and no dependency. An `_id` beside the id field of a
|
|
79
|
+
record is not written (`toDocument` of a body checked with `unknown: 'ignore'` cannot
|
|
80
|
+
choose the `_id`), `fromDocument(schema, null)` is a `type` issue rather than a throw, a
|
|
81
|
+
value of a class at the root is `ERR_INVALID_TARGET`, reading a document and `ids:
|
|
82
|
+
'string'` need no driver, and `binary` is coerced from text of the base64 shape only. The
|
|
83
|
+
walks of a document stop at `maxDepth` (128, an option of `toDocument` and `toUpdate`) as
|
|
84
|
+
the check does, instead of recursing until the stack ends; `checkUpdate` reports a path with
|
|
85
|
+
an empty segment, into a part a field cannot have or to the `_id` of the document whatever
|
|
86
|
+
the policy for unknown keys says, a path through more records than `maxDepth` as a `depth`
|
|
87
|
+
issue, and names what an operator takes in its issues (`array` for a `$push` on a field
|
|
88
|
+
that is no list, `number` for an `$inc` on a string); `register({ bson })` installs the
|
|
89
|
+
converters of the BSON types the module can build only, `decimal128` is coerced from a
|
|
90
|
+
decimal written as one or a finite number (`fromString` of the driver reads `NaN`, `inf`
|
|
91
|
+
and an exponent), and an element of a list of ids (`partners.0`) is an id in `checkUpdate`
|
|
92
|
+
and `toUpdate`. The subpath is tested against the real `bson` (a devDependency) as well as
|
|
93
|
+
the fakes.
|
|
94
|
+
- **`ids: 'objectId'`** for the mongodb target of `toJSONSchema`: the validator of the
|
|
95
|
+
documents `toDocument` writes.
|
|
96
|
+
- `pnpm size` measures a subpath on top of another it requires (`./mongodb` on `./coerce`).
|
|
97
|
+
|
|
98
|
+
### Changed
|
|
99
|
+
|
|
100
|
+
- **The core gate is 19 KB** (was 17.75 KB): coercion costs the core about 360 bytes; the core
|
|
101
|
+
measures 18.6 KB. The formats gate is 5 KB (was 2 KB; the scanners of the review took the
|
|
102
|
+
subpath to 4.0 KB), the compile gate 5 KB (was 4.25) and the model gate 13 KB (was 12): a
|
|
103
|
+
gate is raised to the next whole KB when a fix needs it, and never sits a few bytes over a
|
|
104
|
+
subpath. New gates: 2.5 KB for `./derive`, 2 KB for `./coerce`, 4 KB for `./mongodb` (3.5 before the review: the depth limit and the markers of the paths took the subpath over it).
|
|
105
|
+
- **`coerce` is a reserved key** of a field definition and of the kind metadata. A field
|
|
106
|
+
`coerce` that is not a boolean is `ERR_INVALID_DEFINITION` (was an `unknown-option` warning),
|
|
107
|
+
a kind metadata `coerce` that is not a boolean is `ERR_INVALID_OPTIONS` (was an ordinary
|
|
108
|
+
custom key), and a kind metadata `coerce: true` makes every `parse` of the schema need
|
|
109
|
+
`register()` of `./coerce`.
|
|
110
|
+
- **An option of a call that is not what it must be carries `code: 'ERR_INVALID_OPTION'`**
|
|
111
|
+
(a new `RuntimeErrorCode`): the `TypeError` of `maxErrors`, `references`, `root`, the
|
|
112
|
+
`coerce` of a parse, and of the options of `./stream`, `./derive` and `./mongodb` had no
|
|
113
|
+
code. The options of `./derive` are the call's (`keys` that is no list, options that are no
|
|
114
|
+
object are `ERR_INVALID_OPTION`, a `TypeError`; they were `ERR_INVALID_OPTIONS`, a
|
|
115
|
+
`SchemaDefinitionError`); its `metadata` stays a definition (`ERR_INVALID_OPTIONS`), and
|
|
116
|
+
`keyof` with a `metadata` is `ERR_INVALID_OPTIONS` (a schema of an enum has no kind; it was
|
|
117
|
+
ignored).
|
|
118
|
+
- The rule of the id field of a stored kind (`orderId`, `id` for an anonymous schema) is
|
|
119
|
+
`idFieldOf` of `kinds.js`, read by `./derive`, `./mongodb` and `toJSONSchema`; the alias
|
|
120
|
+
chain of a custom type ends in `baseTypeOf` of `types.js`, read by the generators, `sample`
|
|
121
|
+
and `checkUpdate`; the shapes of the walks of `./mongodb` are named once (`SHAPES`) and held
|
|
122
|
+
to the core's by a test.
|
|
123
|
+
- **`infer` writes the more specific formats**: a column of http(s) addresses is `url` (was
|
|
124
|
+
`uri`), local date-times are `iso-date-time`, ULIDs `ulid`, durations `duration`.
|
|
125
|
+
- **A format of the program under the name of a built-in takes its place** instead of throwing
|
|
126
|
+
`ERR_FORMAT_REGISTERED`, so a format a program registered as `ulid` before 3.3 keeps working.
|
|
127
|
+
- `merge` and `derive` share the code that turns a schema back into a definition
|
|
128
|
+
(`model/data.js`).
|
|
129
|
+
|
|
10
130
|
## [3.2.0] - 2026-10-12
|
|
11
131
|
|
|
12
132
|
3.2 takes a schema to the records around it. The new `@alexify/metaschema/stream` subpath runs
|
|
@@ -1274,7 +1394,8 @@ published.
|
|
|
1274
1394
|
|
|
1275
1395
|
### [metaschema 0.x][upstream-0.x] - First generation of metaschema
|
|
1276
1396
|
|
|
1277
|
-
[unreleased]: https://github.com/Alexis-Technologies/metaschema/compare/v3.
|
|
1397
|
+
[unreleased]: https://github.com/Alexis-Technologies/metaschema/compare/v3.3.0...HEAD
|
|
1398
|
+
[3.3.0]: https://github.com/Alexis-Technologies/metaschema/compare/v3.2.0...v3.3.0
|
|
1278
1399
|
[3.2.0]: https://github.com/Alexis-Technologies/metaschema/compare/v3.1.0...v3.2.0
|
|
1279
1400
|
[3.1.0]: https://github.com/Alexis-Technologies/metaschema/compare/v3.0.0...v3.1.0
|
|
1280
1401
|
[3.0.0]: https://github.com/Alexis-Technologies/metaschema/compare/v2.0.0...v3.0.0
|