@alexify/metaschema 2.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 +443 -1
- package/README.md +124 -12
- package/SECURITY.md +27 -8
- package/index.d.ts +537 -84
- package/package.json +25 -6
- package/src/codec.js +544 -0
- package/src/compile/code.js +92 -0
- package/src/compile/emit.js +540 -0
- package/src/compile/index.d.ts +38 -0
- package/src/compile/index.js +176 -0
- package/src/context.js +68 -19
- package/src/errors.js +19 -1
- package/src/generators/fromjsonschema.js +520 -0
- package/src/generators/index.d.ts +46 -0
- package/src/generators/index.js +95 -0
- package/src/generators/jsonschema.js +535 -0
- package/src/generators/typescript.js +237 -0
- package/src/index.js +0 -2
- package/src/issues.js +117 -22
- package/src/kinds.js +9 -12
- package/src/lint.js +31 -14
- package/src/locales/en.js +10 -2
- package/src/locales/uk.js +7 -2
- package/src/metadata.js +41 -44
- package/src/metautil.js +8 -1
- 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 +18 -15
- package/src/preprocessor.js +9 -11
- package/src/prototypes/abstract.js +14 -1
- package/src/prototypes/collections.js +20 -3
- package/src/prototypes/reference.js +17 -3
- package/src/prototypes/scalars.js +1 -3
- package/src/prototypes/schema.js +15 -2
- package/src/prototypes/tuple.js +14 -9
- package/src/prototypes/union.js +57 -18
- package/src/result.js +5 -14
- package/src/rules.js +55 -26
- package/src/runtime/browser.js +2 -2
- package/src/runtime/node.js +4 -2
- package/src/schema.js +129 -101
- package/src/snapshot.js +111 -0
- package/src/struct.js +80 -23
- package/src/types.js +5 -11
- package/src/util.js +39 -0
package/index.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ export type IssueCode =
|
|
|
40
40
|
| 'union'
|
|
41
41
|
| 'reference'
|
|
42
42
|
| 'circular'
|
|
43
|
+
| 'depth'
|
|
43
44
|
| 'exception'
|
|
44
45
|
| 'custom';
|
|
45
46
|
|
|
@@ -60,6 +61,7 @@ export interface IssueParams {
|
|
|
60
61
|
union: { expected: unknown[]; discriminator: string | undefined };
|
|
61
62
|
reference: { entity: string };
|
|
62
63
|
circular: {};
|
|
64
|
+
depth: { max: number };
|
|
63
65
|
exception: { error: unknown };
|
|
64
66
|
// A validator's own issue carries the params it gave, if any.
|
|
65
67
|
custom: Record<string, unknown>;
|
|
@@ -118,6 +120,9 @@ export interface CheckOptions {
|
|
|
118
120
|
root?: string;
|
|
119
121
|
// Stop collecting after this many issues (at least 1).
|
|
120
122
|
maxErrors?: number;
|
|
123
|
+
// How deep a value may nest through embedded references (an integer of at
|
|
124
|
+
// least 1, 128 by default): a record deeper than that is a `depth` issue.
|
|
125
|
+
maxDepth?: number;
|
|
121
126
|
// What to do with keys the schema does not have: report them as one
|
|
122
127
|
// `unexpected` issue per struct, or ignore them. The default is the
|
|
123
128
|
// schema's own `unknown` metadata, 'reject' unless it says otherwise.
|
|
@@ -130,6 +135,33 @@ export interface CheckOptions {
|
|
|
130
135
|
messages?: Messages;
|
|
131
136
|
}
|
|
132
137
|
|
|
138
|
+
// The options of parse: those of check, and `strip` for the keys the schema
|
|
139
|
+
// does not have, which the output then leaves out.
|
|
140
|
+
export interface ParseOptions extends Omit<CheckOptions, 'unknown'> {
|
|
141
|
+
unknown?: 'reject' | 'ignore' | 'strip';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// What parse returns: the output only for a valid value, the issues always.
|
|
145
|
+
export type ParseResult<T> =
|
|
146
|
+
| { valid: true; value: T; issues: [] }
|
|
147
|
+
| { valid: false; value: undefined; issues: ValidationIssue[] };
|
|
148
|
+
|
|
149
|
+
// The options of serialize: `check` validates the result with the other
|
|
150
|
+
// options as the options of check.
|
|
151
|
+
export interface SerializeOptions extends CheckOptions {
|
|
152
|
+
check?: boolean;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export interface Serialized<T> {
|
|
156
|
+
value: T;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface SerializeChecked<T> {
|
|
160
|
+
valid: boolean;
|
|
161
|
+
value: T;
|
|
162
|
+
issues: ValidationIssue[];
|
|
163
|
+
}
|
|
164
|
+
|
|
133
165
|
export interface ResultOptions {
|
|
134
166
|
// The label the error lines start with (the schema name in a check).
|
|
135
167
|
root?: string;
|
|
@@ -169,15 +201,30 @@ export interface CheckContext {
|
|
|
169
201
|
issues: ValidationIssue[];
|
|
170
202
|
count: number;
|
|
171
203
|
limit: number;
|
|
204
|
+
// How many embedded records the check is inside, and how many it may be
|
|
205
|
+
// before a record is a `depth` issue (128 by default).
|
|
206
|
+
depth: number;
|
|
207
|
+
maxDepth: number;
|
|
172
208
|
path: PropertyKey[];
|
|
173
209
|
seen: Set<object> | null;
|
|
174
|
-
|
|
210
|
+
// The value the check started from, when the schema can reach it again
|
|
211
|
+
// through a reference; null otherwise.
|
|
212
|
+
source: unknown;
|
|
213
|
+
// 'strip' in a parse, where the check behind it treats it as 'ignore'.
|
|
214
|
+
unknown: 'reject' | 'ignore' | 'strip';
|
|
175
215
|
references: 'kind' | 'embed' | 'id';
|
|
176
216
|
root: string;
|
|
177
217
|
messages: Messages;
|
|
218
|
+
// The validators that returned a promise, settled after the walk by
|
|
219
|
+
// checkAsync and parseAsync; null in a synchronous check.
|
|
220
|
+
async: unknown[] | null;
|
|
221
|
+
// The context parse tries the branches of a plain union in; null in a check.
|
|
222
|
+
scratch: CheckContext | null;
|
|
178
223
|
}
|
|
179
224
|
|
|
180
|
-
|
|
225
|
+
// A validator may return a promise of its result to checkAsync and parseAsync;
|
|
226
|
+
// to check and parse a promise is a TypeError with the code ERR_ASYNC_VALIDATOR.
|
|
227
|
+
export type Validator = (value: any, path: string) => ValidationReturn | Promise<ValidationReturn>;
|
|
181
228
|
|
|
182
229
|
export type CalculatedField = (value: any) => unknown;
|
|
183
230
|
|
|
@@ -192,6 +239,17 @@ export interface FieldType {
|
|
|
192
239
|
// (false) whatever the kind of its target.
|
|
193
240
|
embed?: boolean;
|
|
194
241
|
validate?: Validator;
|
|
242
|
+
// The decoder parse runs on the validated value, and the serializer
|
|
243
|
+
// serialize runs on the value of the program, when the definition gave them.
|
|
244
|
+
parse?: (value: any) => unknown;
|
|
245
|
+
serialize?: (value: any) => unknown;
|
|
246
|
+
// What parse fills a missing key with: a value, or a function called for one.
|
|
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[];
|
|
195
253
|
// Records the problems of a value into the context of the current check.
|
|
196
254
|
check(value: unknown, context: CheckContext): void;
|
|
197
255
|
toJSON(): object;
|
|
@@ -209,16 +267,28 @@ export interface TypeConstructor {
|
|
|
209
267
|
|
|
210
268
|
export type TypeTable = Record<string, TypeConstructor>;
|
|
211
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
|
+
|
|
212
282
|
// An entry of the table passed to Model: metadata for a built-in, an alias
|
|
213
283
|
// (`js`), or a prototype with its own `construct` and `checkType`.
|
|
214
284
|
export interface TypeEntry {
|
|
215
285
|
js?: string;
|
|
216
|
-
metadata?:
|
|
286
|
+
metadata?: TypeMetadata;
|
|
217
287
|
kind?: 'scalar' | 'struct';
|
|
218
288
|
// The rules the type accepts in a field definition (`length`, `min`, `max`).
|
|
219
289
|
rules?: string[];
|
|
220
290
|
construct?(def: object, preprocessor: object): void;
|
|
221
|
-
checkType?(value: any, path: string): ValidationReturn
|
|
291
|
+
checkType?(value: any, path: string): ValidationReturn | Promise<ValidationReturn>;
|
|
222
292
|
[key: string]: unknown;
|
|
223
293
|
}
|
|
224
294
|
|
|
@@ -261,7 +331,28 @@ export type DefinitionErrorCode =
|
|
|
261
331
|
| 'ERR_RESERVED_KEY'
|
|
262
332
|
| 'ERR_TYPE_REGISTERED'
|
|
263
333
|
| 'ERR_UNKNOWN_JS_TYPE'
|
|
264
|
-
| 'ERR_INVALID_OPTIONS'
|
|
334
|
+
| 'ERR_INVALID_OPTIONS'
|
|
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'
|
|
349
|
+
// Thrown by `compile` and `register` of the `@alexify/metaschema/compile`
|
|
350
|
+
// subpath with `jit: true` where code generation from strings is not allowed.
|
|
351
|
+
| 'ERR_NO_EVAL'
|
|
352
|
+
// The check of a schema could not be generated (the generator's error is the
|
|
353
|
+
// `cause`): thrown with `jit: true`, reported as `error` and to `onFallback`
|
|
354
|
+
// in the default mode.
|
|
355
|
+
| 'ERR_JIT_BUILD';
|
|
265
356
|
|
|
266
357
|
export class SchemaDefinitionError extends TypeError {
|
|
267
358
|
code: DefinitionErrorCode;
|
|
@@ -271,6 +362,151 @@ export class SchemaDefinitionError extends TypeError {
|
|
|
271
362
|
locate(schema: string, field: string): this;
|
|
272
363
|
}
|
|
273
364
|
|
|
365
|
+
// The code of the TypeError a synchronous check or parse throws when a
|
|
366
|
+
// validator, a custom checkType or a decoder returns a promise: the call,
|
|
367
|
+
// not the definition, is wrong, so it is a plain TypeError with `code`.
|
|
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
|
+
}
|
|
509
|
+
|
|
274
510
|
export const KIND: Array<string>;
|
|
275
511
|
export const KIND_STORED: Array<string>;
|
|
276
512
|
export const KIND_MEMORY: Array<string>;
|
|
@@ -283,7 +519,98 @@ export function getKindMetadata(
|
|
|
283
519
|
meta?: Record<string, unknown>,
|
|
284
520
|
root?: Schema,
|
|
285
521
|
): { defs: Record<string, unknown>; metadata: KindMetadata };
|
|
286
|
-
|
|
522
|
+
// ---------------------------------------------------------------------------
|
|
523
|
+
// The generators: JSON Schema and TypeScript. The functions live in the
|
|
524
|
+
// `@alexify/metaschema/generators` subpath (`toJSONSchema`, `toTypeScript`,
|
|
525
|
+
// `saveTypes`); its `register()` installs the methods declared on `Schema`
|
|
526
|
+
// and `Model` below and the Standard JSON Schema converter. The option
|
|
527
|
+
// types are here so that the core and the subpath share them.
|
|
528
|
+
// ---------------------------------------------------------------------------
|
|
529
|
+
|
|
530
|
+
// The options of `toTypeScript`, `toInterface` and `saveTypes`.
|
|
531
|
+
export interface InterfaceOptions {
|
|
532
|
+
// Give a nested struct and an enum a type of their own, named after the
|
|
533
|
+
// entity and the field (`interface AccountFullName`, `type IdentifierStorage`),
|
|
534
|
+
// rendered before the interface that uses them.
|
|
535
|
+
named?: boolean;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// The JSON Schema export renders the rules of `check` as a document of a
|
|
539
|
+
// target dialect; see the JSON Schema guide for the mapping.
|
|
540
|
+
export type JSONSchemaTarget = 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | 'mongodb';
|
|
541
|
+
|
|
542
|
+
export type JSONSchema = Record<string, unknown>;
|
|
543
|
+
|
|
544
|
+
export interface JSONSchemaOptions {
|
|
545
|
+
// The dialect of the document: JSON Schema draft 2020-12 (the default) or
|
|
546
|
+
// draft-07, an OpenAPI 3.0 schema object, or a MongoDB `$jsonSchema`
|
|
547
|
+
// (`bsonType`, no `$ref`, references inline).
|
|
548
|
+
target?: JSONSchemaTarget;
|
|
549
|
+
// 'strict' renders the dialect of LLM structured outputs (OpenAI, Anthropic):
|
|
550
|
+
// an object at the root, every property listed as required (an optional one
|
|
551
|
+
// accepts null), `additionalProperties: false` on every object, and the
|
|
552
|
+
// structure only (no `length`, `pattern`, `min` or `max`). For the
|
|
553
|
+
// draft-2020-12 and draft-07 targets.
|
|
554
|
+
profile?: 'strict';
|
|
555
|
+
// Which side of the wire the document describes: 'input' (the default) is
|
|
556
|
+
// the JSON a value is parsed from (a `date` is a date-time string), 'output'
|
|
557
|
+
// the value itself (a `date`, `set` or `map` has no JSON form).
|
|
558
|
+
io?: 'input' | 'output';
|
|
559
|
+
// What to do with a type that has no form in the target: throw
|
|
560
|
+
// `ERR_UNREPRESENTABLE` (the default), or render it as `{}`.
|
|
561
|
+
unrepresentable?: 'throw' | 'any';
|
|
562
|
+
// How a reference renders, as in `check`: by the kind of its target ('kind',
|
|
563
|
+
// the default: a stored kind as an id, a memory kind as a `$ref` to its
|
|
564
|
+
// definition), every reference as the record ('embed') or as an id ('id').
|
|
565
|
+
references?: 'kind' | 'embed' | 'id';
|
|
566
|
+
// The pointer the `$ref` of a definition starts with, and where the
|
|
567
|
+
// definitions are placed in the document: '#/$defs/' for draft-2020-12,
|
|
568
|
+
// '#/definitions/' for draft-07, '#/components/schemas/' for openapi-3.0.
|
|
569
|
+
definitions?: string;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
export interface ModelJSONSchemaOptions extends JSONSchemaOptions {
|
|
573
|
+
// The entity at the root of the document, with the definitions it needs;
|
|
574
|
+
// without it the document holds every entity as a definition, in
|
|
575
|
+
// dependency order (one document per entity for the mongodb target).
|
|
576
|
+
root?: string;
|
|
577
|
+
}
|
|
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
|
+
|
|
600
|
+
// Standard JSON Schema (https://standardschema.dev/json-schema): the converter
|
|
601
|
+
// `schema['~standard'].jsonSchema`, whose `input` and `output` take the
|
|
602
|
+
// target and the other options of `toJSONSchema` as `libraryOptions`. The
|
|
603
|
+
// mongodb target is not a JSON Schema and is refused here. Installed by
|
|
604
|
+
// `register()` of the generators subpath.
|
|
605
|
+
export interface StandardJSONSchemaOptions {
|
|
606
|
+
readonly target: 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | ({} & string);
|
|
607
|
+
readonly libraryOptions?: Omit<JSONSchemaOptions, 'target' | 'io'> | undefined;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
export interface StandardConverter {
|
|
611
|
+
readonly input: (options: StandardJSONSchemaOptions) => JSONSchema;
|
|
612
|
+
readonly output: (options: StandardJSONSchemaOptions) => JSONSchema;
|
|
613
|
+
}
|
|
287
614
|
|
|
288
615
|
// ---------------------------------------------------------------------------
|
|
289
616
|
// Static inference: the TypeScript type of a value a definition accepts.
|
|
@@ -309,6 +636,11 @@ type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
|
309
636
|
|
|
310
637
|
type IsUnion<T, U = T> = T extends unknown ? ([U] extends [T] ? false : true) : never;
|
|
311
638
|
|
|
639
|
+
// The value a definition describes is read twice: as `check` and `parse`
|
|
640
|
+
// accept it ('input') and as `parse` gives it ('output'), where a decoder
|
|
641
|
+
// replaces the value of its field and a default makes its key present.
|
|
642
|
+
type Mode = 'input' | 'output';
|
|
643
|
+
|
|
312
644
|
type ScalarName =
|
|
313
645
|
| 'string'
|
|
314
646
|
| 'number'
|
|
@@ -406,10 +738,10 @@ type Embedded<Name extends string, Embed, E> = Embed extends boolean
|
|
|
406
738
|
: true
|
|
407
739
|
: false;
|
|
408
740
|
|
|
409
|
-
type Referenced<Name extends string, Embed, E> =
|
|
741
|
+
type Referenced<Name extends string, Embed, E, M extends Mode> =
|
|
410
742
|
Embedded<Name, Embed, E> extends true
|
|
411
743
|
? Name extends keyof E
|
|
412
|
-
?
|
|
744
|
+
? Value<E[Name], E, M>
|
|
413
745
|
: unknown
|
|
414
746
|
: string;
|
|
415
747
|
|
|
@@ -446,9 +778,13 @@ type ArgOf<D, N extends string, Alt extends string> = D extends { [P in N]: infe
|
|
|
446
778
|
type ElementsOf<D, N extends string, Alt extends string> =
|
|
447
779
|
ArgOf<D, N, Alt> extends infer X ? (X extends readonly unknown[] ? X : never) : never;
|
|
448
780
|
|
|
449
|
-
type TupleOf<T extends readonly unknown[], E> = {
|
|
781
|
+
type TupleOf<T extends readonly unknown[], E, M extends Mode> = {
|
|
782
|
+
-readonly [I in keyof T]: Element<T[I], E, M>;
|
|
783
|
+
};
|
|
450
784
|
|
|
451
|
-
type UnionOf<T extends readonly unknown[], E> = {
|
|
785
|
+
type UnionOf<T extends readonly unknown[], E, M extends Mode> = {
|
|
786
|
+
[I in keyof T]: Value<T[I], E, M>;
|
|
787
|
+
}[number];
|
|
452
788
|
|
|
453
789
|
// A one-key object holding a type name names a tuple element (`{ x: 'number'
|
|
454
790
|
// }`); the key must not itself be `type`, a type name or a kind.
|
|
@@ -470,99 +806,107 @@ type NamedKey<T> = keyof T extends infer K
|
|
|
470
806
|
: never
|
|
471
807
|
: never;
|
|
472
808
|
|
|
473
|
-
type Element<T, E> = T extends string
|
|
474
|
-
?
|
|
809
|
+
type Element<T, E, M extends Mode> = T extends string
|
|
810
|
+
? Value<T, E, M>
|
|
475
811
|
: T extends readonly unknown[]
|
|
476
|
-
?
|
|
812
|
+
? Value<T, E, M>
|
|
477
813
|
: T extends object
|
|
478
814
|
? NamedKey<T> extends infer K
|
|
479
815
|
? [K] extends [never]
|
|
480
|
-
?
|
|
816
|
+
? Value<T, E, M>
|
|
481
817
|
: K extends keyof T
|
|
482
818
|
? T[K] extends string
|
|
483
819
|
? K extends `${string}?`
|
|
484
|
-
?
|
|
485
|
-
:
|
|
486
|
-
:
|
|
487
|
-
:
|
|
820
|
+
? Value<T[K], E, M> | null | undefined
|
|
821
|
+
: Value<T[K], E, M>
|
|
822
|
+
: Value<T, E, M>
|
|
823
|
+
: Value<T, E, M>
|
|
488
824
|
: never
|
|
489
|
-
:
|
|
825
|
+
: Value<T, E, M>;
|
|
490
826
|
|
|
491
827
|
// The value type of a type by name, with the definition it is read from.
|
|
492
|
-
type TypeOf<N extends string, D, E> =
|
|
828
|
+
type TypeOf<N extends string, D, E, M extends Mode> =
|
|
493
829
|
IsCapitalized<N> extends true
|
|
494
|
-
? Referenced<N, EmbedOf<D>, E>
|
|
830
|
+
? Referenced<N, EmbedOf<D>, E, M>
|
|
495
831
|
: N extends 'array'
|
|
496
|
-
?
|
|
832
|
+
? Value<ArgOf<D, 'array', 'value'>, E, M>[]
|
|
497
833
|
: N extends 'set'
|
|
498
|
-
? Set<
|
|
834
|
+
? Set<Value<ArgOf<D, 'set', 'value'>, E, M>>
|
|
499
835
|
: N extends 'object'
|
|
500
836
|
? Pair<D, 'object'> extends [infer K, infer V]
|
|
501
|
-
? Record<KeyTypeOf<K> & PropertyKey,
|
|
837
|
+
? Record<KeyTypeOf<K> & PropertyKey, Value<V, E, M>>
|
|
502
838
|
: Record<string, unknown>
|
|
503
839
|
: N extends 'map'
|
|
504
840
|
? Pair<D, 'map'> extends [infer K, infer V]
|
|
505
|
-
? Map<KeyTypeOf<K>,
|
|
841
|
+
? Map<KeyTypeOf<K>, Value<V, E, M>>
|
|
506
842
|
: Map<unknown, unknown>
|
|
507
843
|
: N extends 'enum'
|
|
508
844
|
? D extends { enum: readonly (infer V)[] }
|
|
509
845
|
? V
|
|
510
846
|
: unknown
|
|
511
847
|
: N extends 'tuple'
|
|
512
|
-
? TupleOf<ElementsOf<D, 'tuple', 'value'>, E>
|
|
848
|
+
? TupleOf<ElementsOf<D, 'tuple', 'value'>, E, M>
|
|
513
849
|
: N extends 'union'
|
|
514
|
-
? UnionOf<ElementsOf<D, 'union', 'union'>, E>
|
|
850
|
+
? UnionOf<ElementsOf<D, 'union', 'union'>, E, M>
|
|
515
851
|
: N extends 'schema'
|
|
516
852
|
? D extends { schema: infer S }
|
|
517
|
-
?
|
|
853
|
+
? Value<S, E, M>
|
|
518
854
|
: unknown
|
|
519
855
|
: N extends 'one'
|
|
520
856
|
? D extends { one: infer T extends string }
|
|
521
|
-
? Referenced<T, EmbedOf<D>, E>
|
|
857
|
+
? Referenced<T, EmbedOf<D>, E, M>
|
|
522
858
|
: unknown
|
|
523
859
|
: N extends 'many'
|
|
524
860
|
? D extends { many: infer T extends string }
|
|
525
|
-
? Referenced<T, EmbedOf<D>, E>[]
|
|
861
|
+
? Referenced<T, EmbedOf<D>, E, M>[]
|
|
526
862
|
: unknown
|
|
527
863
|
: N extends keyof CustomTypes
|
|
528
864
|
? CustomTypes[N]
|
|
529
865
|
: N extends ScalarName
|
|
530
866
|
? ScalarOf<N>
|
|
531
867
|
: D extends { schema: infer S extends object }
|
|
532
|
-
?
|
|
868
|
+
? Value<S, E, M>
|
|
533
869
|
: unknown;
|
|
534
870
|
|
|
871
|
+
// On the output side a decoder (`parse`) gives the value of its field; a
|
|
872
|
+
// promise of one is awaited.
|
|
873
|
+
type Decoded<T, D, M extends Mode> = M extends 'output'
|
|
874
|
+
? D extends { parse: (...args: never[]) => infer R }
|
|
875
|
+
? Awaited<R>
|
|
876
|
+
: T
|
|
877
|
+
: T;
|
|
878
|
+
|
|
535
879
|
type Nullable<T, D> = D extends { nullable: true } ? T | null : T;
|
|
536
880
|
|
|
537
881
|
type Optional<T, D> = D extends { required: false } ? T | null | undefined : T;
|
|
538
882
|
|
|
539
883
|
// `{ type: 'name', ... }`: the type, `| null` for `nullable: true`, and `|
|
|
540
884
|
// null | undefined` for `'?name'` or `required: false`.
|
|
541
|
-
type LongForm<T extends string, D, E> = T extends `?${infer N}`
|
|
542
|
-
? Nullable<TypeOf<N, D, E>, D> | null | undefined
|
|
543
|
-
: Optional<Nullable<TypeOf<T, D, E>, D>, D>;
|
|
885
|
+
type LongForm<T extends string, D, E, M extends Mode> = T extends `?${infer N}`
|
|
886
|
+
? Nullable<Decoded<TypeOf<N, D, E, M>, D, M>, D> | null | undefined
|
|
887
|
+
: Optional<Nullable<Decoded<TypeOf<T, D, E, M>, D, M>, D>, D>;
|
|
544
888
|
|
|
545
|
-
type Shorthand<D, E> = D extends { array: unknown }
|
|
546
|
-
? LongForm<'array', D, E>
|
|
889
|
+
type Shorthand<D, E, M extends Mode> = D extends { array: unknown }
|
|
890
|
+
? LongForm<'array', D, E, M>
|
|
547
891
|
: D extends { set: unknown }
|
|
548
|
-
? LongForm<'set', D, E>
|
|
892
|
+
? LongForm<'set', D, E, M>
|
|
549
893
|
: D extends { object: object }
|
|
550
|
-
? LongForm<'object', D, E>
|
|
894
|
+
? LongForm<'object', D, E, M>
|
|
551
895
|
: D extends { map: object }
|
|
552
|
-
? LongForm<'map', D, E>
|
|
896
|
+
? LongForm<'map', D, E, M>
|
|
553
897
|
: D extends { enum: readonly unknown[] }
|
|
554
|
-
? LongForm<'enum', D, E>
|
|
898
|
+
? LongForm<'enum', D, E, M>
|
|
555
899
|
: D extends { tuple: readonly unknown[] }
|
|
556
|
-
? LongForm<'tuple', D, E>
|
|
900
|
+
? LongForm<'tuple', D, E, M>
|
|
557
901
|
: D extends { union: readonly unknown[] }
|
|
558
|
-
? LongForm<'union', D, E>
|
|
902
|
+
? LongForm<'union', D, E, M>
|
|
559
903
|
: D extends { schema: object }
|
|
560
|
-
? LongForm<'schema', D, E>
|
|
904
|
+
? LongForm<'schema', D, E, M>
|
|
561
905
|
: D extends { one: string }
|
|
562
|
-
? LongForm<'one', D, E>
|
|
906
|
+
? LongForm<'one', D, E, M>
|
|
563
907
|
: D extends { many: string }
|
|
564
|
-
? LongForm<'many', D, E>
|
|
565
|
-
: Struct<D, E>;
|
|
908
|
+
? LongForm<'many', D, E, M>
|
|
909
|
+
: Decoded<Struct<D, E, M>, D, M>;
|
|
566
910
|
|
|
567
911
|
// What is not a field of a struct: a function (a calculated field, or the
|
|
568
912
|
// schema-level validate/parse/serialize/format) and an index definition.
|
|
@@ -587,13 +931,44 @@ type IsOptionalDef<X> = X extends `?${string}`
|
|
|
587
931
|
? true
|
|
588
932
|
: false;
|
|
589
933
|
|
|
934
|
+
// Whether a key is optional by its name (`'key?'`) or its definition.
|
|
935
|
+
type IsOptionalKey<K, X> = K extends `${string}?` ? true : IsOptionalDef<X>;
|
|
936
|
+
|
|
937
|
+
// Whether parse fills the key when it is missing.
|
|
938
|
+
type Defaulted<X> = X extends { default: infer V } ? ([V] extends [undefined] ? false : true) : false;
|
|
939
|
+
|
|
590
940
|
type FieldKeys<D> = { [K in keyof D]: IsField<D[K]> extends true ? K : never }[keyof D];
|
|
591
941
|
|
|
592
|
-
|
|
593
|
-
|
|
942
|
+
// The keys that may be left out: the optional ones, and on the input side
|
|
943
|
+
// those with a default, which parse fills; on the output side a defaulted
|
|
944
|
+
// key is always there.
|
|
945
|
+
type OptionalKeys<D, M extends Mode> = {
|
|
946
|
+
[K in FieldKeys<D>]: M extends 'output'
|
|
947
|
+
? Defaulted<D[K]> extends true
|
|
948
|
+
? never
|
|
949
|
+
: IsOptionalKey<K, D[K]> extends true
|
|
950
|
+
? K
|
|
951
|
+
: never
|
|
952
|
+
: IsOptionalKey<K, D[K]> extends true
|
|
953
|
+
? K
|
|
954
|
+
: Defaulted<D[K]> extends true
|
|
955
|
+
? K
|
|
956
|
+
: never;
|
|
594
957
|
}[FieldKeys<D>];
|
|
595
958
|
|
|
596
|
-
type RequiredKeys<D> = Exclude<FieldKeys<D>, OptionalKeys<D>>;
|
|
959
|
+
type RequiredKeys<D, M extends Mode> = Exclude<FieldKeys<D>, OptionalKeys<D, M>>;
|
|
960
|
+
|
|
961
|
+
// An optional property accepts null and undefined, as check does; a key that
|
|
962
|
+
// is optional only through its default accepts a missing value.
|
|
963
|
+
type OptionalProperty<K, X, E, M extends Mode> =
|
|
964
|
+
IsOptionalKey<K, X> extends true ? Value<X, E, M> | null | undefined : Value<X, E, M>;
|
|
965
|
+
|
|
966
|
+
// On the output side a defaulted key is present: null stays where the key
|
|
967
|
+
// was optional, undefined is gone.
|
|
968
|
+
type RequiredProperty<K, X, E, M extends Mode> =
|
|
969
|
+
Defaulted<X> extends true
|
|
970
|
+
? Exclude<Value<X, E, M>, undefined> | (IsOptionalKey<K, X> extends true ? null : never)
|
|
971
|
+
: Value<X, E, M>;
|
|
597
972
|
|
|
598
973
|
type FieldName<K> = K extends `${infer F}?` ? F : K;
|
|
599
974
|
|
|
@@ -602,33 +977,35 @@ type Simplify<T> = { [K in keyof T]: T[K] };
|
|
|
602
977
|
// The fields of a struct: optional ones (`'key?'`, `'?type'`, `required:
|
|
603
978
|
// false`) are optional properties that accept null and undefined, as `check`
|
|
604
979
|
// does.
|
|
605
|
-
type Struct<D, E> = Simplify<
|
|
606
|
-
{
|
|
607
|
-
-readonly [K in
|
|
980
|
+
type Struct<D, E, M extends Mode> = Simplify<
|
|
981
|
+
{
|
|
982
|
+
-readonly [K in RequiredKeys<D, M> as FieldName<K>]-?: RequiredProperty<K, D[K], E, M>;
|
|
983
|
+
} & {
|
|
984
|
+
-readonly [K in OptionalKeys<D, M> as FieldName<K>]?: OptionalProperty<K, D[K], E, M>;
|
|
608
985
|
}
|
|
609
986
|
>;
|
|
610
987
|
|
|
611
988
|
// A projection copies fields from its parent, known only through `E`.
|
|
612
|
-
type Projection<D, E> = D extends {
|
|
989
|
+
type Projection<D, E, M extends Mode> = D extends {
|
|
613
990
|
Projection: { schema: infer S extends string; fields: readonly (infer F)[] };
|
|
614
991
|
}
|
|
615
992
|
? S extends keyof E
|
|
616
|
-
? Pick<
|
|
993
|
+
? Pick<Value<E[S], E, M>, Extract<F, keyof Value<E[S], E, M>>>
|
|
617
994
|
: unknown
|
|
618
995
|
: unknown;
|
|
619
996
|
|
|
620
|
-
type WithKind<D, K extends keyof D, E> = K extends 'Projection'
|
|
621
|
-
? Projection<D, E>
|
|
622
|
-
: Struct<Omit<D, K>, E>;
|
|
997
|
+
type WithKind<D, K extends keyof D, E, M extends Mode> = K extends 'Projection'
|
|
998
|
+
? Projection<D, E, M>
|
|
999
|
+
: Struct<Omit<D, K>, E, M>;
|
|
623
1000
|
|
|
624
|
-
type ObjectDef<D, E> =
|
|
1001
|
+
type ObjectDef<D, E, M extends Mode> =
|
|
625
1002
|
KindKey<D> extends infer K
|
|
626
1003
|
? [K] extends [never]
|
|
627
1004
|
? D extends { type: infer T extends string }
|
|
628
|
-
? LongForm<T, D, E>
|
|
629
|
-
: Shorthand<D, E>
|
|
1005
|
+
? LongForm<T, D, E, M>
|
|
1006
|
+
: Shorthand<D, E, M>
|
|
630
1007
|
: K extends keyof D
|
|
631
|
-
? WithKind<D, K, E>
|
|
1008
|
+
? Decoded<WithKind<D, K, E, M>, D, M>
|
|
632
1009
|
: never
|
|
633
1010
|
: never;
|
|
634
1011
|
|
|
@@ -636,30 +1013,39 @@ type ObjectDef<D, E> =
|
|
|
636
1013
|
type IdOf<Name extends string, Def> =
|
|
637
1014
|
StoreOf<Def> extends 'persistent' ? { [K in `${Uncapitalize<Name>}Id`]?: string } : {};
|
|
638
1015
|
|
|
639
|
-
|
|
640
|
-
// optional map of entity definitions by name (the second argument of `Model`
|
|
641
|
-
// as an object): with it a reference to a memory kind is the record and a
|
|
642
|
-
// projection has its parent's fields; without it a memory reference is
|
|
643
|
-
// `unknown` and a stored one is its id. A stored kind's own id field depends
|
|
644
|
-
// on the entity's name, which a definition does not carry: `InferEntity`
|
|
645
|
-
// adds it.
|
|
646
|
-
export type Infer<D, E = {}> =
|
|
1016
|
+
type Value<D, E, M extends Mode> =
|
|
647
1017
|
IsAny<D> extends true
|
|
648
1018
|
? any
|
|
649
1019
|
: D extends string
|
|
650
1020
|
? D extends `?${infer N}`
|
|
651
|
-
?
|
|
1021
|
+
? Value<N, E, M> | null | undefined
|
|
652
1022
|
: IsCapitalized<D> extends true
|
|
653
|
-
? Referenced<D, undefined, E>
|
|
1023
|
+
? Referenced<D, undefined, E, M>
|
|
654
1024
|
: ScalarOf<D>
|
|
655
1025
|
: D extends Schema<infer X>
|
|
656
|
-
?
|
|
1026
|
+
? Value<X, E, M>
|
|
657
1027
|
: D extends readonly unknown[]
|
|
658
|
-
? TupleOf<D, E>
|
|
1028
|
+
? TupleOf<D, E, M>
|
|
659
1029
|
: D extends object
|
|
660
|
-
? ObjectDef<D, E>
|
|
1030
|
+
? ObjectDef<D, E, M>
|
|
661
1031
|
: unknown;
|
|
662
1032
|
|
|
1033
|
+
// The type of a value `check` and `parse` accept for the definition `D` (a
|
|
1034
|
+
// key with a `default` may be left out, parse fills it). `E` is an optional
|
|
1035
|
+
// map of entity definitions by name (the second argument of `Model` as an
|
|
1036
|
+
// object): with it a reference to a memory kind is the record and a
|
|
1037
|
+
// projection has its parent's fields; without it a memory reference is
|
|
1038
|
+
// `unknown` and a stored one is its id. A stored kind's own id field depends
|
|
1039
|
+
// on the entity's name, which a definition does not carry: `InferEntity`
|
|
1040
|
+
// adds it.
|
|
1041
|
+
export type Infer<D, E = {}> = Value<D, E, 'input'>;
|
|
1042
|
+
|
|
1043
|
+
// The type of the value `parse` gives for the definition `D`: `Infer<D, E>`
|
|
1044
|
+
// with the result of every `parse` function (a decoder of a field, the
|
|
1045
|
+
// schema's own) in place of the value it decodes, and every defaulted key
|
|
1046
|
+
// present.
|
|
1047
|
+
export type InferOutput<D, E = {}> = Value<D, E, 'output'>;
|
|
1048
|
+
|
|
663
1049
|
// The value type of a `Schema<D>` instance: `InferSchema<typeof schema>`.
|
|
664
1050
|
export type InferSchema<S> = S extends Schema<infer D> ? Infer<D> : never;
|
|
665
1051
|
|
|
@@ -676,14 +1062,15 @@ export type InferEntity<E, Name extends keyof E & string> = Simplify<
|
|
|
676
1062
|
// devDependency); tests/types/standard.test-d.ts pins that a `Schema<D>` is a
|
|
677
1063
|
// `StandardSchemaV1<Infer<D>, Infer<D>>` of that package.
|
|
678
1064
|
|
|
679
|
-
// The vendor-specific options of a `validate` call are the options of `
|
|
1065
|
+
// The vendor-specific options of a `validate` call are the options of `parse`.
|
|
680
1066
|
export interface StandardOptions {
|
|
681
|
-
readonly libraryOptions?:
|
|
1067
|
+
readonly libraryOptions?: ParseOptions | undefined;
|
|
682
1068
|
}
|
|
683
1069
|
|
|
684
|
-
//
|
|
685
|
-
//
|
|
686
|
-
//
|
|
1070
|
+
// The result of parse as the specification reads it: the value parse gives
|
|
1071
|
+
// (the value given, when there was nothing to apply), or the issues of an
|
|
1072
|
+
// invalid one, the result's own (`message` and `path`, as the specification
|
|
1073
|
+
// reads them, plus `code` and `params`).
|
|
687
1074
|
export type StandardResult<T> =
|
|
688
1075
|
| { readonly value: T; readonly issues?: undefined }
|
|
689
1076
|
| { readonly issues: ReadonlyArray<ValidationIssue> };
|
|
@@ -691,10 +1078,18 @@ export type StandardResult<T> =
|
|
|
691
1078
|
export interface StandardProps<D = string | object> {
|
|
692
1079
|
readonly version: 1;
|
|
693
1080
|
readonly vendor: 'alexify.metaschema';
|
|
694
|
-
|
|
695
|
-
//
|
|
696
|
-
|
|
697
|
-
|
|
1081
|
+
// `parse` with the policies of the schema; a promise of the result for a
|
|
1082
|
+
// schema with an async validator, decoder or custom type.
|
|
1083
|
+
readonly validate: (
|
|
1084
|
+
value: unknown,
|
|
1085
|
+
options?: StandardOptions,
|
|
1086
|
+
) => StandardResult<InferOutput<D>> | Promise<StandardResult<InferOutput<D>>>;
|
|
1087
|
+
// Standard JSON Schema: `input(options)` and `output(options)` by target.
|
|
1088
|
+
// Present once `register()` of the generators subpath has run.
|
|
1089
|
+
readonly jsonSchema: StandardConverter;
|
|
1090
|
+
// What parse accepts and what it gives. Type-level only: no property of
|
|
1091
|
+
// the object holds it.
|
|
1092
|
+
readonly types?: { readonly input: Infer<D>; readonly output: InferOutput<D> } | undefined;
|
|
698
1093
|
}
|
|
699
1094
|
|
|
700
1095
|
// `D` is the definition the schema was built from, kept as a type only (no
|
|
@@ -708,6 +1103,18 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
708
1103
|
|
|
709
1104
|
static from<const D extends string | object>(raw: D, namespaces?: Array<Model>): Schema<D>;
|
|
710
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;
|
|
711
1118
|
|
|
712
1119
|
kind: Kind;
|
|
713
1120
|
scope: Scope;
|
|
@@ -737,7 +1144,34 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
737
1144
|
checkConsistency(): Array<string>;
|
|
738
1145
|
findReference(name: string): Schema | null;
|
|
739
1146
|
check(value: unknown, options?: CheckOptions): ValidationResult;
|
|
740
|
-
|
|
1147
|
+
// The same check, with the validators that return a promise settled after
|
|
1148
|
+
// the walk, in field order.
|
|
1149
|
+
checkAsync(value: unknown, options?: CheckOptions): Promise<ValidationResult>;
|
|
1150
|
+
// Validates a value and builds the value of the program from it: defaults,
|
|
1151
|
+
// the check, `unknown: 'strip'`, the decoders of the fields (parts before
|
|
1152
|
+
// the whole) and the schema's own `parse`. The value given is never
|
|
1153
|
+
// written to; it is the output itself when there was nothing to apply.
|
|
1154
|
+
parse(value: unknown, options?: ParseOptions): ParseResult<InferOutput<D>>;
|
|
1155
|
+
parseAsync(value: unknown, options?: ParseOptions): Promise<ParseResult<InferOutput<D>>>;
|
|
1156
|
+
// The other direction: the schema's own `serialize`, then the serializers
|
|
1157
|
+
// of the fields, from the value down to its parts; with `check` the
|
|
1158
|
+
// verdict of check over the result as well.
|
|
1159
|
+
serialize(
|
|
1160
|
+
value: InferOutput<D>,
|
|
1161
|
+
options: SerializeOptions & { check: true },
|
|
1162
|
+
): SerializeChecked<Infer<D>>;
|
|
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>;
|
|
1168
|
+
// The two methods below exist once `register()` of the generators subpath
|
|
1169
|
+
// has run; the subpath's `toJSONSchema(schema)` and `toTypeScript(schema)`
|
|
1170
|
+
// are the same without it.
|
|
1171
|
+
// The schema as a JSON Schema document of the target dialect.
|
|
1172
|
+
toJSONSchema(options?: JSONSchemaOptions): JSONSchema;
|
|
1173
|
+
// The schema as a TypeScript interface, the description of a field as JSDoc.
|
|
1174
|
+
toInterface(options?: InterfaceOptions): string;
|
|
741
1175
|
attach(...namespaces: Array<Model>): void;
|
|
742
1176
|
detach(...namespaces: Array<Model>): void;
|
|
743
1177
|
toString(): string;
|
|
@@ -761,5 +1195,24 @@ export class Model {
|
|
|
761
1195
|
database?: Record<string, unknown> | null,
|
|
762
1196
|
options?: ModelOptions,
|
|
763
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;
|
|
1208
|
+
// The members below exist once `register()` of the generators subpath has
|
|
1209
|
+
// run; the subpath's `toJSONSchema(model)` and `toTypeScript(model)` are the
|
|
1210
|
+
// same without it.
|
|
1211
|
+
// Every entity as a JSON Schema definition (`$defs`, `definitions` or
|
|
1212
|
+
// `components.schemas` by target), or the document of `options.root`.
|
|
1213
|
+
toJSONSchema(options?: ModelJSONSchemaOptions): JSONSchema;
|
|
1214
|
+
// The interfaces of every entity in dependency order; `dts` is the same
|
|
1215
|
+
// without options.
|
|
1216
|
+
toTypeScript(options?: InterfaceOptions): string;
|
|
764
1217
|
get dts(): string;
|
|
765
1218
|
}
|