@alexify/metaschema 3.4.1 → 4.0.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 +162 -1
- package/README.md +22 -7
- package/SECURITY.md +5 -4
- package/index.d.ts +856 -460
- package/package.json +8 -6
- package/src/checks.js +72 -107
- package/src/codec.js +67 -58
- package/src/coerce/index.d.ts +31 -21
- package/src/coerce/index.js +64 -47
- package/src/compile/code.js +2 -2
- package/src/compile/emit.js +20 -31
- package/src/compile/index.d.ts +41 -25
- package/src/compile/index.js +45 -32
- package/src/context.js +10 -10
- package/src/definition/index.d.ts +10 -7
- package/src/definition/index.js +2 -1
- package/src/derive/index.d.ts +25 -17
- package/src/derive/index.js +14 -11
- package/src/errors.js +84 -59
- package/src/formats/index.d.ts +27 -21
- package/src/formats/index.js +15 -13
- package/src/generators/{fromjsonschema.js → from-json-schema.js} +170 -127
- package/src/generators/index.d.ts +23 -15
- package/src/generators/index.js +2 -2
- package/src/generators/{jsonschema.js → json-schema.js} +37 -43
- package/src/generators/typescript.js +9 -6
- package/src/index.js +2 -1
- package/src/issues.js +41 -42
- package/src/kinds.js +18 -16
- package/src/lint.js +15 -18
- package/src/locales/en.js +7 -7
- package/src/locales/uk.js +6 -6
- package/src/metadata.js +2 -2
- package/src/model/data.js +17 -20
- package/src/model/diff.js +29 -24
- package/src/model/index.d.ts +51 -35
- package/src/model/infer.js +69 -60
- package/src/model/merge.js +29 -18
- package/src/model/pattern.js +105 -102
- package/src/model/restore.js +24 -19
- package/src/model/sample.js +90 -100
- package/src/model.js +4 -2
- package/src/mongodb/bson.js +12 -12
- package/src/mongodb/document.js +7 -6
- package/src/mongodb/index.d.ts +54 -31
- package/src/mongodb/update.js +12 -9
- package/src/preprocessor.js +13 -13
- package/src/prototypes/abstract.js +8 -8
- package/src/prototypes/collections.js +7 -7
- package/src/prototypes/flags.js +1 -1
- package/src/prototypes/reference.js +3 -4
- package/src/prototypes/scalars.js +1 -1
- package/src/prototypes/schema.js +5 -6
- package/src/prototypes/tuple.js +6 -5
- package/src/prototypes/union.js +5 -4
- package/src/rules.js +23 -20
- package/src/runtime/browser.js +6 -1
- package/src/runtime/node.js +19 -2
- package/src/schema.js +15 -13
- package/src/snapshot.js +14 -12
- package/src/stream/index.d.ts +33 -21
- package/src/stream/index.js +37 -20
- package/src/struct.js +6 -6
- package/src/types.js +28 -22
- package/src/util.js +65 -64
- package/src/validation.js +10 -13
package/index.d.ts
CHANGED
|
@@ -17,8 +17,10 @@ export type KnownKind =
|
|
|
17
17
|
| 'struct'
|
|
18
18
|
| 'scalar';
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
/**
|
|
21
|
+
* A capitalized first key that is not a known kind is a custom kind; the
|
|
22
|
+
* intersection keeps the known names in completions.
|
|
23
|
+
*/
|
|
22
24
|
export type Kind = KnownKind | (string & {});
|
|
23
25
|
|
|
24
26
|
export type Cardinality = 'one-to-many' | 'many-to-one';
|
|
@@ -28,7 +30,7 @@ export interface Relation {
|
|
|
28
30
|
type: Cardinality;
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
/** Codes of the issues the library produces; a validator may use its own. */
|
|
32
34
|
export type IssueCode =
|
|
33
35
|
| 'required'
|
|
34
36
|
| 'type'
|
|
@@ -46,38 +48,48 @@ export type IssueCode =
|
|
|
46
48
|
| 'exception'
|
|
47
49
|
| 'custom';
|
|
48
50
|
|
|
49
|
-
|
|
51
|
+
/** The params of the issues the library produces, by code. */
|
|
50
52
|
export interface IssueParams {
|
|
51
53
|
required: {};
|
|
52
|
-
|
|
54
|
+
/** `key` is set when a key of an `object` or `map`, not a value, has the wrong type. */
|
|
53
55
|
type: { expected: string; received: string; key?: PropertyKey };
|
|
54
56
|
unexpected: { keys: string[] };
|
|
55
57
|
enum: { values: unknown[] };
|
|
56
58
|
length: { min: number | undefined; max: number | undefined; actual: number };
|
|
57
|
-
|
|
58
|
-
range: {
|
|
59
|
-
|
|
59
|
+
/** A `min`/`max` rule failed; the bounds are what the definition gave. */
|
|
60
|
+
range: {
|
|
61
|
+
min: number | bigint | undefined;
|
|
62
|
+
max: number | bigint | undefined;
|
|
63
|
+
actual: number | bigint;
|
|
64
|
+
};
|
|
65
|
+
/** The source of the pattern the string did not match. */
|
|
60
66
|
pattern: { pattern: string };
|
|
61
|
-
|
|
67
|
+
/** The name of the format the string is not of. */
|
|
62
68
|
format: { format: string };
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
/**
|
|
70
|
+
* The decimals the number may have: it has more, or it is not finite
|
|
71
|
+
* (`received` names NaN or the infinity then).
|
|
72
|
+
*/
|
|
65
73
|
decimals: { decimals: number; received?: string };
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
/**
|
|
75
|
+
* No branch of a union matched: the branch names, or the discriminator
|
|
76
|
+
* values when the union has a discriminator (the path then ends with it).
|
|
77
|
+
*/
|
|
68
78
|
union: { expected: unknown[]; discriminator: string | undefined };
|
|
69
79
|
reference: { entity: string };
|
|
70
80
|
circular: {};
|
|
71
81
|
depth: { max: number };
|
|
72
82
|
exception: { error: unknown };
|
|
73
|
-
|
|
83
|
+
/** A validator's own issue carries the params it gave, if any. */
|
|
74
84
|
custom: Record<string, unknown>;
|
|
75
85
|
}
|
|
76
86
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
87
|
+
/**
|
|
88
|
+
* An issue is data: the code, the path of keys from the root of the value, the
|
|
89
|
+
* message about the problem (without the location) and the params the message
|
|
90
|
+
* was rendered from. A validator's own issue carries the code and params it
|
|
91
|
+
* gave; `IssueOf<Code>` is the issue of one of the library's codes.
|
|
92
|
+
*/
|
|
81
93
|
export type ValidationIssue =
|
|
82
94
|
| {
|
|
83
95
|
[Code in IssueCode]: {
|
|
@@ -91,8 +103,10 @@ export type ValidationIssue =
|
|
|
91
103
|
|
|
92
104
|
export type IssueOf<Code extends IssueCode> = Extract<ValidationIssue, { code: Code }>;
|
|
93
105
|
|
|
94
|
-
|
|
95
|
-
|
|
106
|
+
/**
|
|
107
|
+
* What a validator may return: an issue of its own needs only a message. Its
|
|
108
|
+
* path is relative to the field the validator belongs to.
|
|
109
|
+
*/
|
|
96
110
|
export interface IssueInput {
|
|
97
111
|
code?: string;
|
|
98
112
|
path?: PropertyKey | PropertyKey[];
|
|
@@ -100,6 +114,11 @@ export interface IssueInput {
|
|
|
100
114
|
params?: Record<string, unknown>;
|
|
101
115
|
}
|
|
102
116
|
|
|
117
|
+
/**
|
|
118
|
+
* What a validator or a custom `checkType` may return: `true`, `null` or `undefined` when the
|
|
119
|
+
* value is valid; `false` for a `custom` issue (a `type` issue from a `checkType`); a message;
|
|
120
|
+
* an issue; a list of those; or a `ValidationResult`, whose issues are relative to the field.
|
|
121
|
+
*/
|
|
103
122
|
export type ValidationReturn =
|
|
104
123
|
| boolean
|
|
105
124
|
| string
|
|
@@ -110,74 +129,96 @@ export type ValidationReturn =
|
|
|
110
129
|
| undefined
|
|
111
130
|
| void;
|
|
112
131
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
132
|
+
/**
|
|
133
|
+
* The messages of a locale: a renderer per issue code, from the params of the
|
|
134
|
+
* issue to the text that follows the field, and `field` for the location
|
|
135
|
+
* prefix of an error line. A partial table falls back to English.
|
|
136
|
+
*/
|
|
116
137
|
export type Locale = {
|
|
117
138
|
[Code in IssueCode]?: (params: IssueParams[Code], issue?: ValidationIssue) => string;
|
|
118
139
|
} & {
|
|
119
140
|
field?: (path: string) => string;
|
|
120
141
|
};
|
|
121
142
|
|
|
143
|
+
/** The messages of a check: a locale, or a function that renders every issue. */
|
|
122
144
|
export type Messages = Locale | ((issue: ValidationIssue) => string);
|
|
123
145
|
|
|
146
|
+
/** The options of `check` and `checkAsync`, and of the check inside `parse`. */
|
|
124
147
|
export interface CheckOptions {
|
|
125
|
-
|
|
126
|
-
|
|
148
|
+
/**
|
|
149
|
+
* The label the error lines start with: the schema name by default, '' for
|
|
150
|
+
* none. Issue paths are relative to the value and do not include it.
|
|
151
|
+
*/
|
|
127
152
|
root?: string;
|
|
128
|
-
|
|
153
|
+
/** Stop collecting after this many issues (at least 1). */
|
|
129
154
|
maxErrors?: number;
|
|
130
|
-
|
|
131
|
-
|
|
155
|
+
/**
|
|
156
|
+
* How deep a value may nest through embedded references (an integer of at
|
|
157
|
+
* least 1, 128 by default): a record deeper than that is a `depth` issue.
|
|
158
|
+
*/
|
|
132
159
|
maxDepth?: number;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
160
|
+
/**
|
|
161
|
+
* What to do with keys the schema does not have: report them as one
|
|
162
|
+
* `unexpected` issue per struct, or ignore them. The default is the
|
|
163
|
+
* schema's own `unknown` metadata, 'reject' unless it says otherwise.
|
|
164
|
+
*/
|
|
136
165
|
unknown?: 'reject' | 'ignore';
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
166
|
+
/**
|
|
167
|
+
* How a reference is checked: as its target's kind says ('kind', the
|
|
168
|
+
* default: a stored kind as an id, a memory kind as the record), every
|
|
169
|
+
* reference as the record ('embed') or every reference as an id ('id').
|
|
170
|
+
*/
|
|
140
171
|
references?: 'kind' | 'embed' | 'id';
|
|
141
|
-
|
|
172
|
+
/** The locale of the messages, or a function that renders every message. */
|
|
142
173
|
messages?: Messages;
|
|
143
174
|
}
|
|
144
175
|
|
|
145
|
-
|
|
146
|
-
|
|
176
|
+
/**
|
|
177
|
+
* The options of parse: those of check, and `strip` for the keys the schema
|
|
178
|
+
* does not have, which the output then leaves out.
|
|
179
|
+
*/
|
|
147
180
|
export interface ParseOptions extends Omit<CheckOptions, 'unknown'> {
|
|
148
181
|
unknown?: 'reject' | 'ignore' | 'strip';
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Whether parse converts the input to the types of the fields before the
|
|
184
|
+
* check (`'42'` to a number, an ISO string to a Date): the schema's
|
|
185
|
+
* `coerce` metadata unless the call says otherwise, a field's own
|
|
186
|
+
* `coerce` key for its subtree. The converters are those `register()` of
|
|
187
|
+
* the `@alexify/metaschema/coerce` subpath installed; `check` never
|
|
188
|
+
* converts.
|
|
189
|
+
*/
|
|
155
190
|
coerce?: boolean;
|
|
156
191
|
}
|
|
157
192
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
193
|
+
/**
|
|
194
|
+
* The options of `coerce(schema, value)` of the `@alexify/metaschema/coerce`
|
|
195
|
+
* subpath: how references are followed and how deep, and an array given the
|
|
196
|
+
* path of every key the pass changed (a value converted, a default filled).
|
|
197
|
+
*/
|
|
161
198
|
export interface CoerceOptions {
|
|
162
199
|
references?: CheckOptions['references'];
|
|
163
200
|
maxDepth?: number;
|
|
164
201
|
report?: PropertyKey[][];
|
|
165
202
|
}
|
|
166
203
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
204
|
+
/**
|
|
205
|
+
* A converter of coercion: the value of a field of its type read from what
|
|
206
|
+
* the input gave, or the value itself when it cannot be read (the check then
|
|
207
|
+
* reports it as given). Registered process-wide by `register()` of the
|
|
208
|
+
* `@alexify/metaschema/coerce` subpath, or given to a model for its own type
|
|
209
|
+
* names (`ModelOptions.coercions`).
|
|
210
|
+
*/
|
|
172
211
|
export type Coercion = (value: unknown, field: FieldType) => unknown;
|
|
173
212
|
|
|
174
|
-
|
|
213
|
+
/** What parse returns: the output only for a valid value, the issues always. */
|
|
175
214
|
export type ParseResult<T> =
|
|
176
215
|
| { valid: true; value: T; issues: [] }
|
|
177
216
|
| { valid: false; value: undefined; issues: ValidationIssue[] };
|
|
178
217
|
|
|
179
|
-
|
|
180
|
-
|
|
218
|
+
/**
|
|
219
|
+
* The options of serialize: `check` validates the result with the other
|
|
220
|
+
* options as the options of check.
|
|
221
|
+
*/
|
|
181
222
|
export interface SerializeOptions extends CheckOptions {
|
|
182
223
|
check?: boolean;
|
|
183
224
|
}
|
|
@@ -193,107 +234,146 @@ export interface SerializeChecked<T> {
|
|
|
193
234
|
}
|
|
194
235
|
|
|
195
236
|
export interface ResultOptions {
|
|
196
|
-
|
|
237
|
+
/** The label the error lines start with (the schema name in a check). */
|
|
197
238
|
root?: string;
|
|
198
239
|
messages?: Messages;
|
|
199
240
|
}
|
|
200
241
|
|
|
201
|
-
|
|
202
|
-
|
|
242
|
+
/**
|
|
243
|
+
* Messages by dotted path: `formErrors` for the value itself, `fieldErrors`
|
|
244
|
+
* for its fields.
|
|
245
|
+
*/
|
|
203
246
|
export interface FlatIssues {
|
|
204
247
|
formErrors: string[];
|
|
205
248
|
fieldErrors: Record<string, string[]>;
|
|
206
249
|
}
|
|
207
250
|
|
|
208
|
-
|
|
251
|
+
/** Messages as a tree that follows the value. */
|
|
209
252
|
export interface IssueTree {
|
|
210
253
|
errors: string[];
|
|
211
254
|
properties?: Record<string, IssueTree>;
|
|
212
255
|
items?: IssueTree[];
|
|
213
256
|
}
|
|
214
257
|
|
|
258
|
+
/** What `check` returns: the issues as data and their messages, rendered once at the end. */
|
|
215
259
|
export class ValidationResult {
|
|
260
|
+
/** The issues in the order the check met them; `path` is relative to the value. */
|
|
216
261
|
issues: ValidationIssue[];
|
|
262
|
+
/** True when the check recorded no issue. */
|
|
217
263
|
readonly valid: boolean;
|
|
218
|
-
|
|
264
|
+
/** The messages with their location, rendered on first use. */
|
|
219
265
|
readonly errors: string[];
|
|
266
|
+
/** The lines of `errors` joined by newlines. */
|
|
220
267
|
readonly summary: string;
|
|
221
268
|
constructor(options?: ResultOptions);
|
|
269
|
+
/** Adds what a validator may return as issues (a validator that builds a result of its own). */
|
|
222
270
|
add(error: ValidationReturn, code?: IssueCode): this;
|
|
271
|
+
/** The messages by dotted path, for a form. */
|
|
223
272
|
flatten(): FlatIssues;
|
|
273
|
+
/** The messages as a tree that follows the value. */
|
|
224
274
|
tree(): IssueTree;
|
|
225
|
-
|
|
275
|
+
/** The result as data: what JSON.stringify(result) writes. */
|
|
226
276
|
toJSON(): { valid: boolean; issues: ValidationIssue[] };
|
|
227
|
-
|
|
277
|
+
/** The issues of what a validator may return, under `path`. */
|
|
278
|
+
static issuesOf(
|
|
279
|
+
error: ValidationReturn,
|
|
280
|
+
path?: PropertyKey[],
|
|
281
|
+
code?: IssueCode,
|
|
282
|
+
): ValidationIssue[];
|
|
283
|
+
/**
|
|
284
|
+
* Whether a value is a ValidationResult of any copy of the package (by brand, not instanceof).
|
|
285
|
+
*/
|
|
228
286
|
static isInstance(error: unknown): boolean;
|
|
229
287
|
}
|
|
230
288
|
|
|
231
|
-
|
|
289
|
+
/** The state of one check, threaded through the `check` of every field. */
|
|
232
290
|
export interface CheckContext {
|
|
233
291
|
issues: ValidationIssue[];
|
|
234
292
|
count: number;
|
|
235
293
|
limit: number;
|
|
236
|
-
|
|
237
|
-
|
|
294
|
+
/**
|
|
295
|
+
* How many embedded records the check is inside, and how many it may be
|
|
296
|
+
* before a record is a `depth` issue (128 by default).
|
|
297
|
+
*/
|
|
238
298
|
depth: number;
|
|
239
299
|
maxDepth: number;
|
|
240
300
|
path: PropertyKey[];
|
|
241
301
|
seen: Set<object> | null;
|
|
242
|
-
|
|
243
|
-
|
|
302
|
+
/**
|
|
303
|
+
* The value the check started from, when the schema can reach it again
|
|
304
|
+
* through a reference; null otherwise.
|
|
305
|
+
*/
|
|
244
306
|
source: unknown;
|
|
245
|
-
|
|
307
|
+
/** 'strip' in a parse, where the check behind it treats it as 'ignore'. */
|
|
246
308
|
unknown: 'reject' | 'ignore' | 'strip';
|
|
247
309
|
references: 'kind' | 'embed' | 'id';
|
|
248
310
|
root: string;
|
|
249
311
|
messages: Messages;
|
|
250
|
-
|
|
251
|
-
|
|
312
|
+
/**
|
|
313
|
+
* The validators that returned a promise, settled after the walk by
|
|
314
|
+
* checkAsync and parseAsync; null in a synchronous check.
|
|
315
|
+
*/
|
|
252
316
|
async: unknown[] | null;
|
|
253
|
-
|
|
317
|
+
/** The context parse tries the branches of a plain union in; null in a check. */
|
|
254
318
|
scratch: CheckContext | null;
|
|
255
|
-
|
|
256
|
-
|
|
319
|
+
/**
|
|
320
|
+
* Whether the check read a field from one of its aliases: the pass after it
|
|
321
|
+
* renames only when it did.
|
|
322
|
+
*/
|
|
257
323
|
renamed: boolean;
|
|
258
324
|
}
|
|
259
325
|
|
|
260
|
-
|
|
261
|
-
|
|
326
|
+
/**
|
|
327
|
+
* A validator may return a promise of its result to checkAsync and parseAsync;
|
|
328
|
+
* to check and parse a promise is a SchemaUsageError with the code ALX_ERR_ASYNC_VALIDATOR.
|
|
329
|
+
*/
|
|
262
330
|
export type Validator = (value: any, path: string) => ValidationReturn | Promise<ValidationReturn>;
|
|
263
331
|
|
|
332
|
+
/** A calculated field: a function of the struct, kept as it is and never validated. */
|
|
264
333
|
export type CalculatedField = (value: any) => unknown;
|
|
265
334
|
|
|
266
|
-
|
|
267
|
-
|
|
335
|
+
/**
|
|
336
|
+
* A field of a struct: an instance of the type's class. Keys of the
|
|
337
|
+
* definition the type does not use (`unique`, `default`, ...) are kept on it.
|
|
338
|
+
*/
|
|
268
339
|
export interface FieldType {
|
|
269
340
|
readonly type: string;
|
|
270
341
|
required: boolean;
|
|
271
|
-
|
|
342
|
+
/** The value may be null; the key is still required unless `required` is false. */
|
|
272
343
|
nullable?: boolean;
|
|
273
|
-
|
|
274
|
-
|
|
344
|
+
/**
|
|
345
|
+
* Whether parse converts the input of this field and what is under it,
|
|
346
|
+
* whatever the policy of the call or the schema.
|
|
347
|
+
*/
|
|
275
348
|
coerce?: boolean;
|
|
276
|
-
|
|
277
|
-
|
|
349
|
+
/**
|
|
350
|
+
* On a reference: check and render it as the record (true) or its id
|
|
351
|
+
* (false) whatever the kind of its target.
|
|
352
|
+
*/
|
|
278
353
|
embed?: boolean;
|
|
279
354
|
validate?: Validator;
|
|
280
|
-
|
|
281
|
-
|
|
355
|
+
/**
|
|
356
|
+
* The decoder parse runs on the validated value, and the serializer
|
|
357
|
+
* serialize runs on the value of the program, when the definition gave them.
|
|
358
|
+
*/
|
|
282
359
|
parse?: (value: any) => unknown;
|
|
283
360
|
serialize?: (value: any) => unknown;
|
|
284
|
-
|
|
361
|
+
/** What parse fills a missing key with: a value, or a function called for one. */
|
|
285
362
|
default?: unknown;
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
363
|
+
/**
|
|
364
|
+
* The keys the field is also read from, in a struct: check accepts a value
|
|
365
|
+
* under an alias in place of the field's key, parse renames it. The keys
|
|
366
|
+
* of the value the schema accepts (`Infer`) and the generated documents
|
|
367
|
+
* name the field's own key only.
|
|
368
|
+
*/
|
|
290
369
|
aliases?: string[];
|
|
291
|
-
|
|
370
|
+
/** Records the problems of a value into the context of the current check. */
|
|
292
371
|
check(value: unknown, context: CheckContext): void;
|
|
293
372
|
toJSON(): object;
|
|
294
373
|
[key: string]: unknown;
|
|
295
374
|
}
|
|
296
375
|
|
|
376
|
+
/** The fields of a struct by key. */
|
|
297
377
|
export type Fields = Record<string, FieldType | CalculatedField>;
|
|
298
378
|
|
|
299
379
|
export interface TypeConstructor {
|
|
@@ -305,59 +385,75 @@ export interface TypeConstructor {
|
|
|
305
385
|
|
|
306
386
|
export type TypeTable = Record<string, TypeConstructor>;
|
|
307
387
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
388
|
+
/**
|
|
389
|
+
* What `sample` of the `@alexify/metaschema/model` subpath draws for a
|
|
390
|
+
* custom type: the value `metadata.sample` holds, or what the function it
|
|
391
|
+
* holds returns for the field and the seeded generator of the draw.
|
|
392
|
+
*/
|
|
311
393
|
export type SampleHook = (field: FieldType, random: () => number) => unknown;
|
|
312
394
|
|
|
313
|
-
|
|
314
|
-
|
|
395
|
+
/**
|
|
396
|
+
* The metadata of a type: anything, read by the programs built on the
|
|
397
|
+
* model; `sample` is the one key the package reads (see `SampleHook`).
|
|
398
|
+
*/
|
|
315
399
|
export interface TypeMetadata {
|
|
316
400
|
sample?: SampleHook | {} | null;
|
|
317
401
|
[key: string]: unknown;
|
|
318
402
|
}
|
|
319
403
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
404
|
+
/**
|
|
405
|
+
* An entry of the table passed to Model: metadata for a built-in, an alias
|
|
406
|
+
* (`js`), or a prototype with its own `construct` and `checkType`. A
|
|
407
|
+
* prototype may bring a `compile` in place of `checkType` (it returns the
|
|
408
|
+
* check, `(value, context, key) => void`): it runs on the first check of a
|
|
409
|
+
* field, not when the schema is built, so it must not be where the
|
|
410
|
+
* definition is checked (`construct` is); what it throws is
|
|
411
|
+
* ALX_ERR_INVALID_CUSTOM_TYPE with the error as the cause.
|
|
412
|
+
*/
|
|
327
413
|
export interface TypeEntry {
|
|
328
414
|
js?: string;
|
|
329
415
|
metadata?: TypeMetadata;
|
|
330
416
|
kind?: 'scalar' | 'struct';
|
|
331
|
-
|
|
417
|
+
/** The rules the type accepts in a field definition (`length`, `min`, `max`). */
|
|
332
418
|
rules?: string[];
|
|
333
419
|
construct?(def: object, preprocessor: object): void;
|
|
334
420
|
checkType?(value: any, path: string): ValidationReturn | Promise<ValidationReturn>;
|
|
335
421
|
[key: string]: unknown;
|
|
336
422
|
}
|
|
337
423
|
|
|
424
|
+
/** The options of `new Model`. */
|
|
338
425
|
export interface ModelOptions {
|
|
339
|
-
|
|
340
|
-
|
|
426
|
+
/**
|
|
427
|
+
* 'shared' (the default) registers the types into the process-wide registry;
|
|
428
|
+
* 'isolated' gives the model its own copy of the built-in types to register into.
|
|
429
|
+
*/
|
|
341
430
|
registry?: 'shared' | 'isolated';
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
431
|
+
/**
|
|
432
|
+
* The converters of coercion for the model's own type names, read before
|
|
433
|
+
* the process-wide table for the fields of its entities: what an isolated
|
|
434
|
+
* registry is to the types.
|
|
435
|
+
*/
|
|
345
436
|
coercions?: Record<string, Coercion>;
|
|
346
437
|
}
|
|
347
438
|
|
|
439
|
+
/** The metadata of a kind key, with the defaults of its kind filled in. */
|
|
348
440
|
export interface KindMetadata {
|
|
349
441
|
kind: Kind;
|
|
350
442
|
scope: Scope;
|
|
351
443
|
store: Store;
|
|
352
444
|
allow: Allow;
|
|
353
445
|
parent?: string;
|
|
354
|
-
|
|
446
|
+
/** The default of `check` for keys the schema does not have. */
|
|
355
447
|
unknown?: 'reject' | 'ignore';
|
|
356
|
-
|
|
448
|
+
/** The default of `parse` (and so of `~standard.validate`) for coercion. */
|
|
357
449
|
coerce?: boolean;
|
|
358
450
|
[key: string]: unknown;
|
|
359
451
|
}
|
|
360
452
|
|
|
453
|
+
/**
|
|
454
|
+
* The functions of a schema itself: `validate` runs once its fields passed, `parse` and
|
|
455
|
+
* `serialize` are its codecs, and `format` is collected and never called.
|
|
456
|
+
*/
|
|
361
457
|
export interface SchemaOptions {
|
|
362
458
|
validate: Validator | null;
|
|
363
459
|
format: ((value: any) => unknown) | null;
|
|
@@ -365,88 +461,154 @@ export interface SchemaOptions {
|
|
|
365
461
|
serialize: ((value: any) => unknown) | null;
|
|
366
462
|
}
|
|
367
463
|
|
|
464
|
+
/** The codes of a SchemaDefinitionError: the definition, not the call, is wrong. */
|
|
368
465
|
export type DefinitionErrorCode =
|
|
369
|
-
| '
|
|
370
|
-
| '
|
|
371
|
-
| '
|
|
372
|
-
| '
|
|
373
|
-
| '
|
|
374
|
-
| '
|
|
375
|
-
| '
|
|
376
|
-
| '
|
|
377
|
-
| '
|
|
378
|
-
| '
|
|
379
|
-
| '
|
|
380
|
-
| '
|
|
381
|
-
| '
|
|
382
|
-
| '
|
|
383
|
-
|
|
384
|
-
|
|
466
|
+
| 'ALX_ERR_INVALID_DEFINITION'
|
|
467
|
+
| 'ALX_ERR_UNKNOWN_TYPE'
|
|
468
|
+
| 'ALX_ERR_MISSING_SCHEMA'
|
|
469
|
+
| 'ALX_ERR_INVALID_TUPLE'
|
|
470
|
+
| 'ALX_ERR_INVALID_UNION'
|
|
471
|
+
| 'ALX_ERR_PROJECTION'
|
|
472
|
+
| 'ALX_ERR_INVALID_CUSTOM_TYPE'
|
|
473
|
+
| 'ALX_ERR_INVALID_ENUM'
|
|
474
|
+
| 'ALX_ERR_INVALID_LENGTH'
|
|
475
|
+
| 'ALX_ERR_INVALID_RULE'
|
|
476
|
+
| 'ALX_ERR_INVALID_REFERENCE'
|
|
477
|
+
| 'ALX_ERR_RESERVED_KEY'
|
|
478
|
+
| 'ALX_ERR_TYPE_REGISTERED'
|
|
479
|
+
| 'ALX_ERR_UNKNOWN_JS_TYPE'
|
|
480
|
+
// The metadata of a definition that is not what it must be (the `unknown`
|
|
481
|
+
// or `coerce` metadata of a schema, the `metadata` of merge and derive); an
|
|
482
|
+
// option of a call is ALX_ERR_INVALID_OPTION of a SchemaUsageError.
|
|
483
|
+
| 'ALX_ERR_INVALID_OPTIONS'
|
|
484
|
+
| 'ALX_ERR_UNREPRESENTABLE'
|
|
385
485
|
// Thrown by `fromJSONSchema` of the generators subpath for a keyword the
|
|
386
486
|
// subset has no form for.
|
|
387
|
-
| '
|
|
487
|
+
| 'ALX_ERR_UNSUPPORTED_SCHEMA'
|
|
388
488
|
// Thrown by `fromJSON` and `diff` of the `@alexify/metaschema/model`
|
|
389
489
|
// subpath for a value that is not a snapshot of a model.
|
|
390
|
-
| '
|
|
490
|
+
| 'ALX_ERR_INVALID_SNAPSHOT'
|
|
391
491
|
// Thrown by `sample` of the same subpath: a field no value satisfies, a
|
|
392
492
|
// pattern it cannot read, a custom type without `metadata.sample`.
|
|
393
|
-
| '
|
|
394
|
-
| '
|
|
395
|
-
| '
|
|
493
|
+
| 'ALX_ERR_UNSATISFIABLE'
|
|
494
|
+
| 'ALX_ERR_SAMPLE_PATTERN'
|
|
495
|
+
| 'ALX_ERR_SAMPLE_TYPE'
|
|
396
496
|
// Thrown by `merge` of the same subpath for what no value can satisfy.
|
|
397
|
-
| '
|
|
497
|
+
| 'ALX_ERR_MERGE_CONFLICT'
|
|
398
498
|
// Thrown by `infer` of the same subpath: a sample nested deeper than
|
|
399
499
|
// `maxDepth`, and a definition that rejects a sample it was inferred from
|
|
400
500
|
// (a bug of the inference, to report).
|
|
401
|
-
| '
|
|
402
|
-
| '
|
|
501
|
+
| 'ALX_ERR_INFER_DEPTH'
|
|
502
|
+
| 'ALX_ERR_INFER_MISMATCH'
|
|
403
503
|
// Thrown by `register` of the `@alexify/metaschema/formats` subpath: an
|
|
404
504
|
// entry that is not a format, and a name registered again with another
|
|
405
505
|
// test.
|
|
406
|
-
| '
|
|
407
|
-
| '
|
|
506
|
+
| 'ALX_ERR_INVALID_FORMAT'
|
|
507
|
+
| 'ALX_ERR_FORMAT_REGISTERED'
|
|
408
508
|
// Thrown by the functions of the `@alexify/metaschema/derive` subpath for
|
|
409
509
|
// a key the schema has no field of, located in the schema.
|
|
410
|
-
| '
|
|
510
|
+
| 'ALX_ERR_UNKNOWN_FIELD'
|
|
411
511
|
// Thrown by `register` of the `@alexify/metaschema/coerce` subpath: an
|
|
412
512
|
// entry that is not a converter of a type, and a name registered again
|
|
413
513
|
// with another function.
|
|
414
|
-
| '
|
|
415
|
-
| '
|
|
514
|
+
| 'ALX_ERR_INVALID_COERCION'
|
|
515
|
+
| 'ALX_ERR_COERCION_REGISTERED'
|
|
416
516
|
// Thrown by `compile` and `register` of the `@alexify/metaschema/compile`
|
|
417
517
|
// subpath with `jit: true` where code generation from strings is not allowed.
|
|
418
|
-
| '
|
|
518
|
+
| 'ALX_ERR_NO_EVAL'
|
|
419
519
|
// The check of a schema could not be generated (the generator's error is the
|
|
420
520
|
// `cause`): thrown with `jit: true`, reported as `error` and to `onFallback`
|
|
421
521
|
// in the default mode.
|
|
422
|
-
| '
|
|
522
|
+
| 'ALX_ERR_JIT_BUILD';
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* The options of `new SchemaDefinitionError(message, options)`: the code
|
|
526
|
+
* (`ALX_ERR_INVALID_DEFINITION` without one), the cause, the place it is at, and the fields some
|
|
527
|
+
* codes carry.
|
|
528
|
+
*/
|
|
529
|
+
export interface DefinitionErrorOptions {
|
|
530
|
+
code?: DefinitionErrorCode;
|
|
531
|
+
cause?: unknown;
|
|
532
|
+
schema?: string;
|
|
533
|
+
field?: string;
|
|
534
|
+
index?: number;
|
|
535
|
+
seed?: number;
|
|
536
|
+
}
|
|
423
537
|
|
|
538
|
+
/**
|
|
539
|
+
* What a broken definition throws when the schema is built, never from a check: a TypeError
|
|
540
|
+
* with a code, located at the schema and the field.
|
|
541
|
+
*/
|
|
424
542
|
export class SchemaDefinitionError extends TypeError {
|
|
425
|
-
code: DefinitionErrorCode;
|
|
543
|
+
readonly code: DefinitionErrorCode;
|
|
426
544
|
schema: string;
|
|
427
545
|
field: string;
|
|
428
|
-
|
|
546
|
+
/** The sample `infer` could not accept (`ALX_ERR_INFER_MISMATCH`). */
|
|
547
|
+
index?: number;
|
|
548
|
+
/** The seed of the draw no value satisfies (`ALX_ERR_UNSATISFIABLE`): the same call fails again. */
|
|
549
|
+
seed?: number;
|
|
550
|
+
/** As Error's: the code and the fields in `options`, which goes to Error whole. */
|
|
551
|
+
constructor(message: string, options?: DefinitionErrorOptions);
|
|
552
|
+
/**
|
|
553
|
+
* Prefixes the location with a schema and the key of the struct that owns the field; a field
|
|
554
|
+
* of '' locates an error of the schema as a whole.
|
|
555
|
+
*/
|
|
429
556
|
locate(schema: string, field: string): this;
|
|
430
557
|
}
|
|
431
558
|
|
|
432
|
-
|
|
433
|
-
// validator, a custom checkType or a decoder returns a promise: the call,
|
|
434
|
-
// not the definition, is wrong, so it is a plain TypeError with `code`.
|
|
435
|
-
// `ERR_INVALID_TARGET` is the same kind of error from a function of a subpath
|
|
436
|
-
// given something other than what it takes (a Schema, a Model, a snapshot, a
|
|
437
|
-
// list of changes).
|
|
559
|
+
/** The codes of a SchemaUsageError: the call, not the definition, is wrong. */
|
|
438
560
|
export type RuntimeErrorCode =
|
|
439
|
-
|
|
440
|
-
|
|
561
|
+
// A validator, a custom checkType or a decoder returned a promise to a
|
|
562
|
+
// synchronous check or parse.
|
|
563
|
+
| 'ALX_ERR_ASYNC_VALIDATOR'
|
|
564
|
+
// A function of a subpath was given something other than what it takes (a
|
|
565
|
+
// Schema, a Model, a snapshot, a list of changes, a sequence of records).
|
|
566
|
+
| 'ALX_ERR_INVALID_TARGET'
|
|
441
567
|
// An option of a call that is not what it must be (`maxErrors`, the
|
|
442
|
-
// `coerce` of a parse, the options of
|
|
443
|
-
//
|
|
444
|
-
|
|
445
|
-
| '
|
|
568
|
+
// `coerce` of a parse, a Model option, the options of compile, register,
|
|
569
|
+
// toJSONSchema, fromJSONSchema, fromJSON, merge, sample and infer, and of
|
|
570
|
+
// the stream, derive, coerce and mongodb subpaths).
|
|
571
|
+
| 'ALX_ERR_INVALID_OPTION'
|
|
572
|
+
// An invalid record a `transform` of the stream subpath was not told to
|
|
573
|
+
// drop; the error carries `index`, `record` and `result`.
|
|
574
|
+
| 'ALX_ERR_INVALID_RECORD'
|
|
446
575
|
// A parse that coerces before `register()` of the `@alexify/metaschema/coerce`
|
|
447
|
-
// subpath ran (there is no converter to run),
|
|
448
|
-
// ObjectIds before `register({ bson })` of `@alexify/metaschema/mongodb
|
|
449
|
-
|
|
576
|
+
// subpath ran (there is no converter to run), a document written with
|
|
577
|
+
// ObjectIds before `register({ bson })` of `@alexify/metaschema/mongodb`,
|
|
578
|
+
// and a schema of `@alexify/metaschema/definition` asked to validate before
|
|
579
|
+
// the core entry was loaded (the error carries `method`).
|
|
580
|
+
| 'ALX_ERR_NOT_REGISTERED'
|
|
581
|
+
// `saveTypes` of the generators subpath in a browser, where there is no
|
|
582
|
+
// file system to write to.
|
|
583
|
+
| 'ALX_ERR_UNSUPPORTED_RUNTIME';
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* What a wrong call throws: a TypeError with a code. An invalid record of a
|
|
587
|
+
* stream carries it, its position and its result; an ALX_ERR_NOT_REGISTERED of a
|
|
588
|
+
* schema of the definition entry names the method that was called.
|
|
589
|
+
*/
|
|
590
|
+
export class SchemaUsageError extends TypeError {
|
|
591
|
+
readonly code: RuntimeErrorCode;
|
|
592
|
+
index?: number;
|
|
593
|
+
record?: unknown;
|
|
594
|
+
result?: ValidationResult;
|
|
595
|
+
method?: string;
|
|
596
|
+
/** As Error's: the code and the fields in `options`, which goes to Error whole. */
|
|
597
|
+
constructor(message: string, options?: UsageErrorOptions);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* The options of `new SchemaUsageError(message, options)`: the code (`ALX_ERR_INVALID_TARGET`
|
|
602
|
+
* without one), the cause, and the fields some codes carry.
|
|
603
|
+
*/
|
|
604
|
+
export interface UsageErrorOptions {
|
|
605
|
+
code?: RuntimeErrorCode;
|
|
606
|
+
cause?: unknown;
|
|
607
|
+
index?: number;
|
|
608
|
+
record?: unknown;
|
|
609
|
+
result?: ValidationResult;
|
|
610
|
+
method?: string;
|
|
611
|
+
}
|
|
450
612
|
|
|
451
613
|
// ---------------------------------------------------------------------------
|
|
452
614
|
// The snapshot: `model.toJSON()`, what JSON.stringify(model) writes, and
|
|
@@ -469,20 +631,24 @@ export interface FunctionSnapshot {
|
|
|
469
631
|
$function: true;
|
|
470
632
|
}
|
|
471
633
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
634
|
+
/**
|
|
635
|
+
* A field of a snapshot: its type and whether it is required first, then
|
|
636
|
+
* the other keys of its definition, sorted. The type decides what else it
|
|
637
|
+
* holds (narrow on `type`): the rules of its type, the element of a
|
|
638
|
+
* collection, the elements of a tuple, the nested struct, the branches of a
|
|
639
|
+
* union, the entity of a reference (its name is the type, capitalized), or
|
|
640
|
+
* the options of a custom type, whose names are those `CustomTypes` declares
|
|
641
|
+
* (as `Infer` reads them). Any field may carry the keys of
|
|
642
|
+
* `FieldSnapshotBase`; a key the definition gave beyond them is kept too.
|
|
643
|
+
*/
|
|
480
644
|
export interface FieldSnapshotBase {
|
|
481
645
|
required: boolean;
|
|
482
646
|
nullable?: boolean;
|
|
483
647
|
coerce?: boolean;
|
|
484
|
-
|
|
485
|
-
|
|
648
|
+
/**
|
|
649
|
+
* A value, `{ $function: true }` for a factory, `{ $bigint }`, `{ $date }`,
|
|
650
|
+
* `{ $bytes }`, `{ $map }`, `{ $set }` for the values that have no JSON form.
|
|
651
|
+
*/
|
|
486
652
|
default?: unknown;
|
|
487
653
|
aliases?: string[];
|
|
488
654
|
deprecated?: boolean | string;
|
|
@@ -503,8 +669,10 @@ export interface DateSnapshot {
|
|
|
503
669
|
$date: string;
|
|
504
670
|
}
|
|
505
671
|
|
|
506
|
-
|
|
507
|
-
|
|
672
|
+
/**
|
|
673
|
+
* Bytes as base64 text, a Map as its entries and a Set as its values, each
|
|
674
|
+
* value of them written as any value of a snapshot is.
|
|
675
|
+
*/
|
|
508
676
|
export interface BytesSnapshot {
|
|
509
677
|
$bytes: string;
|
|
510
678
|
}
|
|
@@ -517,7 +685,7 @@ export interface SetSnapshot {
|
|
|
517
685
|
$set: unknown[];
|
|
518
686
|
}
|
|
519
687
|
|
|
520
|
-
|
|
688
|
+
/** A `length` rule: the bounds it has. */
|
|
521
689
|
export interface LengthSnapshot {
|
|
522
690
|
min?: number;
|
|
523
691
|
max?: number;
|
|
@@ -527,7 +695,7 @@ export interface StringFieldSnapshot extends FieldSnapshotBase {
|
|
|
527
695
|
type: 'string';
|
|
528
696
|
length?: LengthSnapshot;
|
|
529
697
|
unicode?: boolean;
|
|
530
|
-
|
|
698
|
+
/** The source of a pattern of the `u` flag alone, else its source and flags. */
|
|
531
699
|
pattern?: string | { source: string; flags: string };
|
|
532
700
|
format?: string;
|
|
533
701
|
}
|
|
@@ -549,7 +717,7 @@ export interface ScalarFieldSnapshot extends FieldSnapshotBase {
|
|
|
549
717
|
type: 'boolean' | 'date' | 'null' | 'any' | 'unknown' | 'json';
|
|
550
718
|
}
|
|
551
719
|
|
|
552
|
-
|
|
720
|
+
/** Binary data, its `length` the number of bytes. */
|
|
553
721
|
export interface BytesFieldSnapshot extends FieldSnapshotBase {
|
|
554
722
|
type: 'bytes';
|
|
555
723
|
length?: LengthSnapshot;
|
|
@@ -578,7 +746,7 @@ export interface KeyedFieldSnapshot extends FieldSnapshotBase {
|
|
|
578
746
|
length?: LengthSnapshot;
|
|
579
747
|
}
|
|
580
748
|
|
|
581
|
-
|
|
749
|
+
/** An element of a tuple, `name` the one a named element was given. */
|
|
582
750
|
export type TupleElementSnapshot = FieldSnapshot & { name?: string };
|
|
583
751
|
|
|
584
752
|
export interface TupleFieldSnapshot extends FieldSnapshotBase {
|
|
@@ -586,7 +754,7 @@ export interface TupleFieldSnapshot extends FieldSnapshotBase {
|
|
|
586
754
|
value: TupleElementSnapshot[];
|
|
587
755
|
}
|
|
588
756
|
|
|
589
|
-
|
|
757
|
+
/** A nested struct, or a Schema of one type held as a field. */
|
|
590
758
|
export interface StructFieldSnapshot extends FieldSnapshotBase {
|
|
591
759
|
type: 'schema';
|
|
592
760
|
schema: FieldsSnapshot | FieldSnapshot;
|
|
@@ -599,10 +767,34 @@ export interface UnionFieldSnapshot extends FieldSnapshotBase {
|
|
|
599
767
|
}
|
|
600
768
|
|
|
601
769
|
type UpperLetter =
|
|
602
|
-
| 'A'
|
|
603
|
-
| '
|
|
604
|
-
|
|
605
|
-
|
|
770
|
+
| 'A'
|
|
771
|
+
| 'B'
|
|
772
|
+
| 'C'
|
|
773
|
+
| 'D'
|
|
774
|
+
| 'E'
|
|
775
|
+
| 'F'
|
|
776
|
+
| 'G'
|
|
777
|
+
| 'H'
|
|
778
|
+
| 'I'
|
|
779
|
+
| 'J'
|
|
780
|
+
| 'K'
|
|
781
|
+
| 'L'
|
|
782
|
+
| 'M'
|
|
783
|
+
| 'N'
|
|
784
|
+
| 'O'
|
|
785
|
+
| 'P'
|
|
786
|
+
| 'Q'
|
|
787
|
+
| 'R'
|
|
788
|
+
| 'S'
|
|
789
|
+
| 'T'
|
|
790
|
+
| 'U'
|
|
791
|
+
| 'V'
|
|
792
|
+
| 'W'
|
|
793
|
+
| 'X'
|
|
794
|
+
| 'Y'
|
|
795
|
+
| 'Z';
|
|
796
|
+
|
|
797
|
+
/** A reference: the type is the name of the entity, under `one` or `many`. */
|
|
606
798
|
export interface ReferenceFieldSnapshot extends FieldSnapshotBase {
|
|
607
799
|
type: `${UpperLetter}${string}`;
|
|
608
800
|
one?: string;
|
|
@@ -610,10 +802,12 @@ export interface ReferenceFieldSnapshot extends FieldSnapshotBase {
|
|
|
610
802
|
embed?: boolean;
|
|
611
803
|
}
|
|
612
804
|
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
805
|
+
/**
|
|
806
|
+
* A field of a custom type, by the names `CustomTypes` declares: a snapshot
|
|
807
|
+
* of a model whose custom types are not declared there holds fields of type
|
|
808
|
+
* names no member of `FieldSnapshot` has, which an exhaustive `switch` over
|
|
809
|
+
* `type` does not see; give it a `default`.
|
|
810
|
+
*/
|
|
617
811
|
export interface CustomFieldSnapshot extends FieldSnapshotBase {
|
|
618
812
|
type: keyof CustomTypes & string;
|
|
619
813
|
}
|
|
@@ -634,8 +828,10 @@ export type FieldSnapshot =
|
|
|
634
828
|
| ReferenceFieldSnapshot
|
|
635
829
|
| CustomFieldSnapshot;
|
|
636
830
|
|
|
637
|
-
|
|
638
|
-
|
|
831
|
+
/**
|
|
832
|
+
* The fields of a struct in their order: a calculated field, the validate,
|
|
833
|
+
* parse or serialize of the struct are `{ $function: true }`.
|
|
834
|
+
*/
|
|
639
835
|
export type FieldsSnapshot = Record<string, FieldSnapshot | FunctionSnapshot>;
|
|
640
836
|
|
|
641
837
|
export interface EntitySnapshot {
|
|
@@ -643,22 +839,24 @@ export interface EntitySnapshot {
|
|
|
643
839
|
scope: Scope;
|
|
644
840
|
store: Store;
|
|
645
841
|
allow: Allow;
|
|
646
|
-
|
|
842
|
+
/** The parent of a projection, '' otherwise. */
|
|
647
843
|
parent: string;
|
|
648
844
|
unknown: 'reject' | 'ignore';
|
|
649
|
-
|
|
845
|
+
/** The other keys of the kind metadata, as given. */
|
|
650
846
|
custom: Record<string, unknown>;
|
|
651
|
-
|
|
847
|
+
/** A struct's fields, with the schema-level functions among them, or */
|
|
652
848
|
fields?: FieldsSnapshot;
|
|
653
|
-
|
|
849
|
+
/** the type of a schema of one type. */
|
|
654
850
|
type?: FieldSnapshot;
|
|
655
|
-
|
|
851
|
+
/** The index definitions (`index`, `primary`, `unique` lists), as given. */
|
|
656
852
|
indexes: Record<string, object>;
|
|
657
853
|
relations: Relation[];
|
|
658
854
|
}
|
|
659
855
|
|
|
660
|
-
|
|
661
|
-
|
|
856
|
+
/**
|
|
857
|
+
* A type the entities use: an alias of a built-in (`js`), a type with a
|
|
858
|
+
* check of its own (`check: true`, its kind), or a built-in with metadata.
|
|
859
|
+
*/
|
|
662
860
|
export interface TypeSnapshot {
|
|
663
861
|
js?: string;
|
|
664
862
|
kind?: 'scalar' | 'struct';
|
|
@@ -668,38 +866,48 @@ export interface TypeSnapshot {
|
|
|
668
866
|
|
|
669
867
|
export interface ModelSnapshot {
|
|
670
868
|
format: 1;
|
|
671
|
-
|
|
672
|
-
|
|
869
|
+
/**
|
|
870
|
+
* The entity a snapshot of one schema is about (`schema.toSnapshot()`);
|
|
871
|
+
* a model's snapshot has none.
|
|
872
|
+
*/
|
|
673
873
|
root?: string;
|
|
674
874
|
database: Record<string, unknown> | null;
|
|
675
875
|
types: Record<string, TypeSnapshot>;
|
|
676
876
|
entities: Record<string, EntitySnapshot>;
|
|
677
877
|
}
|
|
678
878
|
|
|
679
|
-
|
|
680
|
-
|
|
879
|
+
/**
|
|
880
|
+
* `schema.toSnapshot()`: the schema (under its name, '' when anonymous)
|
|
881
|
+
* and every entity it reaches through its references, in dependency order.
|
|
882
|
+
*/
|
|
681
883
|
export interface SchemaSnapshot extends ModelSnapshot {
|
|
682
884
|
root: string;
|
|
683
885
|
database: null;
|
|
684
886
|
}
|
|
685
887
|
|
|
686
|
-
|
|
888
|
+
/** The options of `fromJSON` of the `@alexify/metaschema/model` subpath. */
|
|
687
889
|
export interface RestoreOptions {
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
890
|
+
/**
|
|
891
|
+
* Entries of the table Model takes, over the types of the snapshot: the
|
|
892
|
+
* real custom types, where the restored model should validate as the
|
|
893
|
+
* original did.
|
|
894
|
+
*/
|
|
691
895
|
types?: Record<string, TypeEntry>;
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
896
|
+
/**
|
|
897
|
+
* 'isolated' (the default) gives the restored model its own registry, so
|
|
898
|
+
* the placeholders of its custom types never reach the shared one; a
|
|
899
|
+
* 'shared' registry takes no placeholder type (`ALX_ERR_INVALID_SNAPSHOT`
|
|
900
|
+
* unless `types` brings the real one).
|
|
901
|
+
*/
|
|
696
902
|
registry?: 'shared' | 'isolated';
|
|
697
903
|
}
|
|
698
904
|
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
905
|
+
/**
|
|
906
|
+
* A model restored by `fromJSON`: a Model that lists the paths of the
|
|
907
|
+
* placeholders standing in for the functions of the snapshot (`Order.validate`,
|
|
908
|
+
* `Order.total` for a calculated field, `Order.address.parse`, `Order.when.default`,
|
|
909
|
+
* `types.hex.checkType`), invisibly to `toJSON`.
|
|
910
|
+
*/
|
|
703
911
|
export interface RestoredModel extends Model {
|
|
704
912
|
readonly placeholders: string[];
|
|
705
913
|
}
|
|
@@ -713,13 +921,15 @@ export type ChangeKind = 'entity' | 'field' | 'index' | 'relation' | 'type';
|
|
|
713
921
|
|
|
714
922
|
export type ChangeOp = 'added' | 'removed' | 'changed' | 'renamed';
|
|
715
923
|
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
924
|
+
/**
|
|
925
|
+
* One change between two versions of a model: what changed (`kind`), how
|
|
926
|
+
* (`op`), in which entity ('' for a type), under which name (the entity, the
|
|
927
|
+
* field, the index, the target of a relation, the type), what it was
|
|
928
|
+
* (`from`) and is (`to`), and the detail: the keys that differ for a
|
|
929
|
+
* `changed` entity, field, index or type; the old key (`renamed`) and the
|
|
930
|
+
* keys that differ beyond `aliases` for a field renamed through an alias;
|
|
931
|
+
* `deprecated: true` for a removed field that was marked so.
|
|
932
|
+
*/
|
|
723
933
|
export interface SchemaChange {
|
|
724
934
|
kind: ChangeKind;
|
|
725
935
|
op: ChangeOp;
|
|
@@ -730,128 +940,172 @@ export interface SchemaChange {
|
|
|
730
940
|
detail?: { keys?: string[]; renamed?: string; deprecated?: true };
|
|
731
941
|
}
|
|
732
942
|
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
943
|
+
/**
|
|
944
|
+
* 'backward': a reader with the new model reads data written with the old
|
|
945
|
+
* one; 'forward': a reader with the old model reads data written with the
|
|
946
|
+
* new one; 'full': both; 'none': neither.
|
|
947
|
+
*/
|
|
736
948
|
export type Compatibility = 'full' | 'backward' | 'forward' | 'none';
|
|
737
949
|
|
|
738
|
-
|
|
950
|
+
/** The options of `merge` of the `@alexify/metaschema/model` subpath. */
|
|
739
951
|
export interface MergeOptions {
|
|
740
|
-
|
|
952
|
+
/** The name of the merged schema: the first schema's by default. */
|
|
741
953
|
name?: string;
|
|
742
|
-
|
|
743
|
-
|
|
954
|
+
/**
|
|
955
|
+
* The kind metadata of the merged schema, over the first schema's: `kind`,
|
|
956
|
+
* `scope`, `store`, `allow`, `unknown` and any other key.
|
|
957
|
+
*/
|
|
744
958
|
metadata?: Partial<KindMetadata>;
|
|
745
959
|
}
|
|
746
960
|
|
|
747
|
-
|
|
961
|
+
/** The options of every function of the `@alexify/metaschema/derive` subpath. */
|
|
748
962
|
export interface DeriveOptions {
|
|
749
|
-
|
|
963
|
+
/** The name of the derived schema: the source's by default. */
|
|
750
964
|
name?: string;
|
|
751
|
-
|
|
752
|
-
|
|
965
|
+
/**
|
|
966
|
+
* The kind metadata of the derived schema, over the source's (a stored
|
|
967
|
+
* kind or a projection is a struct, keeping its `unknown`).
|
|
968
|
+
*/
|
|
753
969
|
metadata?: Partial<KindMetadata>;
|
|
754
970
|
}
|
|
755
971
|
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
972
|
+
/**
|
|
973
|
+
* The options of `partial`: the fields to make optional (every one by
|
|
974
|
+
* default), `deep` into nested structs, and `defaults` to keep the defaults
|
|
975
|
+
* a patch would otherwise fill.
|
|
976
|
+
*/
|
|
759
977
|
export interface PartialOptions<K extends string = string> extends DeriveOptions {
|
|
760
978
|
keys?: readonly K[];
|
|
761
979
|
deep?: boolean;
|
|
762
980
|
defaults?: boolean;
|
|
763
981
|
}
|
|
764
982
|
|
|
765
|
-
|
|
766
|
-
|
|
983
|
+
/**
|
|
984
|
+
* The options of `required`: the fields to make required (every one by
|
|
985
|
+
* default) and `deep` into nested structs.
|
|
986
|
+
*/
|
|
767
987
|
export interface RequiredOptions<K extends string = string> extends DeriveOptions {
|
|
768
988
|
keys?: readonly K[];
|
|
769
989
|
deep?: boolean;
|
|
770
990
|
}
|
|
771
991
|
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
992
|
+
/**
|
|
993
|
+
* The options of `infer` of the `@alexify/metaschema/model` subpath (the
|
|
994
|
+
* name sits beside the type `Infer<D>`: the function reads samples, the type
|
|
995
|
+
* reads a definition).
|
|
996
|
+
*/
|
|
775
997
|
export interface InferOptions {
|
|
776
|
-
|
|
777
|
-
|
|
998
|
+
/**
|
|
999
|
+
* What comes back: the definition ('definition', the default), or a Schema
|
|
1000
|
+
* built from it ('schema').
|
|
1001
|
+
*/
|
|
778
1002
|
as?: 'definition' | 'schema';
|
|
779
|
-
|
|
1003
|
+
/** The name of the Schema, and of the error locations (''). */
|
|
780
1004
|
name?: string;
|
|
781
|
-
|
|
782
|
-
|
|
1005
|
+
/**
|
|
1006
|
+
* The kind of a root that is a struct ('struct'); a stored kind adds its
|
|
1007
|
+
* id field, which wins over an inferred field of that name.
|
|
1008
|
+
*/
|
|
783
1009
|
kind?: string;
|
|
784
|
-
|
|
785
|
-
|
|
1010
|
+
/**
|
|
1011
|
+
* The most distinct strings a place may hold to be an enum (8), each
|
|
1012
|
+
* value seen twice on average over at least four samples; 0 or false never.
|
|
1013
|
+
*/
|
|
786
1014
|
enum?: number | false;
|
|
787
|
-
|
|
1015
|
+
/** Whether whole numbers are `integer` (true) rather than `number`. */
|
|
788
1016
|
integer?: boolean;
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1017
|
+
/**
|
|
1018
|
+
* The distinct keys past which records of a place read as a dictionary
|
|
1019
|
+
* (16) when every key is missing from some record and the values are of
|
|
1020
|
+
* one kind; keys that are not names are a dictionary from one sample on;
|
|
1021
|
+
* false keeps every record a struct.
|
|
1022
|
+
*/
|
|
793
1023
|
map?: number | false;
|
|
794
|
-
|
|
1024
|
+
/** How deep a sample may nest (128, the most); past it `ALX_ERR_INFER_DEPTH`. */
|
|
795
1025
|
maxDepth?: number;
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
1026
|
+
/**
|
|
1027
|
+
* Whether a place whose every string passes a registered format is that
|
|
1028
|
+
* format (true); the formats subpath registers them, in the order `infer`
|
|
1029
|
+
* tries them, and nothing is registered until its `register()` ran.
|
|
1030
|
+
*/
|
|
799
1031
|
formats?: boolean;
|
|
800
1032
|
}
|
|
801
1033
|
|
|
802
|
-
|
|
1034
|
+
/** The options of `sample` of the `@alexify/metaschema/model` subpath. */
|
|
803
1035
|
export interface SampleOptions {
|
|
804
|
-
|
|
1036
|
+
/** The seed of the generator (1): the same seed gives the same value. */
|
|
805
1037
|
seed?: number;
|
|
806
|
-
|
|
1038
|
+
/** The letters of free strings: 'en' (the default) or 'uk'. */
|
|
807
1039
|
locale?: 'en' | 'uk';
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
1040
|
+
/**
|
|
1041
|
+
* How a reference is held, as in check: an id for a stored kind and the
|
|
1042
|
+
* record for a memory kind ('kind', the default), always the record
|
|
1043
|
+
* ('embed'), always an id ('id').
|
|
1044
|
+
*/
|
|
811
1045
|
references?: 'kind' | 'embed' | 'id';
|
|
812
|
-
|
|
813
|
-
|
|
1046
|
+
/**
|
|
1047
|
+
* How deep embedded records nest (3): an optional reference deeper than
|
|
1048
|
+
* that is left out, a required one is an error.
|
|
1049
|
+
*/
|
|
814
1050
|
maxDepth?: number;
|
|
815
1051
|
}
|
|
816
1052
|
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
1053
|
+
/**
|
|
1054
|
+
* The options of `checkEach` and `checkEachAsync` of the
|
|
1055
|
+
* `@alexify/metaschema/stream` subpath: those of `check`, and how many invalid
|
|
1056
|
+
* records end the iteration (no limit by default).
|
|
1057
|
+
*/
|
|
820
1058
|
export interface EachOptions extends CheckOptions {
|
|
821
1059
|
maxInvalid?: number;
|
|
822
1060
|
}
|
|
823
1061
|
|
|
824
|
-
|
|
825
|
-
|
|
1062
|
+
/**
|
|
1063
|
+
* The options of `parseEach` and `parseEachAsync`: those of `parse`, and
|
|
1064
|
+
* `maxInvalid`.
|
|
1065
|
+
*/
|
|
826
1066
|
export interface ParseEachOptions extends ParseOptions {
|
|
827
1067
|
maxInvalid?: number;
|
|
828
1068
|
}
|
|
829
1069
|
|
|
830
|
-
|
|
1070
|
+
/** The option of the async pair: how many records are in flight at once (1). */
|
|
831
1071
|
export interface EachAsyncOptions {
|
|
832
1072
|
concurrency?: number;
|
|
833
1073
|
}
|
|
834
1074
|
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
1075
|
+
/**
|
|
1076
|
+
* The options of `transform`: those of `parse` (or of `check`, with
|
|
1077
|
+
* `mode: 'check'`), `maxInvalid`, and what the stream does with an invalid
|
|
1078
|
+
* record.
|
|
1079
|
+
*/
|
|
838
1080
|
export interface TransformOptions extends ParseOptions {
|
|
839
|
-
|
|
840
|
-
|
|
1081
|
+
/**
|
|
1082
|
+
* What goes on down the stream: the parsed value ('parse', the default) or
|
|
1083
|
+
* the record as it is ('check').
|
|
1084
|
+
*/
|
|
841
1085
|
mode?: 'parse' | 'check';
|
|
842
|
-
|
|
1086
|
+
/** Whether the stream runs `parseAsync`/`checkAsync` (false). */
|
|
843
1087
|
async?: boolean;
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
1088
|
+
/**
|
|
1089
|
+
* Told of every invalid record, which is then dropped; without it the
|
|
1090
|
+
* stream ends with the error at the first one.
|
|
1091
|
+
*/
|
|
1092
|
+
onInvalid?: (
|
|
1093
|
+
record: unknown,
|
|
1094
|
+
result: ValidationResult | ParseResult<unknown>,
|
|
1095
|
+
index: number,
|
|
1096
|
+
) => void;
|
|
1097
|
+
/**
|
|
1098
|
+
* After that many invalid records the stream ends with the error even with
|
|
1099
|
+
* `onInvalid` (no limit by default).
|
|
1100
|
+
*/
|
|
849
1101
|
maxInvalid?: number;
|
|
850
1102
|
}
|
|
851
1103
|
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
1104
|
+
/**
|
|
1105
|
+
* What `checkEach` and its kin yield for a record: its position in the
|
|
1106
|
+
* sequence, the record as the source gave it, and the result of `check` or
|
|
1107
|
+
* `parse`.
|
|
1108
|
+
*/
|
|
855
1109
|
export interface RecordEntry<T, R> {
|
|
856
1110
|
index: number;
|
|
857
1111
|
record: T;
|
|
@@ -865,6 +1119,10 @@ export const SCOPE: Array<string>;
|
|
|
865
1119
|
export const STORE: Array<string>;
|
|
866
1120
|
export const ALLOW: Array<string>;
|
|
867
1121
|
|
|
1122
|
+
/**
|
|
1123
|
+
* The metadata of a kind key with the defaults of its kind, and the fields the kind adds (the
|
|
1124
|
+
* id of a stored kind, the fields a projection copies).
|
|
1125
|
+
*/
|
|
868
1126
|
export function getKindMetadata(
|
|
869
1127
|
kind: Kind,
|
|
870
1128
|
meta?: Record<string, unknown>,
|
|
@@ -878,86 +1136,116 @@ export function getKindMetadata(
|
|
|
878
1136
|
// types are here so that the core and the subpath share them.
|
|
879
1137
|
// ---------------------------------------------------------------------------
|
|
880
1138
|
|
|
881
|
-
|
|
1139
|
+
/** The options of `toTypeScript`, `toInterface` and `saveTypes`. */
|
|
882
1140
|
export interface InterfaceOptions {
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
1141
|
+
/**
|
|
1142
|
+
* Give a nested struct and an enum a type of their own, named after the
|
|
1143
|
+
* entity and the field (`interface AccountFullName`, `type IdentifierStorage`),
|
|
1144
|
+
* rendered before the interface that uses them.
|
|
1145
|
+
*/
|
|
886
1146
|
named?: boolean;
|
|
887
1147
|
}
|
|
888
1148
|
|
|
889
|
-
|
|
890
|
-
|
|
1149
|
+
/**
|
|
1150
|
+
* The JSON Schema export renders the rules of `check` as a document of a
|
|
1151
|
+
* target dialect; see the JSON Schema guide for the mapping.
|
|
1152
|
+
*/
|
|
891
1153
|
export type JSONSchemaTarget = 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | 'mongodb';
|
|
892
1154
|
|
|
893
1155
|
export type JSONSchema = Record<string, unknown>;
|
|
894
1156
|
|
|
895
1157
|
export interface JSONSchemaOptions {
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
1158
|
+
/**
|
|
1159
|
+
* The dialect of the document: JSON Schema draft 2020-12 (the default) or
|
|
1160
|
+
* draft-07, an OpenAPI 3.0 schema object, or a MongoDB `$jsonSchema`
|
|
1161
|
+
* (`bsonType`, no `$ref`, references inline).
|
|
1162
|
+
*/
|
|
899
1163
|
target?: JSONSchemaTarget;
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
1164
|
+
/**
|
|
1165
|
+
* 'strict' renders the dialect of LLM structured outputs (OpenAI, Anthropic):
|
|
1166
|
+
* an object at the root, every property listed as required (an optional one
|
|
1167
|
+
* accepts null), `additionalProperties: false` on every object, and the
|
|
1168
|
+
* structure only (no `length`, `pattern`, `min` or `max`). For the
|
|
1169
|
+
* draft-2020-12 and draft-07 targets.
|
|
1170
|
+
*/
|
|
905
1171
|
profile?: 'strict';
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
1172
|
+
/**
|
|
1173
|
+
* Which side of the wire the document describes: 'input' (the default) is
|
|
1174
|
+
* the JSON a value is parsed from (a `date` is a date-time string), 'output'
|
|
1175
|
+
* the value itself (a `date`, `set` or `map` has no JSON form).
|
|
1176
|
+
*/
|
|
909
1177
|
io?: 'input' | 'output';
|
|
910
|
-
|
|
911
|
-
|
|
1178
|
+
/**
|
|
1179
|
+
* What to do with a type that has no form in the target: throw
|
|
1180
|
+
* `ALX_ERR_UNREPRESENTABLE` (the default), or render it as `{}`.
|
|
1181
|
+
*/
|
|
912
1182
|
unrepresentable?: 'throw' | 'any';
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
1183
|
+
/**
|
|
1184
|
+
* How a reference renders, as in `check`: by the kind of its target ('kind',
|
|
1185
|
+
* the default: a stored kind as an id, a memory kind as a `$ref` to its
|
|
1186
|
+
* definition), every reference as the record ('embed') or as an id ('id').
|
|
1187
|
+
*/
|
|
916
1188
|
references?: 'kind' | 'embed' | 'id';
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
1189
|
+
/**
|
|
1190
|
+
* The mongodb target only: the ids of stored records as the database holds
|
|
1191
|
+
* them ('objectId': `_id` an ObjectId, required at the root, and every
|
|
1192
|
+
* reference an ObjectId, as `toDocument` of `@alexify/metaschema/mongodb`
|
|
1193
|
+
* writes them), or as the program does ('string', the default).
|
|
1194
|
+
*/
|
|
921
1195
|
ids?: 'string' | 'objectId';
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
1196
|
+
/**
|
|
1197
|
+
* The pointer the `$ref` of a definition starts with, and where the
|
|
1198
|
+
* definitions are placed in the document: '#/$defs/' for draft-2020-12,
|
|
1199
|
+
* '#/definitions/' for draft-07, '#/components/schemas/' for openapi-3.0.
|
|
1200
|
+
*/
|
|
925
1201
|
definitions?: string;
|
|
926
1202
|
}
|
|
927
1203
|
|
|
928
1204
|
export interface ModelJSONSchemaOptions extends JSONSchemaOptions {
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
1205
|
+
/**
|
|
1206
|
+
* The entity at the root of the document, with the definitions it needs;
|
|
1207
|
+
* without it the document holds every entity as a definition, in
|
|
1208
|
+
* dependency order (one document per entity for the mongodb target).
|
|
1209
|
+
*/
|
|
932
1210
|
root?: string;
|
|
933
1211
|
}
|
|
934
1212
|
|
|
935
|
-
|
|
1213
|
+
/** The options of `fromJSONSchema` of the generators subpath. */
|
|
936
1214
|
export interface FromJSONSchemaOptions {
|
|
937
|
-
|
|
938
|
-
|
|
1215
|
+
/**
|
|
1216
|
+
* The name of the schema: what `$ref: '#'` refers to, and the name of the
|
|
1217
|
+
* Schema built with `as: 'schema'`.
|
|
1218
|
+
*/
|
|
939
1219
|
name?: string;
|
|
940
|
-
|
|
1220
|
+
/** What comes back: the definition (the default), or a Schema built from it. */
|
|
941
1221
|
as?: 'definition' | 'schema';
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
1222
|
+
/**
|
|
1223
|
+
* A `$ref` into the definitions of the document: the definition in place
|
|
1224
|
+
* ('inline', the default; a cycle has no form), or a reference to an
|
|
1225
|
+
* entity of that name ('name'), to resolve through a Model.
|
|
1226
|
+
*/
|
|
945
1227
|
references?: 'inline' | 'name';
|
|
946
|
-
|
|
947
|
-
|
|
1228
|
+
/**
|
|
1229
|
+
* A keyword the subset has no form for: `ALX_ERR_UNSUPPORTED_SCHEMA` with its
|
|
1230
|
+
* place in the document ('throw', the default), or dropped ('ignore').
|
|
1231
|
+
*/
|
|
948
1232
|
unsupported?: 'throw' | 'ignore';
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
1233
|
+
/**
|
|
1234
|
+
* Told of every drop of `unsupported: 'ignore'`: what was dropped, as the
|
|
1235
|
+
* error of the throw mode names it (`The keyword "multipleOf"`), and its
|
|
1236
|
+
* place in the document as a JSON pointer. Not called in the throw mode,
|
|
1237
|
+
* where the error carries both.
|
|
1238
|
+
*/
|
|
953
1239
|
onUnsupported?: (reason: string, pointer: string) => void;
|
|
954
1240
|
}
|
|
955
1241
|
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
1242
|
+
/**
|
|
1243
|
+
* Standard JSON Schema (https://standardschema.dev/json-schema): the converter
|
|
1244
|
+
* `schema['~standard'].jsonSchema`, whose `input` and `output` take the
|
|
1245
|
+
* target and the other options of `toJSONSchema` as `libraryOptions`. The
|
|
1246
|
+
* mongodb target is not a JSON Schema and is refused here. Installed by
|
|
1247
|
+
* `register()` of the generators subpath.
|
|
1248
|
+
*/
|
|
961
1249
|
export interface StandardJSONSchemaOptions {
|
|
962
1250
|
readonly target: 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | ({} & string);
|
|
963
1251
|
readonly libraryOptions?: Omit<JSONSchemaOptions, 'target' | 'io'> | undefined;
|
|
@@ -986,9 +1274,11 @@ export interface StandardConverter {
|
|
|
986
1274
|
// which are fields.
|
|
987
1275
|
// ---------------------------------------------------------------------------
|
|
988
1276
|
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1277
|
+
/**
|
|
1278
|
+
* The value types of custom types, by type name, for module augmentation:
|
|
1279
|
+
* declare module '@alexify/metaschema' { interface CustomTypes { datetime: string } }
|
|
1280
|
+
* A type name that is neither built in nor declared here infers as `unknown`.
|
|
1281
|
+
*/
|
|
992
1282
|
export interface CustomTypes {}
|
|
993
1283
|
|
|
994
1284
|
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
@@ -1340,7 +1630,11 @@ type IsOptionalDef<X> = X extends `?${string}`
|
|
|
1340
1630
|
type IsOptionalKey<K, X> = K extends `${string}?` ? true : IsOptionalDef<X>;
|
|
1341
1631
|
|
|
1342
1632
|
// Whether parse fills the key when it is missing.
|
|
1343
|
-
type Defaulted<X> = X extends { default: infer V }
|
|
1633
|
+
type Defaulted<X> = X extends { default: infer V }
|
|
1634
|
+
? [V] extends [undefined]
|
|
1635
|
+
? false
|
|
1636
|
+
: true
|
|
1637
|
+
: false;
|
|
1344
1638
|
|
|
1345
1639
|
type FieldKeys<D> = { [K in keyof D]: IsField<D[K]> extends true ? K : never }[keyof D];
|
|
1346
1640
|
|
|
@@ -1432,27 +1726,33 @@ type Value<D, E, M extends Mode> =
|
|
|
1432
1726
|
? ObjectDef<D, E, M>
|
|
1433
1727
|
: unknown;
|
|
1434
1728
|
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1729
|
+
/**
|
|
1730
|
+
* The type of a value `check` and `parse` accept for the definition `D` (a
|
|
1731
|
+
* key with a `default` may be left out, parse fills it). `E` is an optional
|
|
1732
|
+
* map of entity definitions by name (the second argument of `Model` as an
|
|
1733
|
+
* object): with it a reference to a memory kind is the record and a
|
|
1734
|
+
* projection has its parent's fields; without it a memory reference is
|
|
1735
|
+
* `unknown` and a stored one is its id. A stored kind's own id field depends
|
|
1736
|
+
* on the entity's name, which a definition does not carry: `InferEntity`
|
|
1737
|
+
* adds it.
|
|
1738
|
+
*/
|
|
1443
1739
|
export type Infer<D, E = {}> = Value<D, E, 'input'>;
|
|
1444
1740
|
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1741
|
+
/**
|
|
1742
|
+
* The type of the value `parse` gives for the definition `D`: `Infer<D, E>`
|
|
1743
|
+
* with the result of every `parse` function (a decoder of a field, the
|
|
1744
|
+
* schema's own) in place of the value it decodes, and every defaulted key
|
|
1745
|
+
* present.
|
|
1746
|
+
*/
|
|
1449
1747
|
export type InferOutput<D, E = {}> = Value<D, E, 'output'>;
|
|
1450
1748
|
|
|
1451
|
-
|
|
1749
|
+
/** The value type of a `Schema<D>` instance: `InferSchema<typeof schema>`. */
|
|
1452
1750
|
export type InferSchema<S> = S extends Schema<infer D> ? Infer<D> : never;
|
|
1453
1751
|
|
|
1454
|
-
|
|
1455
|
-
|
|
1752
|
+
/**
|
|
1753
|
+
* The value type of the entity `Name` of the entity map `E`, with the id
|
|
1754
|
+
* field its kind adds (`accountId?: string` for a stored `Account`).
|
|
1755
|
+
*/
|
|
1456
1756
|
export type InferEntity<E, Name extends keyof E & string> = Simplify<
|
|
1457
1757
|
Infer<E[Name], E> & IdOf<Name, E[Name]>
|
|
1458
1758
|
>;
|
|
@@ -1462,7 +1762,7 @@ export type InferEntity<E, Name extends keyof E & string> = Simplify<
|
|
|
1462
1762
|
// as a definition `Infer` reads. A derived definition always has a kind as
|
|
1463
1763
|
// its first key, so its fields read as a struct whatever their names.
|
|
1464
1764
|
|
|
1465
|
-
|
|
1765
|
+
/** The definition a schema type was built from. */
|
|
1466
1766
|
export type DefinitionOf<S> = S extends Schema<infer D> ? D : never;
|
|
1467
1767
|
|
|
1468
1768
|
// A schema whose definition is not known (`Schema`, an entity of a model):
|
|
@@ -1502,7 +1802,7 @@ type IsStructDef<D> = D extends string
|
|
|
1502
1802
|
: true
|
|
1503
1803
|
: false;
|
|
1504
1804
|
|
|
1505
|
-
|
|
1805
|
+
/** What a derive function takes: a struct, or a schema of an unknown one. */
|
|
1506
1806
|
export type StructGuardOf<D> =
|
|
1507
1807
|
IsWide<D> extends true ? unknown : IsStructDef<D> extends true ? unknown : never;
|
|
1508
1808
|
|
|
@@ -1518,9 +1818,11 @@ type CalculatedKeyOf<D> = {
|
|
|
1518
1818
|
[K in keyof D]: D[K] extends Fn ? (K extends OptionKey ? never : K) : never;
|
|
1519
1819
|
}[keyof D];
|
|
1520
1820
|
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1821
|
+
/**
|
|
1822
|
+
* The names a derive function takes: the fields, the calculated fields and,
|
|
1823
|
+
* for a stored kind, its id (named after the entity, which the definition
|
|
1824
|
+
* does not carry).
|
|
1825
|
+
*/
|
|
1524
1826
|
export type DeriveKeyOf<D> =
|
|
1525
1827
|
IsWide<D> extends true
|
|
1526
1828
|
? string
|
|
@@ -1544,31 +1846,35 @@ type DerivedKind<D, O> = O extends { metadata: { kind: infer K extends string }
|
|
|
1544
1846
|
? { Struct: {} }
|
|
1545
1847
|
: { [P in KindName<D> & keyof D]: D[P] };
|
|
1546
1848
|
|
|
1547
|
-
|
|
1849
|
+
/** A derived schema: of an unknown definition when the source's is unknown. */
|
|
1548
1850
|
export type DerivedSchema<D, R> = IsWide<D> extends true ? Schema : Schema<Simplify<R>>;
|
|
1549
1851
|
|
|
1550
1852
|
type Keep<D, K> = {
|
|
1551
|
-
[
|
|
1552
|
-
|
|
1553
|
-
? P
|
|
1554
|
-
: never
|
|
1555
|
-
: P extends CalculatedKeyOf<D>
|
|
1556
|
-
? P extends K
|
|
1853
|
+
[
|
|
1854
|
+
P in keyof D as P extends FieldKeyOf<D>
|
|
1855
|
+
? FieldName<P> extends K
|
|
1557
1856
|
? P
|
|
1558
1857
|
: never
|
|
1559
|
-
:
|
|
1858
|
+
: P extends CalculatedKeyOf<D>
|
|
1859
|
+
? P extends K
|
|
1860
|
+
? P
|
|
1861
|
+
: never
|
|
1862
|
+
: never
|
|
1863
|
+
]: D[P];
|
|
1560
1864
|
};
|
|
1561
1865
|
|
|
1562
1866
|
type Drop<D, K> = {
|
|
1563
|
-
[
|
|
1564
|
-
|
|
1565
|
-
?
|
|
1566
|
-
: P
|
|
1567
|
-
: P extends CalculatedKeyOf<D>
|
|
1568
|
-
? P extends K
|
|
1867
|
+
[
|
|
1868
|
+
P in keyof D as P extends FieldKeyOf<D>
|
|
1869
|
+
? FieldName<P> extends K
|
|
1569
1870
|
? never
|
|
1570
1871
|
: P
|
|
1571
|
-
:
|
|
1872
|
+
: P extends CalculatedKeyOf<D>
|
|
1873
|
+
? P extends K
|
|
1874
|
+
? never
|
|
1875
|
+
: P
|
|
1876
|
+
: never
|
|
1877
|
+
]: D[P];
|
|
1572
1878
|
};
|
|
1573
1879
|
|
|
1574
1880
|
export type PickDefinition<D, K, O = {}> = DerivedKind<D, O> & Keep<D, K>;
|
|
@@ -1617,21 +1923,25 @@ type OptionalDef<X, O> = X extends string
|
|
|
1617
1923
|
: NoDefault<X, O>;
|
|
1618
1924
|
|
|
1619
1925
|
type PartialFields<D, O> = {
|
|
1620
|
-
[
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1926
|
+
[
|
|
1927
|
+
P in keyof D as P extends FieldKeyOf<D>
|
|
1928
|
+
? `${FieldName<P> & string}?`
|
|
1929
|
+
: P extends CalculatedKeyOf<D>
|
|
1930
|
+
? P
|
|
1931
|
+
: never
|
|
1932
|
+
]: P extends FieldKeyOf<D> ? OptionalDef<D[P], O> : D[P];
|
|
1625
1933
|
};
|
|
1626
1934
|
|
|
1627
1935
|
type PartialOf<D, O> = {
|
|
1628
|
-
[
|
|
1629
|
-
|
|
1630
|
-
?
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1936
|
+
[
|
|
1937
|
+
P in keyof D as P extends FieldKeyOf<D>
|
|
1938
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1939
|
+
? `${FieldName<P> & string}?`
|
|
1940
|
+
: P
|
|
1941
|
+
: P extends CalculatedKeyOf<D>
|
|
1942
|
+
? P
|
|
1943
|
+
: never
|
|
1944
|
+
]: P extends FieldKeyOf<D>
|
|
1635
1945
|
? FieldName<P> extends KeysOf<O, D>
|
|
1636
1946
|
? OptionalDef<D[P], O>
|
|
1637
1947
|
: D[P]
|
|
@@ -1645,11 +1955,13 @@ type RequiredDef<X, O> = X extends `?${infer N}`
|
|
|
1645
1955
|
: X extends string
|
|
1646
1956
|
? X
|
|
1647
1957
|
: Simplify<
|
|
1648
|
-
(
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1958
|
+
(
|
|
1959
|
+
X extends { type: `?${infer N}` }
|
|
1960
|
+
? Omit<X, 'type' | 'required'> & { type: N }
|
|
1961
|
+
: IsStructDef<X> extends true
|
|
1962
|
+
? X
|
|
1963
|
+
: Omit<X, 'required'>
|
|
1964
|
+
) extends infer Y
|
|
1653
1965
|
? IsDeep<O> extends true
|
|
1654
1966
|
? [NestedOf<X>] extends [never]
|
|
1655
1967
|
? Y
|
|
@@ -1669,13 +1981,15 @@ type RequiredFields<D, O> = {
|
|
|
1669
1981
|
};
|
|
1670
1982
|
|
|
1671
1983
|
type RequiredOf<D, O> = {
|
|
1672
|
-
[
|
|
1673
|
-
|
|
1674
|
-
? FieldName<P>
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1984
|
+
[
|
|
1985
|
+
P in keyof D as P extends FieldKeyOf<D>
|
|
1986
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1987
|
+
? FieldName<P>
|
|
1988
|
+
: P
|
|
1989
|
+
: P extends CalculatedKeyOf<D> | OptionKey
|
|
1990
|
+
? P
|
|
1991
|
+
: never
|
|
1992
|
+
]: P extends FieldKeyOf<D>
|
|
1679
1993
|
? FieldName<P> extends KeysOf<O, D>
|
|
1680
1994
|
? RequiredDef<D[P], O>
|
|
1681
1995
|
: D[P]
|
|
@@ -1687,22 +2001,30 @@ export type RequiredDefinition<D, O = {}> = DerivedKind<D, O> & RequiredOf<D, O>
|
|
|
1687
2001
|
// What an extension adds: its fields (a Schema's, without its kind).
|
|
1688
2002
|
type ExtensionOf<X> = X extends Schema<infer S> ? Omit<S, KindKey<S>> : X;
|
|
1689
2003
|
|
|
1690
|
-
|
|
1691
|
-
|
|
2004
|
+
/**
|
|
2005
|
+
* An extension whose fields are not known (a `Schema`, an entity of a
|
|
2006
|
+
* model): what it extends is not known either.
|
|
2007
|
+
*/
|
|
1692
2008
|
export type ExtendedSchema<D, X, R> =
|
|
1693
|
-
X extends Schema<infer S>
|
|
2009
|
+
X extends Schema<infer S>
|
|
2010
|
+
? IsWide<S> extends true
|
|
2011
|
+
? Schema
|
|
2012
|
+
: DerivedSchema<D, R>
|
|
2013
|
+
: DerivedSchema<D, R>;
|
|
1694
2014
|
|
|
1695
2015
|
export type ExtendDefinition<D, X, O = {}> = DerivedKind<D, O> & {
|
|
1696
|
-
[
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
?
|
|
2016
|
+
[
|
|
2017
|
+
P in keyof D as P extends KindKey<D>
|
|
2018
|
+
? never
|
|
2019
|
+
: P extends FieldKeyOf<D>
|
|
2020
|
+
? FieldName<P> extends FieldName<keyof ExtensionOf<X>>
|
|
2021
|
+
? never
|
|
2022
|
+
: P
|
|
1701
2023
|
: P
|
|
1702
|
-
|
|
2024
|
+
]: D[P];
|
|
1703
2025
|
} & ExtensionOf<X>;
|
|
1704
2026
|
|
|
1705
|
-
|
|
2027
|
+
/** The names of the fields of a struct definition, as a definition of an enum. */
|
|
1706
2028
|
export type KeyofDefinition<D> = { enum: Array<FieldName<FieldKeyOf<D>>> };
|
|
1707
2029
|
|
|
1708
2030
|
// Standard Schema v1 (https://standardschema.dev): the interface every schema
|
|
@@ -1712,15 +2034,17 @@ export type KeyofDefinition<D> = { enum: Array<FieldName<FieldKeyOf<D>>> };
|
|
|
1712
2034
|
// devDependency); tests/types/standard.test-d.ts pins that a `Schema<D>` is a
|
|
1713
2035
|
// `StandardSchemaV1<Infer<D>, InferOutput<D>>` of that package.
|
|
1714
2036
|
|
|
1715
|
-
|
|
2037
|
+
/** The vendor-specific options of a `validate` call are the options of `parse`. */
|
|
1716
2038
|
export interface StandardOptions {
|
|
1717
2039
|
readonly libraryOptions?: ParseOptions | undefined;
|
|
1718
2040
|
}
|
|
1719
2041
|
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
2042
|
+
/**
|
|
2043
|
+
* The result of parse as the specification reads it: the value parse gives
|
|
2044
|
+
* (the value given, when there was nothing to apply), or the issues of an
|
|
2045
|
+
* invalid one, the result's own (`message` and `path`, as the specification
|
|
2046
|
+
* reads them, plus `code` and `params`).
|
|
2047
|
+
*/
|
|
1724
2048
|
export type StandardResult<T> =
|
|
1725
2049
|
| { readonly value: T; readonly issues?: undefined }
|
|
1726
2050
|
| { readonly issues: ReadonlyArray<ValidationIssue> };
|
|
@@ -1728,46 +2052,65 @@ export type StandardResult<T> =
|
|
|
1728
2052
|
export interface StandardProps<D = string | object> {
|
|
1729
2053
|
readonly version: 1;
|
|
1730
2054
|
readonly vendor: 'alexify.metaschema';
|
|
1731
|
-
|
|
1732
|
-
|
|
2055
|
+
/**
|
|
2056
|
+
* `parse` with the policies of the schema; a promise of the result for a
|
|
2057
|
+
* schema with an async validator, decoder or custom type.
|
|
2058
|
+
*/
|
|
1733
2059
|
readonly validate: (
|
|
1734
2060
|
value: unknown,
|
|
1735
2061
|
options?: StandardOptions,
|
|
1736
2062
|
) => StandardResult<InferOutput<D>> | Promise<StandardResult<InferOutput<D>>>;
|
|
1737
|
-
|
|
1738
|
-
|
|
2063
|
+
/**
|
|
2064
|
+
* Standard JSON Schema: `input(options)` and `output(options)` by target.
|
|
2065
|
+
* Present once `register()` of the generators subpath has run.
|
|
2066
|
+
*/
|
|
1739
2067
|
readonly jsonSchema: StandardConverter;
|
|
1740
|
-
|
|
1741
|
-
|
|
2068
|
+
/**
|
|
2069
|
+
* What parse accepts and what it gives. Type-level only: no property of
|
|
2070
|
+
* the object holds it.
|
|
2071
|
+
*/
|
|
1742
2072
|
readonly types?: { readonly input: Infer<D>; readonly output: InferOutput<D> } | undefined;
|
|
1743
2073
|
}
|
|
1744
2074
|
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
2075
|
+
/**
|
|
2076
|
+
* `D` is the definition the schema was built from, kept as a type only (no
|
|
2077
|
+
* instance property holds it): `Schema.from({ name: 'string' })` is a
|
|
2078
|
+
* `Schema<{ readonly name: 'string' }>`, and `InferSchema` reads `D` from it.
|
|
2079
|
+
* The const type parameter keeps the literal types of an inline definition.
|
|
2080
|
+
*/
|
|
1749
2081
|
export class Schema<const D extends string | object = string | object> {
|
|
1750
2082
|
readonly '~definition'?: D;
|
|
1751
|
-
|
|
2083
|
+
/** Standard Schema v1, built on first use; see `StandardProps`. */
|
|
1752
2084
|
readonly '~standard': StandardProps<D>;
|
|
1753
2085
|
|
|
2086
|
+
/** A schema without a name: `new Schema('', raw, namespaces)`. */
|
|
1754
2087
|
static from<const D extends string | object>(raw: D, namespaces?: Array<Model>): Schema<D>;
|
|
2088
|
+
/** The Schema a definition is or holds under `schema`, else null. */
|
|
1755
2089
|
static extractSchema(def: object): Schema | null;
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
2090
|
+
/**
|
|
2091
|
+
* A definition (or a Schema) from a JSON Schema document: exists once
|
|
2092
|
+
* `register()` of the generators subpath has run; its `fromJSONSchema` is
|
|
2093
|
+
* the same without it.
|
|
2094
|
+
*/
|
|
1759
2095
|
static fromJSONSchema(
|
|
1760
2096
|
document: JSONSchema | true,
|
|
1761
2097
|
options: FromJSONSchemaOptions & { as: 'schema' },
|
|
1762
2098
|
): Schema;
|
|
1763
|
-
static fromJSONSchema(
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
2099
|
+
static fromJSONSchema(
|
|
2100
|
+
document: JSONSchema | true,
|
|
2101
|
+
options?: FromJSONSchemaOptions,
|
|
2102
|
+
): object | string;
|
|
2103
|
+
/**
|
|
2104
|
+
* The schema two schemas describe, by unification: exists once `register()`
|
|
2105
|
+
* of the `@alexify/metaschema/model` subpath has run; its `merge(a, b)` is
|
|
2106
|
+
* the same without it.
|
|
2107
|
+
*/
|
|
1767
2108
|
static merge(a: Schema, b: Schema, options?: MergeOptions): Schema;
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
2109
|
+
/**
|
|
2110
|
+
* A definition (or a Schema) that accepts every sample: exists once
|
|
2111
|
+
* `register()` of the `@alexify/metaschema/model` subpath has run; its
|
|
2112
|
+
* `infer(samples, options)` is the same without it.
|
|
2113
|
+
*/
|
|
1771
2114
|
static infer(samples: Iterable<unknown>, options: InferOptions & { as: 'schema' }): Schema;
|
|
1772
2115
|
static infer(samples: Iterable<unknown>, options?: InferOptions): object | string;
|
|
1773
2116
|
|
|
@@ -1776,54 +2119,86 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
1776
2119
|
store: Store;
|
|
1777
2120
|
allow: Allow;
|
|
1778
2121
|
parent: string;
|
|
1779
|
-
|
|
1780
|
-
|
|
2122
|
+
/**
|
|
2123
|
+
* What `check` does with keys the schema does not have unless the call says
|
|
2124
|
+
* otherwise: `{ Struct: { unknown: 'ignore' } }` sets it.
|
|
2125
|
+
*/
|
|
1781
2126
|
unknown: 'reject' | 'ignore';
|
|
1782
2127
|
indexes: Record<string, object>;
|
|
1783
2128
|
options: SchemaOptions;
|
|
1784
2129
|
custom: Record<string, unknown>;
|
|
1785
|
-
|
|
1786
|
-
|
|
2130
|
+
/**
|
|
2131
|
+
* For a schema whose definition is a single type (`Schema.from('string')`,
|
|
2132
|
+
* `Schema.from({ array: 'number' })`) this is that FieldType itself.
|
|
2133
|
+
*/
|
|
1787
2134
|
fields: Fields;
|
|
1788
2135
|
name: string;
|
|
1789
2136
|
namespaces: Set<Model>;
|
|
1790
2137
|
references: Set<string>;
|
|
1791
2138
|
relations: Set<Relation>;
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
2139
|
+
/**
|
|
2140
|
+
* Lint warnings of the definition, `Warning [code]: text`: unknown field
|
|
2141
|
+
* options, a pattern without length.max, an index over a missing field.
|
|
2142
|
+
* Computed on first use.
|
|
2143
|
+
*/
|
|
1795
2144
|
readonly warnings: Array<string>;
|
|
1796
2145
|
|
|
2146
|
+
/**
|
|
2147
|
+
* Builds the schema of a definition, read by its first key; a definition that can never
|
|
2148
|
+
* validate throws SchemaDefinitionError here. A Schema given as the definition is reused and
|
|
2149
|
+
* the namespaces attached to it; `namespaces` are the models its references resolve in.
|
|
2150
|
+
*/
|
|
1797
2151
|
constructor(name: string, raw: D, namespaces?: Array<Model>);
|
|
2152
|
+
/** The type tables of the attached models, merged; rebuilt when they change. */
|
|
1798
2153
|
get types(): TypeTable;
|
|
2154
|
+
/** The references and types the fields use that nothing attached resolves, as warning lines. */
|
|
1799
2155
|
checkConsistency(): Array<string>;
|
|
2156
|
+
/**
|
|
2157
|
+
* The schema a reference resolves to: an entity of the attached models, else this schema when
|
|
2158
|
+
* the name is its own, else null.
|
|
2159
|
+
*/
|
|
1800
2160
|
findReference(name: string): Schema | null;
|
|
2161
|
+
/**
|
|
2162
|
+
* Validates a value and returns its issues; never throws because of the value. A validator
|
|
2163
|
+
* that returns a promise throws ALX_ERR_ASYNC_VALIDATOR: use `checkAsync`.
|
|
2164
|
+
*/
|
|
1801
2165
|
check(value: unknown, options?: CheckOptions): ValidationResult;
|
|
1802
|
-
|
|
1803
|
-
|
|
2166
|
+
/**
|
|
2167
|
+
* The same check, with the validators that return a promise settled after
|
|
2168
|
+
* the walk, in field order.
|
|
2169
|
+
*/
|
|
1804
2170
|
checkAsync(value: unknown, options?: CheckOptions): Promise<ValidationResult>;
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
2171
|
+
/**
|
|
2172
|
+
* Validates a value and builds the value of the program from it: defaults,
|
|
2173
|
+
* the check, `unknown: 'strip'`, the decoders of the fields (parts before
|
|
2174
|
+
* the whole) and the schema's own `parse`. The value given is never
|
|
2175
|
+
* written to; it is the output itself when there was nothing to apply.
|
|
2176
|
+
*/
|
|
1809
2177
|
parse(value: unknown, options?: ParseOptions): ParseResult<InferOutput<D>>;
|
|
2178
|
+
/** `parse` with the validators and decoders that return a promise awaited. */
|
|
1810
2179
|
parseAsync(value: unknown, options?: ParseOptions): Promise<ParseResult<InferOutput<D>>>;
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
2180
|
+
/**
|
|
2181
|
+
* The other direction: the schema's own `serialize`, then the serializers
|
|
2182
|
+
* of the fields, from the value down to its parts; with `check` the
|
|
2183
|
+
* verdict of check over the result as well.
|
|
2184
|
+
*/
|
|
1814
2185
|
serialize(
|
|
1815
2186
|
value: InferOutput<D>,
|
|
1816
2187
|
options: SerializeOptions & { check: true },
|
|
1817
2188
|
): SerializeChecked<Infer<D>>;
|
|
1818
2189
|
serialize(value: InferOutput<D>, options?: SerializeOptions): Serialized<Infer<D>>;
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
2190
|
+
/**
|
|
2191
|
+
* A value the schema accepts, the same for a seed: exists once `register()`
|
|
2192
|
+
* of the `@alexify/metaschema/model` subpath has run; its `sample(schema,
|
|
2193
|
+
* options)` is the same without it.
|
|
2194
|
+
*/
|
|
1822
2195
|
sample(options?: SampleOptions): Infer<D>;
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
2196
|
+
/**
|
|
2197
|
+
* The value with the conversions of the schema and its defaults applied,
|
|
2198
|
+
* nothing checked: exists once `register()` of the
|
|
2199
|
+
* `@alexify/metaschema/coerce` subpath has run; its `coerce(schema,
|
|
2200
|
+
* value)` is the same without it.
|
|
2201
|
+
*/
|
|
1827
2202
|
coerce(value: unknown, options?: CoerceOptions): unknown;
|
|
1828
2203
|
// The six members below exist once `register()` of the
|
|
1829
2204
|
// `@alexify/metaschema/derive` subpath has run; its `pick(schema, keys)`
|
|
@@ -1891,28 +2266,45 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
1891
2266
|
// are the same without it.
|
|
1892
2267
|
// The schema as a JSON Schema document of the target dialect.
|
|
1893
2268
|
toJSONSchema(options?: JSONSchemaOptions): JSONSchema;
|
|
1894
|
-
|
|
2269
|
+
/** The schema as a TypeScript interface, the description of a field as JSDoc. */
|
|
1895
2270
|
toInterface(options?: InterfaceOptions): string;
|
|
2271
|
+
/** Adds models the references of the schema resolve in. */
|
|
1896
2272
|
attach(...namespaces: Array<Model>): void;
|
|
2273
|
+
/** Removes models the references of the schema resolved in. */
|
|
1897
2274
|
detach(...namespaces: Array<Model>): void;
|
|
2275
|
+
/** The schema as JSON text. */
|
|
1898
2276
|
toString(): string;
|
|
2277
|
+
/** The fields of the schema as plain data (a single type: its definition). */
|
|
1899
2278
|
toJSON(): object;
|
|
1900
|
-
|
|
2279
|
+
/** The schema as plain JSON: see `SchemaSnapshot`. */
|
|
1901
2280
|
toSnapshot(): SchemaSnapshot;
|
|
2281
|
+
/** Runs the schema's own `validate` alone: a result of what it returned, or null without one. */
|
|
1902
2282
|
validate(value: unknown, path?: string): ValidationResult | null;
|
|
1903
2283
|
}
|
|
1904
2284
|
|
|
2285
|
+
/**
|
|
2286
|
+
* A domain model: the entities built as Schemas with the model as their namespace, ordered
|
|
2287
|
+
* dependencies first, and the custom types they use.
|
|
2288
|
+
*/
|
|
1905
2289
|
export class Model {
|
|
2290
|
+
/** The type table of the entities: the process-wide one, or the model's own when isolated. */
|
|
1906
2291
|
types: TypeTable;
|
|
1907
|
-
|
|
1908
|
-
|
|
2292
|
+
/**
|
|
2293
|
+
* The converters of coercion the model was given (`ModelOptions.coercions`),
|
|
2294
|
+
* frozen, or null.
|
|
2295
|
+
*/
|
|
1909
2296
|
readonly coercions: Readonly<Record<string, Coercion>> | null;
|
|
2297
|
+
/** The entities by name. */
|
|
1910
2298
|
entities: Map<string, Schema>;
|
|
2299
|
+
/** The database metadata the model was given. */
|
|
1911
2300
|
database: Record<string, unknown> | null;
|
|
2301
|
+
/** The names of the entities, `Identifier` first, each after the entities it references. */
|
|
1912
2302
|
order: Set<string>;
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
2303
|
+
/**
|
|
2304
|
+
* The warnings of every entity, plus references that do not resolve
|
|
2305
|
+
* (missing-reference) and recursive dependencies (recursive-reference).
|
|
2306
|
+
* Computed on first use.
|
|
2307
|
+
*/
|
|
1916
2308
|
readonly warnings: Array<string>;
|
|
1917
2309
|
|
|
1918
2310
|
constructor(
|
|
@@ -1921,8 +2313,10 @@ export class Model {
|
|
|
1921
2313
|
database?: Record<string, unknown> | null,
|
|
1922
2314
|
options?: ModelOptions,
|
|
1923
2315
|
);
|
|
1924
|
-
|
|
1925
|
-
|
|
2316
|
+
/**
|
|
2317
|
+
* The model as plain JSON, what JSON.stringify(model) writes; see
|
|
2318
|
+
* `ModelSnapshot`.
|
|
2319
|
+
*/
|
|
1926
2320
|
toJSON(): ModelSnapshot;
|
|
1927
2321
|
// The four statics below exist once `register()` of the
|
|
1928
2322
|
// `@alexify/metaschema/model` subpath has run; its `fromJSON`, `diff`,
|
|
@@ -1937,8 +2331,10 @@ export class Model {
|
|
|
1937
2331
|
// Every entity as a JSON Schema definition (`$defs`, `definitions` or
|
|
1938
2332
|
// `components.schemas` by target), or the document of `options.root`.
|
|
1939
2333
|
toJSONSchema(options?: ModelJSONSchemaOptions): JSONSchema;
|
|
1940
|
-
|
|
1941
|
-
|
|
2334
|
+
/**
|
|
2335
|
+
* The interfaces of every entity in dependency order; `dts` is the same
|
|
2336
|
+
* without options.
|
|
2337
|
+
*/
|
|
1942
2338
|
toTypeScript(options?: InterfaceOptions): string;
|
|
1943
2339
|
get dts(): string;
|
|
1944
2340
|
}
|