@alexify/metaschema 3.1.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 +314 -1
- package/README.md +535 -20
- package/SECURITY.md +26 -7
- package/index.d.ts +496 -2
- package/package.json +53 -5
- package/src/codec.js +239 -74
- package/src/coerce/index.d.ts +34 -0
- package/src/coerce/index.js +352 -0
- package/src/compile/code.js +1 -1
- package/src/compile/emit.js +32 -7
- package/src/compile/index.d.ts +6 -0
- package/src/compile/index.js +12 -1
- package/src/context.js +11 -10
- package/src/derive/index.d.ts +86 -0
- package/src/derive/index.js +266 -0
- package/src/errors.js +28 -1
- package/src/formats/index.d.ts +31 -0
- package/src/formats/index.js +557 -0
- package/src/generators/fromjsonschema.js +16 -14
- package/src/generators/index.js +2 -1
- package/src/generators/jsonschema.js +47 -13
- package/src/issues.js +37 -20
- package/src/kinds.js +11 -2
- package/src/lint.js +24 -0
- package/src/locales/en.js +11 -2
- package/src/locales/uk.js +10 -2
- package/src/metadata.js +7 -2
- package/src/model/data.js +17 -0
- package/src/model/diff.js +1 -0
- package/src/model/index.d.ts +14 -2
- package/src/model/index.js +13 -10
- package/src/model/infer.js +483 -0
- package/src/model/merge.js +10 -6
- package/src/model/restore.js +3 -3
- package/src/model/sample.js +8 -5
- package/src/model.js +34 -4
- 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/preprocessor.js +1 -0
- package/src/prototypes/abstract.js +9 -3
- package/src/prototypes/collections.js +4 -3
- package/src/prototypes/scalars.js +4 -4
- package/src/prototypes/union.js +2 -2
- package/src/result.js +6 -0
- package/src/rules.js +53 -4
- package/src/schema.js +53 -28
- package/src/snapshot.js +1 -1
- package/src/stream/index.d.ts +68 -0
- package/src/stream/index.js +247 -0
- package/src/struct.js +1 -1
- package/src/types.js +33 -1
- package/src/util.js +18 -0
package/SECURITY.md
CHANGED
|
@@ -55,13 +55,32 @@ Some things work as intended and are not vulnerabilities:
|
|
|
55
55
|
check the generator cannot build is reported (`compile().error`, `register({ onFallback })`),
|
|
56
56
|
never installed. The core entry never calls `new Function`.
|
|
57
57
|
- **Input shape is checked strictly.** Every key that is not in the schema is reported as
|
|
58
|
-
unexpected (`has unexpected keys:
|
|
59
|
-
unless the schema or the call says `unknown: 'ignore'`. Fields
|
|
60
|
-
null-prototype dictionary, so nothing on `Object.prototype` is ever read as a
|
|
61
|
-
metaschema never writes to the value it checks. A
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
unexpected (`has unexpected keys: ...`, the first ten of them), including `constructor`,
|
|
59
|
+
`__proto__` and `toString`, unless the schema or the call says `unknown: 'ignore'`. Fields
|
|
60
|
+
live in a null-prototype dictionary, so nothing on `Object.prototype` is ever read as a
|
|
61
|
+
field, and metaschema never writes to the value it checks. A built-in (`Map`, `Set`, `Date`)
|
|
62
|
+
is recognised by the name of its constructor and by its shape (an iterator, `getTime`), so a
|
|
63
|
+
value parsed from JSON that carries `{ "constructor": { "name": "Map" } }` is a type issue,
|
|
64
|
+
not a value the check iterates. A value with a `then` function is data: the passes of
|
|
65
|
+
`parse` and `parseAsync` await only the promises their own decoders make, never a thenable
|
|
66
|
+
of the value. A graph nested deeper than `maxDepth` embedded records (128 by default) is a
|
|
67
|
+
`depth` issue, not a stack overflow.
|
|
68
|
+
- **A `pattern` or a `format` is never run over a string longer than `length.max`.** The bound
|
|
69
|
+
is what keeps a backtracking expression fast, so give every pattern one (the lint says where
|
|
70
|
+
one is missing); the built-in formats are linear and anchored, a custom format's `test` is
|
|
71
|
+
the program's code, like a `validate` function. A pattern taken from a document given to
|
|
72
|
+
`fromJSONSchema` or a snapshot given to `fromJSON` runs as a regular expression in `check`
|
|
73
|
+
all the same: those documents are untrusted input for their structure (a keyword outside the
|
|
74
|
+
subset, a key named `__proto__`, a depth past the limit are errors, never a looser schema),
|
|
75
|
+
but a pattern in them is the importer's to review or to bound.
|
|
76
|
+
- **Samples are input.** `infer` of the model subpath walks the values it is given with a depth
|
|
77
|
+
limit, trusts no key (`__proto__`, `constructor` and the names of the methods of a field are
|
|
78
|
+
keys of the definition, written as own properties), and checks the definition it built
|
|
79
|
+
against every sample before it returns it. A key of a sample that reaches an error message
|
|
80
|
+
is truncated to 100 characters, as a key of a value under check is.
|
|
81
|
+
- **The error a stream ends with carries the record.** `ERR_INVALID_RECORD` of the stream
|
|
82
|
+
subpath has `record` and `result` on it, since the stream carries them no further; a logger
|
|
83
|
+
that serializes error objects writes the record.
|
|
65
84
|
- **Validation errors are data.** `check` collects problems as issues (a code, a path and params,
|
|
66
85
|
with a rendered message) instead of throwing on bad input. The messages contain field paths (which can include key names taken from the input,
|
|
67
86
|
truncated to 100 characters) and, for `enum`, the allowed values; they never contain the
|
package/index.d.ts
CHANGED
|
@@ -37,6 +37,7 @@ export type IssueCode =
|
|
|
37
37
|
| 'length'
|
|
38
38
|
| 'range'
|
|
39
39
|
| 'pattern'
|
|
40
|
+
| 'format'
|
|
40
41
|
| 'union'
|
|
41
42
|
| 'reference'
|
|
42
43
|
| 'circular'
|
|
@@ -56,6 +57,8 @@ export interface IssueParams {
|
|
|
56
57
|
range: { min: number | bigint | undefined; max: number | bigint | undefined; actual: number | bigint };
|
|
57
58
|
// The source of the pattern the string did not match.
|
|
58
59
|
pattern: { pattern: string };
|
|
60
|
+
// The name of the format the string is not of.
|
|
61
|
+
format: { format: string };
|
|
59
62
|
// No branch of a union matched: the branch names, or the discriminator
|
|
60
63
|
// values when the union has a discriminator (the path then ends with it).
|
|
61
64
|
union: { expected: unknown[]; discriminator: string | undefined };
|
|
@@ -139,8 +142,31 @@ export interface CheckOptions {
|
|
|
139
142
|
// does not have, which the output then leaves out.
|
|
140
143
|
export interface ParseOptions extends Omit<CheckOptions, 'unknown'> {
|
|
141
144
|
unknown?: 'reject' | 'ignore' | 'strip';
|
|
145
|
+
// Whether parse converts the input to the types of the fields before the
|
|
146
|
+
// check (`'42'` to a number, an ISO string to a Date): the schema's
|
|
147
|
+
// `coerce` metadata unless the call says otherwise, a field's own
|
|
148
|
+
// `coerce` key for its subtree. The converters are those `register()` of
|
|
149
|
+
// the `@alexify/metaschema/coerce` subpath installed; `check` never
|
|
150
|
+
// converts.
|
|
151
|
+
coerce?: boolean;
|
|
142
152
|
}
|
|
143
153
|
|
|
154
|
+
// The options of `coerce(schema, value)` of the `@alexify/metaschema/coerce`
|
|
155
|
+
// subpath: how references are followed and how deep, and an array given the
|
|
156
|
+
// path of every key the pass changed (a value converted, a default filled).
|
|
157
|
+
export interface CoerceOptions {
|
|
158
|
+
references?: CheckOptions['references'];
|
|
159
|
+
maxDepth?: number;
|
|
160
|
+
report?: PropertyKey[][];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// A converter of coercion: the value of a field of its type read from what
|
|
164
|
+
// the input gave, or the value itself when it cannot be read (the check then
|
|
165
|
+
// reports it as given). Registered process-wide by `register()` of the
|
|
166
|
+
// `@alexify/metaschema/coerce` subpath, or given to a model for its own type
|
|
167
|
+
// names (`ModelOptions.coercions`).
|
|
168
|
+
export type Coercion = (value: unknown, field: FieldType) => unknown;
|
|
169
|
+
|
|
144
170
|
// What parse returns: the output only for a valid value, the issues always.
|
|
145
171
|
export type ParseResult<T> =
|
|
146
172
|
| { valid: true; value: T; issues: [] }
|
|
@@ -192,6 +218,8 @@ export class ValidationResult {
|
|
|
192
218
|
add(error: ValidationReturn, code?: IssueCode): this;
|
|
193
219
|
flatten(): FlatIssues;
|
|
194
220
|
tree(): IssueTree;
|
|
221
|
+
// The result as data: what JSON.stringify(result) writes.
|
|
222
|
+
toJSON(): { valid: boolean; issues: ValidationIssue[] };
|
|
195
223
|
static issuesOf(error: ValidationReturn, path?: PropertyKey[], code?: IssueCode): ValidationIssue[];
|
|
196
224
|
static isInstance(error: unknown): boolean;
|
|
197
225
|
}
|
|
@@ -220,6 +248,9 @@ export interface CheckContext {
|
|
|
220
248
|
async: unknown[] | null;
|
|
221
249
|
// The context parse tries the branches of a plain union in; null in a check.
|
|
222
250
|
scratch: CheckContext | null;
|
|
251
|
+
// Whether the check read a field from one of its aliases: the pass after it
|
|
252
|
+
// renames only when it did.
|
|
253
|
+
renamed: boolean;
|
|
223
254
|
}
|
|
224
255
|
|
|
225
256
|
// A validator may return a promise of its result to checkAsync and parseAsync;
|
|
@@ -235,6 +266,9 @@ export interface FieldType {
|
|
|
235
266
|
required: boolean;
|
|
236
267
|
// The value may be null; the key is still required unless `required` is false.
|
|
237
268
|
nullable?: boolean;
|
|
269
|
+
// Whether parse converts the input of this field and what is under it,
|
|
270
|
+
// whatever the policy of the call or the schema.
|
|
271
|
+
coerce?: boolean;
|
|
238
272
|
// On a reference: check and render it as the record (true) or its id
|
|
239
273
|
// (false) whatever the kind of its target.
|
|
240
274
|
embed?: boolean;
|
|
@@ -296,6 +330,10 @@ export interface ModelOptions {
|
|
|
296
330
|
// 'shared' (the default) registers the types into the process-wide registry;
|
|
297
331
|
// 'isolated' gives the model its own copy of the built-in types to register into.
|
|
298
332
|
registry?: 'shared' | 'isolated';
|
|
333
|
+
// The converters of coercion for the model's own type names, read before
|
|
334
|
+
// the process-wide table for the fields of its entities: what an isolated
|
|
335
|
+
// registry is to the types.
|
|
336
|
+
coercions?: Record<string, Coercion>;
|
|
299
337
|
}
|
|
300
338
|
|
|
301
339
|
export interface KindMetadata {
|
|
@@ -306,6 +344,8 @@ export interface KindMetadata {
|
|
|
306
344
|
parent?: string;
|
|
307
345
|
// The default of `check` for keys the schema does not have.
|
|
308
346
|
unknown?: 'reject' | 'ignore';
|
|
347
|
+
// The default of `parse` (and so of `~standard.validate`) for coercion.
|
|
348
|
+
coerce?: boolean;
|
|
309
349
|
[key: string]: unknown;
|
|
310
350
|
}
|
|
311
351
|
|
|
@@ -346,6 +386,24 @@ export type DefinitionErrorCode =
|
|
|
346
386
|
| 'ERR_SAMPLE_TYPE'
|
|
347
387
|
// Thrown by `merge` of the same subpath for what no value can satisfy.
|
|
348
388
|
| 'ERR_MERGE_CONFLICT'
|
|
389
|
+
// Thrown by `infer` of the same subpath: a sample nested deeper than
|
|
390
|
+
// `maxDepth`, and a definition that rejects a sample it was inferred from
|
|
391
|
+
// (a bug of the inference, to report).
|
|
392
|
+
| 'ERR_INFER_DEPTH'
|
|
393
|
+
| 'ERR_INFER_MISMATCH'
|
|
394
|
+
// Thrown by `register` of the `@alexify/metaschema/formats` subpath: an
|
|
395
|
+
// entry that is not a format, and a name registered again with another
|
|
396
|
+
// test.
|
|
397
|
+
| 'ERR_INVALID_FORMAT'
|
|
398
|
+
| 'ERR_FORMAT_REGISTERED'
|
|
399
|
+
// Thrown by the functions of the `@alexify/metaschema/derive` subpath for
|
|
400
|
+
// a key the schema has no field of, located in the schema.
|
|
401
|
+
| 'ERR_UNKNOWN_FIELD'
|
|
402
|
+
// Thrown by `register` of the `@alexify/metaschema/coerce` subpath: an
|
|
403
|
+
// entry that is not a converter of a type, and a name registered again
|
|
404
|
+
// with another function.
|
|
405
|
+
| 'ERR_INVALID_COERCION'
|
|
406
|
+
| 'ERR_COERCION_REGISTERED'
|
|
349
407
|
// Thrown by `compile` and `register` of the `@alexify/metaschema/compile`
|
|
350
408
|
// subpath with `jit: true` where code generation from strings is not allowed.
|
|
351
409
|
| 'ERR_NO_EVAL'
|
|
@@ -368,7 +426,18 @@ export class SchemaDefinitionError extends TypeError {
|
|
|
368
426
|
// `ERR_INVALID_TARGET` is the same kind of error from a function of a subpath
|
|
369
427
|
// given something other than what it takes (a Schema, a Model, a snapshot, a
|
|
370
428
|
// list of changes).
|
|
371
|
-
export type RuntimeErrorCode =
|
|
429
|
+
export type RuntimeErrorCode =
|
|
430
|
+
| 'ERR_ASYNC_VALIDATOR'
|
|
431
|
+
| 'ERR_INVALID_TARGET'
|
|
432
|
+
// An option of a call that is not what it must be (`maxErrors`, the
|
|
433
|
+
// `coerce` of a parse, the options of the stream, derive and mongodb
|
|
434
|
+
// subpaths): the call is wrong, not the definition.
|
|
435
|
+
| 'ERR_INVALID_OPTION'
|
|
436
|
+
| 'ERR_INVALID_RECORD'
|
|
437
|
+
// A parse that coerces before `register()` of the `@alexify/metaschema/coerce`
|
|
438
|
+
// subpath ran (there is no converter to run), and a document written with
|
|
439
|
+
// ObjectIds before `register({ bson })` of `@alexify/metaschema/mongodb`.
|
|
440
|
+
| 'ERR_NOT_REGISTERED';
|
|
372
441
|
|
|
373
442
|
// ---------------------------------------------------------------------------
|
|
374
443
|
// The snapshot: `model.toJSON()`, what JSON.stringify(model) writes. The
|
|
@@ -492,6 +561,61 @@ export interface MergeOptions {
|
|
|
492
561
|
metadata?: Partial<KindMetadata>;
|
|
493
562
|
}
|
|
494
563
|
|
|
564
|
+
// The options of every function of the `@alexify/metaschema/derive` subpath.
|
|
565
|
+
export interface DeriveOptions {
|
|
566
|
+
// The name of the derived schema: the source's by default.
|
|
567
|
+
name?: string;
|
|
568
|
+
// The kind metadata of the derived schema, over the source's (a stored
|
|
569
|
+
// kind or a projection is a struct, keeping its `unknown`).
|
|
570
|
+
metadata?: Partial<KindMetadata>;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// The options of `partial`: the fields to make optional (every one by
|
|
574
|
+
// default), `deep` into nested structs, and `defaults` to keep the defaults
|
|
575
|
+
// a patch would otherwise fill.
|
|
576
|
+
export interface PartialOptions<K extends string = string> extends DeriveOptions {
|
|
577
|
+
keys?: readonly K[];
|
|
578
|
+
deep?: boolean;
|
|
579
|
+
defaults?: boolean;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// The options of `required`: the fields to make required (every one by
|
|
583
|
+
// default) and `deep` into nested structs.
|
|
584
|
+
export interface RequiredOptions<K extends string = string> extends DeriveOptions {
|
|
585
|
+
keys?: readonly K[];
|
|
586
|
+
deep?: boolean;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// The options of `infer` of the `@alexify/metaschema/model` subpath (the
|
|
590
|
+
// name sits beside the type `Infer<D>`: the function reads samples, the type
|
|
591
|
+
// reads a definition).
|
|
592
|
+
export interface InferOptions {
|
|
593
|
+
// What comes back: the definition ('definition', the default), or a Schema
|
|
594
|
+
// built from it ('schema').
|
|
595
|
+
as?: 'definition' | 'schema';
|
|
596
|
+
// The name of the Schema, and of the error locations ('').
|
|
597
|
+
name?: string;
|
|
598
|
+
// The kind of a root that is a struct ('struct'); a stored kind adds its
|
|
599
|
+
// id field, which wins over an inferred field of that name.
|
|
600
|
+
kind?: string;
|
|
601
|
+
// The most distinct strings a place may hold to be an enum (8), each
|
|
602
|
+
// value seen twice on average over at least four samples; 0 or false never.
|
|
603
|
+
enum?: number | false;
|
|
604
|
+
// Whether whole numbers are `integer` (true) rather than `number`.
|
|
605
|
+
integer?: boolean;
|
|
606
|
+
// The distinct keys past which records of a place read as a dictionary
|
|
607
|
+
// (16) when every key is missing from some record and the values are of
|
|
608
|
+
// one kind; keys that are not names are a dictionary from one sample on;
|
|
609
|
+
// false keeps every record a struct.
|
|
610
|
+
map?: number | false;
|
|
611
|
+
// How deep a sample may nest (128, the most); past it `ERR_INFER_DEPTH`.
|
|
612
|
+
maxDepth?: number;
|
|
613
|
+
// Whether a place whose every string passes a registered format is that
|
|
614
|
+
// format (true); the formats subpath registers them, in the order `infer`
|
|
615
|
+
// tries them, and nothing is registered until its `register()` ran.
|
|
616
|
+
formats?: boolean;
|
|
617
|
+
}
|
|
618
|
+
|
|
495
619
|
// The options of `sample` of the `@alexify/metaschema/model` subpath.
|
|
496
620
|
export interface SampleOptions {
|
|
497
621
|
// The seed of the generator (1): the same seed gives the same value.
|
|
@@ -507,6 +631,50 @@ export interface SampleOptions {
|
|
|
507
631
|
maxDepth?: number;
|
|
508
632
|
}
|
|
509
633
|
|
|
634
|
+
// The options of `checkEach` and `checkEachAsync` of the
|
|
635
|
+
// `@alexify/metaschema/stream` subpath: those of `check`, and how many invalid
|
|
636
|
+
// records end the iteration (no limit by default).
|
|
637
|
+
export interface EachOptions extends CheckOptions {
|
|
638
|
+
maxInvalid?: number;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// The options of `parseEach` and `parseEachAsync`: those of `parse`, and
|
|
642
|
+
// `maxInvalid`.
|
|
643
|
+
export interface ParseEachOptions extends ParseOptions {
|
|
644
|
+
maxInvalid?: number;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// The option of the async pair: how many records are in flight at once (1).
|
|
648
|
+
export interface EachAsyncOptions {
|
|
649
|
+
concurrency?: number;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// The options of `transform`: those of `parse` (or of `check`, with
|
|
653
|
+
// `mode: 'check'`), `maxInvalid`, and what the stream does with an invalid
|
|
654
|
+
// record.
|
|
655
|
+
export interface TransformOptions extends ParseOptions {
|
|
656
|
+
// What goes on down the stream: the parsed value ('parse', the default) or
|
|
657
|
+
// the record as it is ('check').
|
|
658
|
+
mode?: 'parse' | 'check';
|
|
659
|
+
// Whether the stream runs `parseAsync`/`checkAsync` (false).
|
|
660
|
+
async?: boolean;
|
|
661
|
+
// Told of every invalid record, which is then dropped; without it the
|
|
662
|
+
// stream ends with the error at the first one.
|
|
663
|
+
onInvalid?: (record: unknown, result: ValidationResult | ParseResult<unknown>, index: number) => void;
|
|
664
|
+
// After that many invalid records the stream ends with the error even with
|
|
665
|
+
// `onInvalid` (no limit by default).
|
|
666
|
+
maxInvalid?: number;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// What `checkEach` and its kin yield for a record: its position in the
|
|
670
|
+
// sequence, the record as the source gave it, and the result of `check` or
|
|
671
|
+
// `parse`.
|
|
672
|
+
export interface RecordEntry<T, R> {
|
|
673
|
+
index: number;
|
|
674
|
+
record: T;
|
|
675
|
+
result: R;
|
|
676
|
+
}
|
|
677
|
+
|
|
510
678
|
export const KIND: Array<string>;
|
|
511
679
|
export const KIND_STORED: Array<string>;
|
|
512
680
|
export const KIND_MEMORY: Array<string>;
|
|
@@ -563,6 +731,11 @@ export interface JSONSchemaOptions {
|
|
|
563
731
|
// the default: a stored kind as an id, a memory kind as a `$ref` to its
|
|
564
732
|
// definition), every reference as the record ('embed') or as an id ('id').
|
|
565
733
|
references?: 'kind' | 'embed' | 'id';
|
|
734
|
+
// The mongodb target only: the ids of stored records as the database holds
|
|
735
|
+
// them ('objectId': `_id` an ObjectId, required at the root, and every
|
|
736
|
+
// reference an ObjectId, as `toDocument` of `@alexify/metaschema/mongodb`
|
|
737
|
+
// writes them), or as the program does ('string', the default).
|
|
738
|
+
ids?: 'string' | 'objectId';
|
|
566
739
|
// The pointer the `$ref` of a definition starts with, and where the
|
|
567
740
|
// definitions are placed in the document: '#/$defs/' for draft-2020-12,
|
|
568
741
|
// '#/definitions/' for draft-07, '#/components/schemas/' for openapi-3.0.
|
|
@@ -1055,12 +1228,259 @@ export type InferEntity<E, Name extends keyof E & string> = Simplify<
|
|
|
1055
1228
|
Infer<E[Name], E> & IdOf<Name, E[Name]>
|
|
1056
1229
|
>;
|
|
1057
1230
|
|
|
1231
|
+
// The definitions of derived schemas (the `@alexify/metaschema/derive`
|
|
1232
|
+
// subpath): what `pick`, `omit`, `partial`, `required` and `extend` build,
|
|
1233
|
+
// as a definition `Infer` reads. A derived definition always has a kind as
|
|
1234
|
+
// its first key, so its fields read as a struct whatever their names.
|
|
1235
|
+
|
|
1236
|
+
// The definition a schema type was built from.
|
|
1237
|
+
export type DefinitionOf<S> = S extends Schema<infer D> ? D : never;
|
|
1238
|
+
|
|
1239
|
+
// A schema whose definition is not known (`Schema`, an entity of a model):
|
|
1240
|
+
// what is derived from it is not known either.
|
|
1241
|
+
type IsWide<D> = string extends D ? true : object extends D ? true : false;
|
|
1242
|
+
|
|
1243
|
+
type IsShorthand<D> = D extends { array: unknown }
|
|
1244
|
+
? true
|
|
1245
|
+
: D extends
|
|
1246
|
+
| { set: unknown }
|
|
1247
|
+
| { object: object }
|
|
1248
|
+
| { map: object }
|
|
1249
|
+
| { enum: readonly unknown[] }
|
|
1250
|
+
| { tuple: readonly unknown[] }
|
|
1251
|
+
| { union: readonly unknown[] }
|
|
1252
|
+
| { schema: object }
|
|
1253
|
+
| { one: string }
|
|
1254
|
+
| { many: string }
|
|
1255
|
+
? true
|
|
1256
|
+
: false;
|
|
1257
|
+
|
|
1258
|
+
// Whether a definition is a struct, as `Infer` reads it.
|
|
1259
|
+
type IsStructDef<D> = D extends string
|
|
1260
|
+
? false
|
|
1261
|
+
: D extends readonly unknown[]
|
|
1262
|
+
? false
|
|
1263
|
+
: D extends Schema<infer X>
|
|
1264
|
+
? IsStructDef<X>
|
|
1265
|
+
: D extends object
|
|
1266
|
+
? [KindKey<D>] extends [never]
|
|
1267
|
+
? D extends { type: string }
|
|
1268
|
+
? false
|
|
1269
|
+
: IsShorthand<D> extends true
|
|
1270
|
+
? false
|
|
1271
|
+
: true
|
|
1272
|
+
: true
|
|
1273
|
+
: false;
|
|
1274
|
+
|
|
1275
|
+
// What a derive function takes: a struct, or a schema of an unknown one.
|
|
1276
|
+
export type StructGuardOf<D> =
|
|
1277
|
+
IsWide<D> extends true ? unknown : IsStructDef<D> extends true ? unknown : never;
|
|
1278
|
+
|
|
1279
|
+
type OptionKey = 'validate' | 'parse' | 'serialize' | 'format';
|
|
1280
|
+
|
|
1281
|
+
type Fn = (...args: never[]) => unknown;
|
|
1282
|
+
|
|
1283
|
+
// The keys of the fields of a struct definition, as written ('nick?').
|
|
1284
|
+
type FieldKeyOf<D> = Exclude<FieldKeys<D>, KindKey<D>>;
|
|
1285
|
+
|
|
1286
|
+
// The keys of its calculated fields.
|
|
1287
|
+
type CalculatedKeyOf<D> = {
|
|
1288
|
+
[K in keyof D]: D[K] extends Fn ? (K extends OptionKey ? never : K) : never;
|
|
1289
|
+
}[keyof D];
|
|
1290
|
+
|
|
1291
|
+
// The names a derive function takes: the fields, the calculated fields and,
|
|
1292
|
+
// for a stored kind, its id (named after the entity, which the definition
|
|
1293
|
+
// does not carry).
|
|
1294
|
+
export type DeriveKeyOf<D> =
|
|
1295
|
+
IsWide<D> extends true
|
|
1296
|
+
? string
|
|
1297
|
+
: Extract<
|
|
1298
|
+
| FieldName<FieldKeyOf<D>>
|
|
1299
|
+
| CalculatedKeyOf<D>
|
|
1300
|
+
| (StoreOf<D> extends 'persistent' ? `${string}Id` | 'id' : never),
|
|
1301
|
+
string
|
|
1302
|
+
>;
|
|
1303
|
+
|
|
1304
|
+
type KindName<D> = [KindKey<D>] extends [never] ? 'Struct' : KindKey<D>;
|
|
1305
|
+
|
|
1306
|
+
// The kind of a derived definition: the one of the options, else a struct
|
|
1307
|
+
// for a kindless definition, a stored kind and a projection, else the
|
|
1308
|
+
// source's kind with its metadata.
|
|
1309
|
+
type DerivedKind<D, O> = O extends { metadata: { kind: infer K extends string } }
|
|
1310
|
+
? { [P in Capitalize<K>]: {} }
|
|
1311
|
+
: KindName<D> extends 'Struct' | 'Projection'
|
|
1312
|
+
? { Struct: {} }
|
|
1313
|
+
: Lowercase<KindName<D> & string> extends StoredKind
|
|
1314
|
+
? { Struct: {} }
|
|
1315
|
+
: { [P in KindName<D> & keyof D]: D[P] };
|
|
1316
|
+
|
|
1317
|
+
// A derived schema: of an unknown definition when the source's is unknown.
|
|
1318
|
+
export type DerivedSchema<D, R> = IsWide<D> extends true ? Schema : Schema<Simplify<R>>;
|
|
1319
|
+
|
|
1320
|
+
type Keep<D, K> = {
|
|
1321
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1322
|
+
? FieldName<P> extends K
|
|
1323
|
+
? P
|
|
1324
|
+
: never
|
|
1325
|
+
: P extends CalculatedKeyOf<D>
|
|
1326
|
+
? P extends K
|
|
1327
|
+
? P
|
|
1328
|
+
: never
|
|
1329
|
+
: never]: D[P];
|
|
1330
|
+
};
|
|
1331
|
+
|
|
1332
|
+
type Drop<D, K> = {
|
|
1333
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1334
|
+
? FieldName<P> extends K
|
|
1335
|
+
? never
|
|
1336
|
+
: P
|
|
1337
|
+
: P extends CalculatedKeyOf<D>
|
|
1338
|
+
? P extends K
|
|
1339
|
+
? never
|
|
1340
|
+
: P
|
|
1341
|
+
: never]: D[P];
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1344
|
+
export type PickDefinition<D, K, O = {}> = DerivedKind<D, O> & Keep<D, K>;
|
|
1345
|
+
|
|
1346
|
+
export type OmitDefinition<D, K, O = {}> = DerivedKind<D, O> & Drop<D, K>;
|
|
1347
|
+
|
|
1348
|
+
type KeysOf<O, D> = O extends { keys: readonly (infer K)[] } ? K : FieldName<FieldKeyOf<D>>;
|
|
1349
|
+
|
|
1350
|
+
type IsDeep<O> = O extends { deep: true } ? true : false;
|
|
1351
|
+
|
|
1352
|
+
// The fields of a nested struct as a definition of their own, in each form
|
|
1353
|
+
// a field may hold one in; never for anything else.
|
|
1354
|
+
type NestedOf<X> =
|
|
1355
|
+
X extends Schema<infer S>
|
|
1356
|
+
? IsStructDef<S> extends true
|
|
1357
|
+
? S
|
|
1358
|
+
: never
|
|
1359
|
+
: X extends { schema: infer S }
|
|
1360
|
+
? IsStructDef<S> extends true
|
|
1361
|
+
? S
|
|
1362
|
+
: never
|
|
1363
|
+
: IsStructDef<X> extends true
|
|
1364
|
+
? Omit<X, KindKey<X>>
|
|
1365
|
+
: never;
|
|
1366
|
+
|
|
1367
|
+
// A field holding a nested struct with the fields `F` in its place.
|
|
1368
|
+
type Reshaped<X, F> =
|
|
1369
|
+
X extends Schema<any>
|
|
1370
|
+
? F
|
|
1371
|
+
: X extends { schema: unknown }
|
|
1372
|
+
? Omit<X, 'schema' | 'validate' | 'parse' | 'serialize'> & { schema: F }
|
|
1373
|
+
: { [K in KindKey<X> & keyof X]: X[K] } & F;
|
|
1374
|
+
|
|
1375
|
+
type NoDefault<X, O> = O extends { defaults: true }
|
|
1376
|
+
? X
|
|
1377
|
+
: IsStructDef<X> extends true
|
|
1378
|
+
? X
|
|
1379
|
+
: Omit<X, 'default'>;
|
|
1380
|
+
|
|
1381
|
+
type OptionalDef<X, O> = X extends string
|
|
1382
|
+
? X
|
|
1383
|
+
: IsDeep<O> extends true
|
|
1384
|
+
? [NestedOf<X>] extends [never]
|
|
1385
|
+
? NoDefault<X, O>
|
|
1386
|
+
: Simplify<Reshaped<NoDefault<X, O>, PartialFields<NestedOf<X>, O>>>
|
|
1387
|
+
: NoDefault<X, O>;
|
|
1388
|
+
|
|
1389
|
+
type PartialFields<D, O> = {
|
|
1390
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1391
|
+
? `${FieldName<P> & string}?`
|
|
1392
|
+
: P extends CalculatedKeyOf<D>
|
|
1393
|
+
? P
|
|
1394
|
+
: never]: P extends FieldKeyOf<D> ? OptionalDef<D[P], O> : D[P];
|
|
1395
|
+
};
|
|
1396
|
+
|
|
1397
|
+
type PartialOf<D, O> = {
|
|
1398
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1399
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1400
|
+
? `${FieldName<P> & string}?`
|
|
1401
|
+
: P
|
|
1402
|
+
: P extends CalculatedKeyOf<D>
|
|
1403
|
+
? P
|
|
1404
|
+
: never]: P extends FieldKeyOf<D>
|
|
1405
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1406
|
+
? OptionalDef<D[P], O>
|
|
1407
|
+
: D[P]
|
|
1408
|
+
: D[P];
|
|
1409
|
+
};
|
|
1410
|
+
|
|
1411
|
+
export type PartialDefinition<D, O = {}> = DerivedKind<D, O> & PartialOf<D, O>;
|
|
1412
|
+
|
|
1413
|
+
type RequiredDef<X, O> = X extends `?${infer N}`
|
|
1414
|
+
? N
|
|
1415
|
+
: X extends string
|
|
1416
|
+
? X
|
|
1417
|
+
: Simplify<
|
|
1418
|
+
(X extends { type: `?${infer N}` }
|
|
1419
|
+
? Omit<X, 'type' | 'required'> & { type: N }
|
|
1420
|
+
: IsStructDef<X> extends true
|
|
1421
|
+
? X
|
|
1422
|
+
: Omit<X, 'required'>) extends infer Y
|
|
1423
|
+
? IsDeep<O> extends true
|
|
1424
|
+
? [NestedOf<X>] extends [never]
|
|
1425
|
+
? Y
|
|
1426
|
+
: X extends { schema: unknown }
|
|
1427
|
+
? Omit<Y, 'schema'> & { schema: RequiredFields<NestedOf<X>, O> }
|
|
1428
|
+
: X extends Schema<any>
|
|
1429
|
+
? RequiredFields<NestedOf<X>, O>
|
|
1430
|
+
: { [K in KindKey<X> & keyof X]: X[K] } & RequiredFields<NestedOf<X>, O>
|
|
1431
|
+
: Y
|
|
1432
|
+
: never
|
|
1433
|
+
>;
|
|
1434
|
+
|
|
1435
|
+
type RequiredFields<D, O> = {
|
|
1436
|
+
[P in keyof D as P extends FieldKeyOf<D> ? FieldName<P> : P]: P extends FieldKeyOf<D>
|
|
1437
|
+
? RequiredDef<D[P], O>
|
|
1438
|
+
: D[P];
|
|
1439
|
+
};
|
|
1440
|
+
|
|
1441
|
+
type RequiredOf<D, O> = {
|
|
1442
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1443
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1444
|
+
? FieldName<P>
|
|
1445
|
+
: P
|
|
1446
|
+
: P extends CalculatedKeyOf<D> | OptionKey
|
|
1447
|
+
? P
|
|
1448
|
+
: never]: P extends FieldKeyOf<D>
|
|
1449
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1450
|
+
? RequiredDef<D[P], O>
|
|
1451
|
+
: D[P]
|
|
1452
|
+
: D[P];
|
|
1453
|
+
};
|
|
1454
|
+
|
|
1455
|
+
export type RequiredDefinition<D, O = {}> = DerivedKind<D, O> & RequiredOf<D, O>;
|
|
1456
|
+
|
|
1457
|
+
// What an extension adds: its fields (a Schema's, without its kind).
|
|
1458
|
+
type ExtensionOf<X> = X extends Schema<infer S> ? Omit<S, KindKey<S>> : X;
|
|
1459
|
+
|
|
1460
|
+
// An extension whose fields are not known (a `Schema`, an entity of a
|
|
1461
|
+
// model): what it extends is not known either.
|
|
1462
|
+
export type ExtendedSchema<D, X, R> =
|
|
1463
|
+
X extends Schema<infer S> ? (IsWide<S> extends true ? Schema : DerivedSchema<D, R>) : DerivedSchema<D, R>;
|
|
1464
|
+
|
|
1465
|
+
export type ExtendDefinition<D, X, O = {}> = DerivedKind<D, O> & {
|
|
1466
|
+
[P in keyof D as P extends KindKey<D>
|
|
1467
|
+
? never
|
|
1468
|
+
: P extends FieldKeyOf<D>
|
|
1469
|
+
? FieldName<P> extends FieldName<keyof ExtensionOf<X>>
|
|
1470
|
+
? never
|
|
1471
|
+
: P
|
|
1472
|
+
: P]: D[P];
|
|
1473
|
+
} & ExtensionOf<X>;
|
|
1474
|
+
|
|
1475
|
+
// The names of the fields of a struct definition, as a definition of an enum.
|
|
1476
|
+
export type KeyofDefinition<D> = { enum: Array<FieldName<FieldKeyOf<D>>> };
|
|
1477
|
+
|
|
1058
1478
|
// Standard Schema v1 (https://standardschema.dev): the interface every schema
|
|
1059
1479
|
// exposes as `schema['~standard']`, so tRPC, TanStack Form, Hono and the other
|
|
1060
1480
|
// consumers of the specification accept it without an adapter. The shape is
|
|
1061
1481
|
// written out here because this file cannot import `@standard-schema/spec` (a
|
|
1062
1482
|
// devDependency); tests/types/standard.test-d.ts pins that a `Schema<D>` is a
|
|
1063
|
-
// `StandardSchemaV1<Infer<D>,
|
|
1483
|
+
// `StandardSchemaV1<Infer<D>, InferOutput<D>>` of that package.
|
|
1064
1484
|
|
|
1065
1485
|
// The vendor-specific options of a `validate` call are the options of `parse`.
|
|
1066
1486
|
export interface StandardOptions {
|
|
@@ -1115,6 +1535,11 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
1115
1535
|
// of the `@alexify/metaschema/model` subpath has run; its `merge(a, b)` is
|
|
1116
1536
|
// the same without it.
|
|
1117
1537
|
static merge(a: Schema, b: Schema, options?: MergeOptions): Schema;
|
|
1538
|
+
// A definition (or a Schema) that accepts every sample: exists once
|
|
1539
|
+
// `register()` of the `@alexify/metaschema/model` subpath has run; its
|
|
1540
|
+
// `infer(samples, options)` is the same without it.
|
|
1541
|
+
static infer(samples: Iterable<unknown>, options: InferOptions & { as: 'schema' }): Schema;
|
|
1542
|
+
static infer(samples: Iterable<unknown>, options?: InferOptions): object | string;
|
|
1118
1543
|
|
|
1119
1544
|
kind: Kind;
|
|
1120
1545
|
scope: Scope;
|
|
@@ -1165,6 +1590,72 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
1165
1590
|
// of the `@alexify/metaschema/model` subpath has run; its `sample(schema,
|
|
1166
1591
|
// options)` is the same without it.
|
|
1167
1592
|
sample(options?: SampleOptions): Infer<D>;
|
|
1593
|
+
// The value with the conversions of the schema and its defaults applied,
|
|
1594
|
+
// nothing checked: exists once `register()` of the
|
|
1595
|
+
// `@alexify/metaschema/coerce` subpath has run; its `coerce(schema,
|
|
1596
|
+
// value)` is the same without it.
|
|
1597
|
+
coerce(value: unknown, options?: CoerceOptions): unknown;
|
|
1598
|
+
// The six members below exist once `register()` of the
|
|
1599
|
+
// `@alexify/metaschema/derive` subpath has run; its `pick(schema, keys)`
|
|
1600
|
+
// and kin are the same without it. They are typed through `this`, never
|
|
1601
|
+
// through `D`: a member that maps `D` makes every comparison of two Schema
|
|
1602
|
+
// types instantiate the mapping ("excessively deep").
|
|
1603
|
+
pick<
|
|
1604
|
+
S extends Schema,
|
|
1605
|
+
const K extends readonly DeriveKeyOf<DefinitionOf<S>>[],
|
|
1606
|
+
const O extends DeriveOptions = {},
|
|
1607
|
+
>(
|
|
1608
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1609
|
+
keys: K,
|
|
1610
|
+
options?: O,
|
|
1611
|
+
): DerivedSchema<DefinitionOf<S>, PickDefinition<DefinitionOf<S>, K[number], O>>;
|
|
1612
|
+
omit<
|
|
1613
|
+
S extends Schema,
|
|
1614
|
+
const K extends readonly DeriveKeyOf<DefinitionOf<S>>[],
|
|
1615
|
+
const O extends DeriveOptions = {},
|
|
1616
|
+
>(
|
|
1617
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1618
|
+
keys: K,
|
|
1619
|
+
options?: O,
|
|
1620
|
+
): DerivedSchema<DefinitionOf<S>, OmitDefinition<DefinitionOf<S>, K[number], O>>;
|
|
1621
|
+
partial<S extends Schema, const O extends PartialOptions<DeriveKeyOf<DefinitionOf<S>>> = {}>(
|
|
1622
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1623
|
+
options?: O,
|
|
1624
|
+
): DerivedSchema<DefinitionOf<S>, PartialDefinition<DefinitionOf<S>, O>>;
|
|
1625
|
+
required<S extends Schema, const O extends RequiredOptions<DeriveKeyOf<DefinitionOf<S>>> = {}>(
|
|
1626
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1627
|
+
options?: O,
|
|
1628
|
+
): DerivedSchema<DefinitionOf<S>, RequiredDefinition<DefinitionOf<S>, O>>;
|
|
1629
|
+
extend<S extends Schema, const X extends object, const O extends DeriveOptions = {}>(
|
|
1630
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1631
|
+
extension: X,
|
|
1632
|
+
options?: O,
|
|
1633
|
+
): ExtendedSchema<DefinitionOf<S>, X, ExtendDefinition<DefinitionOf<S>, X, O>>;
|
|
1634
|
+
keyof<S extends Schema>(
|
|
1635
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1636
|
+
options?: DeriveOptions,
|
|
1637
|
+
): DerivedSchema<DefinitionOf<S>, KeyofDefinition<DefinitionOf<S>>>;
|
|
1638
|
+
// The five members below exist once `register()` of the
|
|
1639
|
+
// `@alexify/metaschema/stream` subpath has run; its `checkEach(schema,
|
|
1640
|
+
// records, options)` and kin are the same without it.
|
|
1641
|
+
checkEach<T>(
|
|
1642
|
+
records: Iterable<T>,
|
|
1643
|
+
options?: EachOptions,
|
|
1644
|
+
): Generator<RecordEntry<T, ValidationResult>, void, undefined>;
|
|
1645
|
+
parseEach<T>(
|
|
1646
|
+
records: Iterable<T>,
|
|
1647
|
+
options?: ParseEachOptions,
|
|
1648
|
+
): Generator<RecordEntry<T, ParseResult<InferOutput<D>>>, void, undefined>;
|
|
1649
|
+
checkEachAsync<T>(
|
|
1650
|
+
records: Iterable<T> | AsyncIterable<T>,
|
|
1651
|
+
options?: EachOptions & EachAsyncOptions,
|
|
1652
|
+
): AsyncGenerator<RecordEntry<T, ValidationResult>, void, undefined>;
|
|
1653
|
+
parseEachAsync<T>(
|
|
1654
|
+
records: Iterable<T> | AsyncIterable<T>,
|
|
1655
|
+
options?: ParseEachOptions & EachAsyncOptions,
|
|
1656
|
+
): AsyncGenerator<RecordEntry<T, ParseResult<InferOutput<D>>>, void, undefined>;
|
|
1657
|
+
transform(options: TransformOptions & { mode: 'check' }): TransformStream<unknown, Infer<D>>;
|
|
1658
|
+
transform(options?: TransformOptions): TransformStream<unknown, InferOutput<D>>;
|
|
1168
1659
|
// The two methods below exist once `register()` of the generators subpath
|
|
1169
1660
|
// has run; the subpath's `toJSONSchema(schema)` and `toTypeScript(schema)`
|
|
1170
1661
|
// are the same without it.
|
|
@@ -1181,6 +1672,9 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
1181
1672
|
|
|
1182
1673
|
export class Model {
|
|
1183
1674
|
types: TypeTable;
|
|
1675
|
+
// The converters of coercion the model was given (`ModelOptions.coercions`),
|
|
1676
|
+
// frozen, or null.
|
|
1677
|
+
readonly coercions: Readonly<Record<string, Coercion>> | null;
|
|
1184
1678
|
entities: Map<string, Schema>;
|
|
1185
1679
|
database: Record<string, unknown> | null;
|
|
1186
1680
|
order: Set<string>;
|