@alexify/metaschema 3.0.0 → 3.1.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 +120 -1
- package/README.md +11 -4
- package/index.d.ts +219 -2
- package/package.json +6 -2
- package/src/codec.js +88 -34
- package/src/compile/emit.js +28 -0
- package/src/compile/index.js +2 -2
- package/src/context.js +3 -0
- package/src/errors.js +10 -1
- package/src/generators/fromjsonschema.js +520 -0
- package/src/generators/index.d.ts +19 -2
- package/src/generators/index.js +14 -9
- package/src/lint.js +12 -3
- package/src/model/data.js +122 -0
- package/src/model/diff.js +438 -0
- package/src/model/index.d.ts +56 -0
- package/src/model/index.js +42 -0
- package/src/model/merge.js +254 -0
- package/src/model/pattern.js +322 -0
- package/src/model/restore.js +226 -0
- package/src/model/sample.js +394 -0
- package/src/model.js +8 -0
- package/src/preprocessor.js +4 -1
- package/src/prototypes/schema.js +6 -5
- package/src/schema.js +11 -2
- package/src/snapshot.js +111 -0
- package/src/struct.js +63 -17
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,124 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [3.1.0] - 2026-10-11
|
|
11
|
+
|
|
12
|
+
3.1 makes the model data. `model.toJSON()` writes a snapshot of a model, the contract other
|
|
13
|
+
programs read it through, and the new `@alexify/metaschema/model` subpath builds on it:
|
|
14
|
+
`fromJSON` restores a model from a snapshot, `diff` and `compatibility` compare two versions by
|
|
15
|
+
the rules of schema evolution, `sample` draws a value a schema accepts, and `merge` unifies two
|
|
16
|
+
schemas. A field may carry `aliases`, read by `check` and renamed by `parse`, and
|
|
17
|
+
`fromJSONSchema` of the generators reads a JSON Schema document back into a definition. Nothing
|
|
18
|
+
breaks: the core, the generators and the compile subpath keep their 3.0 behaviour, with the
|
|
19
|
+
fixes listed below.
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
|
|
23
|
+
- **The snapshot.** `model.toJSON()` (what `JSON.stringify(model)` writes) is the model as
|
|
24
|
+
data, format 1: the entities in dependency order with their metadata, fields (each
|
|
25
|
+
`{ type, required, ...sorted keys }`), indexes and relations, and the types the entities use;
|
|
26
|
+
a function is `{ $function: true }` wherever it stands, a bigint and a Date are tagged, a
|
|
27
|
+
RegExp is its source. `tests/fixtures/snapshot.json` pins the format.
|
|
28
|
+
- **`aliases` on a field.** `check` reads a field from an alias the value carries when it has
|
|
29
|
+
nothing under the field's own key, reporting under the key it found the value under; `parse`
|
|
30
|
+
fills no default over it and writes the value under the field's own key, in the place of the
|
|
31
|
+
alias, and only after a check that read an alias (a value without one comes back as it is,
|
|
32
|
+
the same reference); `serialize` never reads an alias. The JIT emits the same lookup. An
|
|
33
|
+
alias is a key of the struct (not a field, a calculated field or another alias:
|
|
34
|
+
`ERR_INVALID_DEFINITION`); the generators and `Infer` name the field's own key only.
|
|
35
|
+
- **`@alexify/metaschema/model`**, a subpath of 9.5 KB min+gzip on top of the core:
|
|
36
|
+
`fromJSON(snapshot, options)` restores a model from its snapshot (placeholders for the
|
|
37
|
+
functions, listed by path in `model.placeholders`; the real custom types through
|
|
38
|
+
`options.types`; an isolated registry unless `options.registry` says otherwise, and a shared
|
|
39
|
+
one refuses a placeholder type). A snapshot is untrusted input: what does not read (a
|
|
40
|
+
bigint, a date, a pattern, an entity without a kind, a nesting deeper than 128 levels, an
|
|
41
|
+
index named like a field, a `custom` key that would override the kind metadata) is
|
|
42
|
+
`ERR_INVALID_SNAPSHOT` with the entity and the field, and a key of the snapshot is defined,
|
|
43
|
+
never assigned, so `__proto__` is a field. `diff(prev, next)` lists the changes between two
|
|
44
|
+
models or snapshots as `{ kind, op, entity, name, from, to, detail }` (a field renamed
|
|
45
|
+
through its aliases is `renamed`, a removed `deprecated` field carries `detail.deprecated`);
|
|
46
|
+
`compatibility(changes)` classifies them as `full`, `backward`, `forward` or `none` by the
|
|
47
|
+
rules of the Confluent Schema Registry (the table is in the Model evolution guide), and
|
|
48
|
+
`compatibilityOf(change)` is the class of one change;
|
|
49
|
+
`sample(schema, { seed, locale, references, maxDepth })` is a value the schema accepts, the
|
|
50
|
+
same for a seed, drawn from the definition and verified field by field (a pattern from the
|
|
51
|
+
subset of alternatives, groups, classes, escapes, control escapes and quantifiers, the
|
|
52
|
+
letters of `.` and `\p{L}` those of the locale of the call; a custom type through
|
|
53
|
+
`metadata.sample`; a union draws another branch when one cannot be satisfied; `maxDepth` 0
|
|
54
|
+
to 32), with `ERR_UNSATISFIABLE`, `ERR_SAMPLE_PATTERN` and `ERR_SAMPLE_TYPE` for what cannot
|
|
55
|
+
be drawn, each naming the seed (`error.seed`): a repetition over 10000, a pattern whose
|
|
56
|
+
minimal length exceeds `length.max` (found before a string is built), groups nested deeper
|
|
57
|
+
than 64; `merge(a, b, { name, metadata })` is the schema both schemas describe,
|
|
58
|
+
by unification, with `ERR_MERGE_CONFLICT` for what no value can satisfy, located like a
|
|
59
|
+
definition error (`error.field`, the message ending `in "Name.address.city"`; an alias that
|
|
60
|
+
is a field of the other schema is a conflict too); `register()` installs `Model.fromJSON`,
|
|
61
|
+
`Model.diff`, `Model.compatibility`, `Model.compatibilityOf`, `Schema.merge` and
|
|
62
|
+
`schema.sample`.
|
|
63
|
+
- **`fromJSONSchema(document, { name, as, references, unsupported })`** of the generators
|
|
64
|
+
subpath: a definition (or a Schema) from the subset of JSON Schema a validation schema is
|
|
65
|
+
made of, the other direction of `toJSONSchema` (types and type lists, properties and
|
|
66
|
+
required, enum and const, items, tuples and uniqueItems, anyOf/oneOf with OpenAPI's
|
|
67
|
+
discriminator, `$ref` into the definitions, the rules, null, the annotations, the root's
|
|
68
|
+
`additionalProperties` as the unknown-keys policy); a keyword outside the subset is
|
|
69
|
+
`ERR_UNSUPPORTED_SCHEMA` with its place as a JSON pointer, and so are a `$ref` that is not
|
|
70
|
+
`#` or `#/…` over own keys, a bad escape in one, a nesting deeper than 128 levels, a
|
|
71
|
+
pattern that does not compile under the `u` flag and an unknown name in a type list;
|
|
72
|
+
`onUnsupported(reason, pointer)` tells what `unsupported: 'ignore'` dropped. A `$ref` is
|
|
73
|
+
converted once per pointer and shared where it is inlined, so a diamond of definitions is
|
|
74
|
+
linear. `register()` adds `Schema.fromJSONSchema`. The subpath is 7.1 KB on top of the core
|
|
75
|
+
now.
|
|
76
|
+
- The types `ModelSnapshot`, `EntitySnapshot`, `FieldSnapshot`, `FieldsSnapshot`,
|
|
77
|
+
`FunctionSnapshot`, `TypeSnapshot`, `RestoreOptions`, `RestoredModel`, `SchemaChange`,
|
|
78
|
+
`ChangeKind`, `ChangeOp`, `Compatibility`, `SampleOptions`, `SampleHook`, `TypeMetadata`
|
|
79
|
+
(the `sample` hook of a custom type), `MergeOptions` and `FromJSONSchemaOptions`;
|
|
80
|
+
`aliases` on `FieldType`; the codes `ERR_INVALID_SNAPSHOT`, `ERR_UNSATISFIABLE`,
|
|
81
|
+
`ERR_SAMPLE_PATTERN`, `ERR_SAMPLE_TYPE`, `ERR_MERGE_CONFLICT` and `ERR_UNSUPPORTED_SCHEMA`.
|
|
82
|
+
- A `dead-aliases` warning for `aliases` anywhere but on a field of a struct (an element, a
|
|
83
|
+
branch, a schema of one type), where nothing reads them.
|
|
84
|
+
|
|
85
|
+
### Changed
|
|
86
|
+
|
|
87
|
+
- Two fields of a struct with one key (`{ a: 'string', 'a?': 'number' }`, or a calculated
|
|
88
|
+
field and a field of one name) are `ERR_INVALID_DEFINITION` ("is a field or an alias
|
|
89
|
+
already"); the second silently replaced the first before.
|
|
90
|
+
- `aliases` is a key the struct reads: `{ type: 'string', aliases: 'old' }` is
|
|
91
|
+
`ERR_INVALID_DEFINITION` where it was an `unknown-option` warning, and a custom type does not
|
|
92
|
+
see an option of that name.
|
|
93
|
+
- `JSON.stringify(model)` writes the snapshot; it wrote the `Model` instance's own enumerable
|
|
94
|
+
properties before.
|
|
95
|
+
- A function of a subpath given something other than what it takes (`toJSONSchema`,
|
|
96
|
+
`toTypeScript`, `compile`, `sample`, `merge`, `compatibility`, `fromJSONSchema`) throws a
|
|
97
|
+
`TypeError` with `code: 'ERR_INVALID_TARGET'` (the `RuntimeErrorCode` of `ERR_ASYNC_VALIDATOR`);
|
|
98
|
+
the message is the same, and `fromJSONSchema` of a non-object document, which was
|
|
99
|
+
`ERR_INVALID_OPTIONS`, is that error too.
|
|
100
|
+
|
|
101
|
+
### Fixed
|
|
102
|
+
|
|
103
|
+
- A `many` reference re-parsed from its built field (a projection copies its parent's fields
|
|
104
|
+
that way, and the long form `{ type: 'Address', many: 'Address' }` reads the same) came back
|
|
105
|
+
with both ends and validated as a single reference.
|
|
106
|
+
- The name of a named tuple element (`[{ x: 'number' }]`) was an `unknown-option` warning.
|
|
107
|
+
- A `validate` given on a `schema` field (`{ type: 'schema', schema, validate }`) was replaced
|
|
108
|
+
by the one of the definition it holds; the field's own wins now, as its `parse` and
|
|
109
|
+
`serialize` did.
|
|
110
|
+
|
|
111
|
+
### Tooling
|
|
112
|
+
|
|
113
|
+
- `pnpm size` has a fourth row and gate, `--max-gzip-model`; the CI gates are 17.5 KB per
|
|
114
|
+
core entry, 7.5 KB for `./generators`, 4.25 KB for `./compile` and 10 KB for `./model` (the
|
|
115
|
+
core, the compile and the model gates moved from 17 KB, 4 KB and 8.5 KB during the review
|
|
116
|
+
of 3.1: the first two had 14 and 15 bytes of headroom left, the third did not hold the
|
|
117
|
+
hardening of the subpath).
|
|
118
|
+
- The bench has a sparse-struct scenario and two alias scenarios of `parse`, and
|
|
119
|
+
`bench/baseline.json` is re-saved after the review.
|
|
120
|
+
- The coverage job runs under `shell: bash`, so the c8 thresholds fail CI through the pipe
|
|
121
|
+
into `tee`; before, the job took the status of `tee`.
|
|
122
|
+
- New suites: `snapshot`, `aliases`, `evolution`, `sample`, `fromjsonschema` and `merge`, on
|
|
123
|
+
all three backends; the bundle test bundles the model subpath with the core.
|
|
124
|
+
- New guides: Model evolution (`diff`, `compatibility`, `aliases`, `deprecated`, the rules
|
|
125
|
+
table), Sample values; the snapshot and `fromJSON` in Domain Models, the import in JSON
|
|
126
|
+
Schema, merging in Schema Syntax.
|
|
127
|
+
|
|
10
128
|
## [3.0.0] - 2026-10-11
|
|
11
129
|
|
|
12
130
|
3.0 moves the generators of JSON Schema and TypeScript from the core entry to the
|
|
@@ -965,7 +1083,8 @@ published.
|
|
|
965
1083
|
|
|
966
1084
|
### [metaschema 0.x][upstream-0.x] - First generation of metaschema
|
|
967
1085
|
|
|
968
|
-
[unreleased]: https://github.com/Alexis-Technologies/metaschema/compare/v3.
|
|
1086
|
+
[unreleased]: https://github.com/Alexis-Technologies/metaschema/compare/v3.1.0...HEAD
|
|
1087
|
+
[3.1.0]: https://github.com/Alexis-Technologies/metaschema/compare/v3.0.0...v3.1.0
|
|
969
1088
|
[3.0.0]: https://github.com/Alexis-Technologies/metaschema/compare/v2.0.0...v3.0.0
|
|
970
1089
|
[2.0.0]: https://github.com/Alexis-Technologies/metaschema/compare/v1.0.0...v2.0.0
|
|
971
1090
|
[1.0.0]: https://github.com/Alexis-Technologies/metaschema/releases/tag/v1.0.0
|
package/README.md
CHANGED
|
@@ -79,9 +79,15 @@ event.parse({ name: 'deploy', when: '2026-10-10T12:00:00.000Z' }).value;
|
|
|
79
79
|
million through the JIT backend of `@alexify/metaschema/compile`, which generates the same
|
|
80
80
|
check where `new Function` is allowed and keeps the closures where it is not (`pnpm bench`,
|
|
81
81
|
see [Performance](https://metaschema.vercel.app/guide/performance)).
|
|
82
|
-
- **
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
- **The model as data.** `model.toJSON()` is a snapshot other programs read the model through
|
|
83
|
+
(a migration tool, an introspecting service, a committed baseline), and
|
|
84
|
+
`@alexify/metaschema/model` tells two versions apart (`diff`), classifies the change by the
|
|
85
|
+
Confluent rules (`compatibility`), draws a value every schema accepts (`sample`) and unifies
|
|
86
|
+
two schemas (`merge`); `aliases` and `deprecated` let a model carry its history, and
|
|
87
|
+
`fromJSONSchema` of the generators reads a JSON Schema document back.
|
|
88
|
+
- **Zero dependencies, 17.2 KB min+gzip.** CommonJS with ESM named imports, no build step,
|
|
89
|
+
one package for Node.js and browsers; the generators add 7.1 KB, the JIT backend 4.0 KB and
|
|
90
|
+
the model tools 9.5 KB where they are imported.
|
|
85
91
|
|
|
86
92
|
## Installation
|
|
87
93
|
|
|
@@ -298,8 +304,9 @@ const {
|
|
|
298
304
|
getKindMetadata, Schema, Model, SchemaDefinitionError, ValidationResult,
|
|
299
305
|
} = require('@alexify/metaschema');
|
|
300
306
|
|
|
301
|
-
const { toJSONSchema, toTypeScript, saveTypes, register } = require('@alexify/metaschema/generators');
|
|
307
|
+
const { toJSONSchema, toTypeScript, saveTypes, fromJSONSchema, register } = require('@alexify/metaschema/generators');
|
|
302
308
|
const { compile, canEval } = require('@alexify/metaschema/compile'); // and its own register()
|
|
309
|
+
const { fromJSON, diff, compatibility, sample, merge } = require('@alexify/metaschema/model'); // and its own register()
|
|
303
310
|
|
|
304
311
|
const en = require('@alexify/metaschema/locales/en'); // the built-in messages
|
|
305
312
|
const uk = require('@alexify/metaschema/locales/uk'); // schema.check(value, { messages: uk })
|
package/index.d.ts
CHANGED
|
@@ -245,6 +245,11 @@ export interface FieldType {
|
|
|
245
245
|
serialize?: (value: any) => unknown;
|
|
246
246
|
// What parse fills a missing key with: a value, or a function called for one.
|
|
247
247
|
default?: unknown;
|
|
248
|
+
// The keys the field is also read from, in a struct: check accepts a value
|
|
249
|
+
// under an alias in place of the field's key, parse renames it. The keys
|
|
250
|
+
// of the value the schema accepts (`Infer`) and the generated documents
|
|
251
|
+
// name the field's own key only.
|
|
252
|
+
aliases?: string[];
|
|
248
253
|
// Records the problems of a value into the context of the current check.
|
|
249
254
|
check(value: unknown, context: CheckContext): void;
|
|
250
255
|
toJSON(): object;
|
|
@@ -262,11 +267,23 @@ export interface TypeConstructor {
|
|
|
262
267
|
|
|
263
268
|
export type TypeTable = Record<string, TypeConstructor>;
|
|
264
269
|
|
|
270
|
+
// What `sample` of the `@alexify/metaschema/model` subpath draws for a
|
|
271
|
+
// custom type: the value `metadata.sample` holds, or what the function it
|
|
272
|
+
// holds returns for the field and the seeded generator of the draw.
|
|
273
|
+
export type SampleHook = (field: FieldType, random: () => number) => unknown;
|
|
274
|
+
|
|
275
|
+
// The metadata of a type: anything, read by the programs built on the
|
|
276
|
+
// model; `sample` is the one key the package reads (see `SampleHook`).
|
|
277
|
+
export interface TypeMetadata {
|
|
278
|
+
sample?: SampleHook | {} | null;
|
|
279
|
+
[key: string]: unknown;
|
|
280
|
+
}
|
|
281
|
+
|
|
265
282
|
// An entry of the table passed to Model: metadata for a built-in, an alias
|
|
266
283
|
// (`js`), or a prototype with its own `construct` and `checkType`.
|
|
267
284
|
export interface TypeEntry {
|
|
268
285
|
js?: string;
|
|
269
|
-
metadata?:
|
|
286
|
+
metadata?: TypeMetadata;
|
|
270
287
|
kind?: 'scalar' | 'struct';
|
|
271
288
|
// The rules the type accepts in a field definition (`length`, `min`, `max`).
|
|
272
289
|
rules?: string[];
|
|
@@ -316,6 +333,19 @@ export type DefinitionErrorCode =
|
|
|
316
333
|
| 'ERR_UNKNOWN_JS_TYPE'
|
|
317
334
|
| 'ERR_INVALID_OPTIONS'
|
|
318
335
|
| 'ERR_UNREPRESENTABLE'
|
|
336
|
+
// Thrown by `fromJSONSchema` of the generators subpath for a keyword the
|
|
337
|
+
// subset has no form for.
|
|
338
|
+
| 'ERR_UNSUPPORTED_SCHEMA'
|
|
339
|
+
// Thrown by `fromJSON` and `diff` of the `@alexify/metaschema/model`
|
|
340
|
+
// subpath for a value that is not a snapshot of a model.
|
|
341
|
+
| 'ERR_INVALID_SNAPSHOT'
|
|
342
|
+
// Thrown by `sample` of the same subpath: a field no value satisfies, a
|
|
343
|
+
// pattern it cannot read, a custom type without `metadata.sample`.
|
|
344
|
+
| 'ERR_UNSATISFIABLE'
|
|
345
|
+
| 'ERR_SAMPLE_PATTERN'
|
|
346
|
+
| 'ERR_SAMPLE_TYPE'
|
|
347
|
+
// Thrown by `merge` of the same subpath for what no value can satisfy.
|
|
348
|
+
| 'ERR_MERGE_CONFLICT'
|
|
319
349
|
// Thrown by `compile` and `register` of the `@alexify/metaschema/compile`
|
|
320
350
|
// subpath with `jit: true` where code generation from strings is not allowed.
|
|
321
351
|
| 'ERR_NO_EVAL'
|
|
@@ -335,7 +365,147 @@ export class SchemaDefinitionError extends TypeError {
|
|
|
335
365
|
// The code of the TypeError a synchronous check or parse throws when a
|
|
336
366
|
// validator, a custom checkType or a decoder returns a promise: the call,
|
|
337
367
|
// not the definition, is wrong, so it is a plain TypeError with `code`.
|
|
338
|
-
|
|
368
|
+
// `ERR_INVALID_TARGET` is the same kind of error from a function of a subpath
|
|
369
|
+
// given something other than what it takes (a Schema, a Model, a snapshot, a
|
|
370
|
+
// list of changes).
|
|
371
|
+
export type RuntimeErrorCode = 'ERR_ASYNC_VALIDATOR' | 'ERR_INVALID_TARGET';
|
|
372
|
+
|
|
373
|
+
// ---------------------------------------------------------------------------
|
|
374
|
+
// The snapshot: `model.toJSON()`, what JSON.stringify(model) writes. The
|
|
375
|
+
// entities in dependency order with a fixed key order, the types the
|
|
376
|
+
// entities use, and the `database` metadata as given. A function is
|
|
377
|
+
// `{ $function: true }` wherever it stands, a bigint `{ $bigint }`, a Date
|
|
378
|
+
// `{ $date }`, a RegExp its source (or `{ source, flags }` with flags beyond
|
|
379
|
+
// `u`). `fromJSON` of the `@alexify/metaschema/model` subpath reads it back.
|
|
380
|
+
// ---------------------------------------------------------------------------
|
|
381
|
+
|
|
382
|
+
export interface FunctionSnapshot {
|
|
383
|
+
$function: true;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// A field of a snapshot: its type and whether it is required first, then
|
|
387
|
+
// the other keys of its definition, sorted (a nested struct under `schema`,
|
|
388
|
+
// an element under `value`, branches under `union`).
|
|
389
|
+
export interface FieldSnapshot {
|
|
390
|
+
type: string;
|
|
391
|
+
required: boolean;
|
|
392
|
+
[key: string]: unknown;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// The fields of a struct in their order: a calculated field, the validate,
|
|
396
|
+
// parse or serialize of the struct are `{ $function: true }`.
|
|
397
|
+
export type FieldsSnapshot = Record<string, FieldSnapshot | FunctionSnapshot>;
|
|
398
|
+
|
|
399
|
+
export interface EntitySnapshot {
|
|
400
|
+
kind: Kind;
|
|
401
|
+
scope: Scope;
|
|
402
|
+
store: Store;
|
|
403
|
+
allow: Allow;
|
|
404
|
+
// The parent of a projection, '' otherwise.
|
|
405
|
+
parent: string;
|
|
406
|
+
unknown: 'reject' | 'ignore';
|
|
407
|
+
// The other keys of the kind metadata, as given.
|
|
408
|
+
custom: Record<string, unknown>;
|
|
409
|
+
// A struct's fields, with the schema-level functions among them, or
|
|
410
|
+
fields?: FieldsSnapshot;
|
|
411
|
+
// the type of a schema of one type.
|
|
412
|
+
type?: FieldSnapshot;
|
|
413
|
+
// The index definitions (`index`, `primary`, `unique` lists), as given.
|
|
414
|
+
indexes: Record<string, object>;
|
|
415
|
+
relations: Relation[];
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// A type the entities use: an alias of a built-in (`js`), a type with a
|
|
419
|
+
// check of its own (`check: true`, its kind), or a built-in with metadata.
|
|
420
|
+
export interface TypeSnapshot {
|
|
421
|
+
js?: string;
|
|
422
|
+
kind?: 'scalar' | 'struct';
|
|
423
|
+
check?: true;
|
|
424
|
+
metadata: Record<string, unknown>;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export interface ModelSnapshot {
|
|
428
|
+
format: 1;
|
|
429
|
+
database: Record<string, unknown> | null;
|
|
430
|
+
types: Record<string, TypeSnapshot>;
|
|
431
|
+
entities: Record<string, EntitySnapshot>;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// The options of `fromJSON` of the `@alexify/metaschema/model` subpath.
|
|
435
|
+
export interface RestoreOptions {
|
|
436
|
+
// Entries of the table Model takes, over the types of the snapshot: the
|
|
437
|
+
// real custom types, where the restored model should validate as the
|
|
438
|
+
// original did.
|
|
439
|
+
types?: Record<string, TypeEntry>;
|
|
440
|
+
// 'isolated' (the default) gives the restored model its own registry, so
|
|
441
|
+
// the placeholders of its custom types never reach the shared one; a
|
|
442
|
+
// 'shared' registry takes no placeholder type (`ERR_INVALID_SNAPSHOT`
|
|
443
|
+
// unless `types` brings the real one).
|
|
444
|
+
registry?: 'shared' | 'isolated';
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// A model restored by `fromJSON`: a Model that lists the paths of the
|
|
448
|
+
// placeholders standing in for the functions of the snapshot (`Order.validate`,
|
|
449
|
+
// `Order.total` for a calculated field, `Order.address.parse`, `Order.when.default`,
|
|
450
|
+
// `types.hex.checkType`), invisibly to `toJSON`.
|
|
451
|
+
export interface RestoredModel extends Model {
|
|
452
|
+
readonly placeholders: string[];
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// ---------------------------------------------------------------------------
|
|
456
|
+
// The evolution of a model: `diff` and `compatibility` of the
|
|
457
|
+
// `@alexify/metaschema/model` subpath.
|
|
458
|
+
// ---------------------------------------------------------------------------
|
|
459
|
+
|
|
460
|
+
export type ChangeKind = 'entity' | 'field' | 'index' | 'relation' | 'type';
|
|
461
|
+
|
|
462
|
+
export type ChangeOp = 'added' | 'removed' | 'changed' | 'renamed';
|
|
463
|
+
|
|
464
|
+
// One change between two versions of a model: what changed (`kind`), how
|
|
465
|
+
// (`op`), in which entity ('' for a type), under which name (the entity, the
|
|
466
|
+
// field, the index, the target of a relation, the type), what it was
|
|
467
|
+
// (`from`) and is (`to`), and the detail: the keys that differ for a
|
|
468
|
+
// `changed` entity, field, index or type; the old key (`renamed`) and the
|
|
469
|
+
// keys that differ beyond `aliases` for a field renamed through an alias;
|
|
470
|
+
// `deprecated: true` for a removed field that was marked so.
|
|
471
|
+
export interface SchemaChange {
|
|
472
|
+
kind: ChangeKind;
|
|
473
|
+
op: ChangeOp;
|
|
474
|
+
entity: string;
|
|
475
|
+
name: string;
|
|
476
|
+
from?: unknown;
|
|
477
|
+
to?: unknown;
|
|
478
|
+
detail?: { keys?: string[]; renamed?: string; deprecated?: true };
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// 'backward': a reader with the new model reads data written with the old
|
|
482
|
+
// one; 'forward': a reader with the old model reads data written with the
|
|
483
|
+
// new one; 'full': both; 'none': neither.
|
|
484
|
+
export type Compatibility = 'full' | 'backward' | 'forward' | 'none';
|
|
485
|
+
|
|
486
|
+
// The options of `merge` of the `@alexify/metaschema/model` subpath.
|
|
487
|
+
export interface MergeOptions {
|
|
488
|
+
// The name of the merged schema: the first schema's by default.
|
|
489
|
+
name?: string;
|
|
490
|
+
// The kind metadata of the merged schema, over the first schema's: `kind`,
|
|
491
|
+
// `scope`, `store`, `allow`, `unknown` and any other key.
|
|
492
|
+
metadata?: Partial<KindMetadata>;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// The options of `sample` of the `@alexify/metaschema/model` subpath.
|
|
496
|
+
export interface SampleOptions {
|
|
497
|
+
// The seed of the generator (1): the same seed gives the same value.
|
|
498
|
+
seed?: number;
|
|
499
|
+
// The letters of free strings: 'en' (the default) or 'uk'.
|
|
500
|
+
locale?: 'en' | 'uk';
|
|
501
|
+
// How a reference is held, as in check: an id for a stored kind and the
|
|
502
|
+
// record for a memory kind ('kind', the default), always the record
|
|
503
|
+
// ('embed'), always an id ('id').
|
|
504
|
+
references?: 'kind' | 'embed' | 'id';
|
|
505
|
+
// How deep embedded records nest (3): an optional reference deeper than
|
|
506
|
+
// that is left out, a required one is an error.
|
|
507
|
+
maxDepth?: number;
|
|
508
|
+
}
|
|
339
509
|
|
|
340
510
|
export const KIND: Array<string>;
|
|
341
511
|
export const KIND_STORED: Array<string>;
|
|
@@ -406,6 +576,27 @@ export interface ModelJSONSchemaOptions extends JSONSchemaOptions {
|
|
|
406
576
|
root?: string;
|
|
407
577
|
}
|
|
408
578
|
|
|
579
|
+
// The options of `fromJSONSchema` of the generators subpath.
|
|
580
|
+
export interface FromJSONSchemaOptions {
|
|
581
|
+
// The name of the schema: what `$ref: '#'` refers to, and the name of the
|
|
582
|
+
// Schema built with `as: 'schema'`.
|
|
583
|
+
name?: string;
|
|
584
|
+
// What comes back: the definition (the default), or a Schema built from it.
|
|
585
|
+
as?: 'definition' | 'schema';
|
|
586
|
+
// A `$ref` into the definitions of the document: the definition in place
|
|
587
|
+
// ('inline', the default; a cycle has no form), or a reference to an
|
|
588
|
+
// entity of that name ('name'), to resolve through a Model.
|
|
589
|
+
references?: 'inline' | 'name';
|
|
590
|
+
// A keyword the subset has no form for: `ERR_UNSUPPORTED_SCHEMA` with its
|
|
591
|
+
// place in the document ('throw', the default), or dropped ('ignore').
|
|
592
|
+
unsupported?: 'throw' | 'ignore';
|
|
593
|
+
// Told of every drop of `unsupported: 'ignore'`: what was dropped, as the
|
|
594
|
+
// error of the throw mode names it (`The keyword "multipleOf"`), and its
|
|
595
|
+
// place in the document as a JSON pointer. Not called in the throw mode,
|
|
596
|
+
// where the error carries both.
|
|
597
|
+
onUnsupported?: (reason: string, pointer: string) => void;
|
|
598
|
+
}
|
|
599
|
+
|
|
409
600
|
// Standard JSON Schema (https://standardschema.dev/json-schema): the converter
|
|
410
601
|
// `schema['~standard'].jsonSchema`, whose `input` and `output` take the
|
|
411
602
|
// target and the other options of `toJSONSchema` as `libraryOptions`. The
|
|
@@ -912,6 +1103,18 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
912
1103
|
|
|
913
1104
|
static from<const D extends string | object>(raw: D, namespaces?: Array<Model>): Schema<D>;
|
|
914
1105
|
static extractSchema(def: object): Schema | null;
|
|
1106
|
+
// A definition (or a Schema) from a JSON Schema document: exists once
|
|
1107
|
+
// `register()` of the generators subpath has run; its `fromJSONSchema` is
|
|
1108
|
+
// the same without it.
|
|
1109
|
+
static fromJSONSchema(
|
|
1110
|
+
document: JSONSchema | true,
|
|
1111
|
+
options: FromJSONSchemaOptions & { as: 'schema' },
|
|
1112
|
+
): Schema;
|
|
1113
|
+
static fromJSONSchema(document: JSONSchema | true, options?: FromJSONSchemaOptions): object | string;
|
|
1114
|
+
// The schema two schemas describe, by unification: exists once `register()`
|
|
1115
|
+
// of the `@alexify/metaschema/model` subpath has run; its `merge(a, b)` is
|
|
1116
|
+
// the same without it.
|
|
1117
|
+
static merge(a: Schema, b: Schema, options?: MergeOptions): Schema;
|
|
915
1118
|
|
|
916
1119
|
kind: Kind;
|
|
917
1120
|
scope: Scope;
|
|
@@ -958,6 +1161,10 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
958
1161
|
options: SerializeOptions & { check: true },
|
|
959
1162
|
): SerializeChecked<Infer<D>>;
|
|
960
1163
|
serialize(value: InferOutput<D>, options?: SerializeOptions): Serialized<Infer<D>>;
|
|
1164
|
+
// A value the schema accepts, the same for a seed: exists once `register()`
|
|
1165
|
+
// of the `@alexify/metaschema/model` subpath has run; its `sample(schema,
|
|
1166
|
+
// options)` is the same without it.
|
|
1167
|
+
sample(options?: SampleOptions): Infer<D>;
|
|
961
1168
|
// The two methods below exist once `register()` of the generators subpath
|
|
962
1169
|
// has run; the subpath's `toJSONSchema(schema)` and `toTypeScript(schema)`
|
|
963
1170
|
// are the same without it.
|
|
@@ -988,6 +1195,16 @@ export class Model {
|
|
|
988
1195
|
database?: Record<string, unknown> | null,
|
|
989
1196
|
options?: ModelOptions,
|
|
990
1197
|
);
|
|
1198
|
+
// The model as plain JSON, what JSON.stringify(model) writes; see
|
|
1199
|
+
// `ModelSnapshot`.
|
|
1200
|
+
toJSON(): ModelSnapshot;
|
|
1201
|
+
// The four statics below exist once `register()` of the
|
|
1202
|
+
// `@alexify/metaschema/model` subpath has run; its `fromJSON`, `diff`,
|
|
1203
|
+
// `compatibility` and `compatibilityOf` are the same without it.
|
|
1204
|
+
static fromJSON(snapshot: ModelSnapshot, options?: RestoreOptions): RestoredModel;
|
|
1205
|
+
static diff(prev: Model | ModelSnapshot, next: Model | ModelSnapshot): SchemaChange[];
|
|
1206
|
+
static compatibility(changes: SchemaChange[]): Compatibility;
|
|
1207
|
+
static compatibilityOf(change: SchemaChange): Compatibility;
|
|
991
1208
|
// The members below exist once `register()` of the generators subpath has
|
|
992
1209
|
// run; the subpath's `toJSONSchema(model)` and `toTypeScript(model)` are the
|
|
993
1210
|
// same without it.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexify/metaschema",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Zero-dependency metadata schema and interface definition language (IDL) for Node.js and browsers — data validation, domain models and TypeScript generation",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alex Dolid <dolid.sasha@gmail.com>",
|
|
@@ -42,6 +42,10 @@
|
|
|
42
42
|
"./compile": {
|
|
43
43
|
"types": "./src/compile/index.d.ts",
|
|
44
44
|
"default": "./src/compile/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./model": {
|
|
47
|
+
"types": "./src/model/index.d.ts",
|
|
48
|
+
"default": "./src/model/index.js"
|
|
45
49
|
}
|
|
46
50
|
},
|
|
47
51
|
"directories": {
|
|
@@ -103,7 +107,7 @@
|
|
|
103
107
|
"test:nojit": "node --disallow-code-generation-from-strings --require ./tests/helpers/jit.js --test tests/unit/*.test.js",
|
|
104
108
|
"test:coverage": "c8 --all --include 'src/**' --check-coverage --lines 98 --statements 98 --branches 90 --functions 100 --reporter text --reporter lcov node --test tests/unit/*.test.js",
|
|
105
109
|
"test:types": "tsd",
|
|
106
|
-
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts src/locales/en.d.ts src/locales/uk.d.ts src/generators/index.d.ts src/compile/index.d.ts",
|
|
110
|
+
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts src/locales/en.d.ts src/locales/uk.d.ts src/generators/index.d.ts src/compile/index.d.ts src/model/index.d.ts",
|
|
107
111
|
"lint": "oxlint index.js browser.js src tests scripts bench",
|
|
108
112
|
"format": "oxfmt index.js browser.js src tests scripts bench",
|
|
109
113
|
"format:check": "oxfmt --check index.js browser.js src tests scripts bench",
|