@alexify/metaschema 1.0.0 → 3.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 +551 -1
- package/README.md +235 -34
- package/SECURITY.md +32 -11
- package/index.d.ts +798 -23
- package/package.json +30 -6
- package/src/codec.js +490 -0
- package/src/compile/code.js +92 -0
- package/src/compile/emit.js +512 -0
- package/src/compile/index.d.ts +38 -0
- package/src/compile/index.js +176 -0
- package/src/context.js +141 -0
- package/src/errors.js +10 -1
- package/src/generators/index.d.ts +29 -0
- package/src/generators/index.js +90 -0
- package/src/generators/jsonschema.js +535 -0
- package/src/generators/typescript.js +237 -0
- package/src/index.js +1 -3
- package/src/issues.js +291 -0
- package/src/kinds.js +9 -12
- package/src/lint.js +98 -0
- package/src/locales/en.d.ts +5 -0
- package/src/locales/en.js +66 -0
- package/src/locales/uk.d.ts +5 -0
- package/src/locales/uk.js +62 -0
- package/src/metadata.js +65 -119
- package/src/metautil.js +8 -1
- package/src/model.js +31 -20
- package/src/preprocessor.js +45 -27
- package/src/prototypes/abstract.js +101 -51
- package/src/prototypes/collections.js +106 -41
- package/src/prototypes/json.js +9 -5
- package/src/prototypes/reference.js +91 -20
- package/src/prototypes/scalars.js +124 -16
- package/src/prototypes/schema.js +31 -6
- package/src/prototypes/tuple.js +57 -29
- package/src/prototypes/union.js +139 -0
- package/src/result.js +115 -0
- package/src/rules.js +185 -0
- package/src/runtime/browser.js +2 -2
- package/src/runtime/node.js +4 -2
- package/src/schema.js +181 -68
- package/src/struct.js +101 -33
- package/src/types.js +14 -14
- package/src/util.js +41 -50
package/index.d.ts
CHANGED
|
@@ -35,22 +35,62 @@ export type IssueCode =
|
|
|
35
35
|
| 'unexpected'
|
|
36
36
|
| 'enum'
|
|
37
37
|
| 'length'
|
|
38
|
+
| 'range'
|
|
39
|
+
| 'pattern'
|
|
40
|
+
| 'union'
|
|
38
41
|
| 'reference'
|
|
39
42
|
| 'circular'
|
|
43
|
+
| 'depth'
|
|
40
44
|
| 'exception'
|
|
41
45
|
| 'custom';
|
|
42
46
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
// The params of the issues the library produces, by code.
|
|
48
|
+
export interface IssueParams {
|
|
49
|
+
required: {};
|
|
50
|
+
// `key` is set when a key of an `object` or `map`, not a value, has the wrong type.
|
|
51
|
+
type: { expected: string; received: string; key?: PropertyKey };
|
|
52
|
+
unexpected: { keys: string[] };
|
|
53
|
+
enum: { values: unknown[] };
|
|
54
|
+
length: { min: number | undefined; max: number | undefined; actual: number };
|
|
55
|
+
// A `min`/`max` rule failed; the bounds are what the definition gave.
|
|
56
|
+
range: { min: number | bigint | undefined; max: number | bigint | undefined; actual: number | bigint };
|
|
57
|
+
// The source of the pattern the string did not match.
|
|
58
|
+
pattern: { pattern: string };
|
|
59
|
+
// No branch of a union matched: the branch names, or the discriminator
|
|
60
|
+
// values when the union has a discriminator (the path then ends with it).
|
|
61
|
+
union: { expected: unknown[]; discriminator: string | undefined };
|
|
62
|
+
reference: { entity: string };
|
|
63
|
+
circular: {};
|
|
64
|
+
depth: { max: number };
|
|
65
|
+
exception: { error: unknown };
|
|
66
|
+
// A validator's own issue carries the params it gave, if any.
|
|
67
|
+
custom: Record<string, unknown>;
|
|
47
68
|
}
|
|
48
69
|
|
|
49
|
-
//
|
|
70
|
+
// An issue is data: the code, the path of keys from the root of the value, the
|
|
71
|
+
// message about the problem (without the location) and the params the message
|
|
72
|
+
// was rendered from. A validator's own issue carries the code and params it
|
|
73
|
+
// gave; `IssueOf<Code>` is the issue of one of the library's codes.
|
|
74
|
+
export type ValidationIssue =
|
|
75
|
+
| {
|
|
76
|
+
[Code in IssueCode]: {
|
|
77
|
+
code: Code;
|
|
78
|
+
path: PropertyKey[];
|
|
79
|
+
message: string;
|
|
80
|
+
params: IssueParams[Code];
|
|
81
|
+
};
|
|
82
|
+
}[IssueCode]
|
|
83
|
+
| { code: string; path: PropertyKey[]; message: string; params: Record<string, unknown> };
|
|
84
|
+
|
|
85
|
+
export type IssueOf<Code extends IssueCode> = Extract<ValidationIssue, { code: Code }>;
|
|
86
|
+
|
|
87
|
+
// What a validator may return: an issue of its own needs only a message. Its
|
|
88
|
+
// path is relative to the field the validator belongs to.
|
|
50
89
|
export interface IssueInput {
|
|
51
90
|
code?: string;
|
|
52
|
-
path?:
|
|
91
|
+
path?: PropertyKey | PropertyKey[];
|
|
53
92
|
message: string;
|
|
93
|
+
params?: Record<string, unknown>;
|
|
54
94
|
}
|
|
55
95
|
|
|
56
96
|
export type ValidationReturn =
|
|
@@ -63,23 +103,128 @@ export type ValidationReturn =
|
|
|
63
103
|
| undefined
|
|
64
104
|
| void;
|
|
65
105
|
|
|
106
|
+
// The messages of a locale: a renderer per issue code, from the params of the
|
|
107
|
+
// issue to the text that follows the field, and `field` for the location
|
|
108
|
+
// prefix of an error line. A partial table falls back to English.
|
|
109
|
+
export type Locale = {
|
|
110
|
+
[Code in IssueCode]?: (params: IssueParams[Code], issue?: ValidationIssue) => string;
|
|
111
|
+
} & {
|
|
112
|
+
field?: (path: string) => string;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export type Messages = Locale | ((issue: ValidationIssue) => string);
|
|
116
|
+
|
|
66
117
|
export interface CheckOptions {
|
|
67
|
-
//
|
|
118
|
+
// The label the error lines start with: the schema name by default, '' for
|
|
119
|
+
// none. Issue paths are relative to the value and do not include it.
|
|
120
|
+
root?: string;
|
|
121
|
+
// Stop collecting after this many issues (at least 1).
|
|
68
122
|
maxErrors?: number;
|
|
123
|
+
// How deep a value may nest through embedded references (an integer of at
|
|
124
|
+
// least 1, 128 by default): a record deeper than that is a `depth` issue.
|
|
125
|
+
maxDepth?: number;
|
|
126
|
+
// What to do with keys the schema does not have: report them as one
|
|
127
|
+
// `unexpected` issue per struct, or ignore them. The default is the
|
|
128
|
+
// schema's own `unknown` metadata, 'reject' unless it says otherwise.
|
|
129
|
+
unknown?: 'reject' | 'ignore';
|
|
130
|
+
// How a reference is checked: as its target's kind says ('kind', the
|
|
131
|
+
// default: a stored kind as an id, a memory kind as the record), every
|
|
132
|
+
// reference as the record ('embed') or every reference as an id ('id').
|
|
133
|
+
references?: 'kind' | 'embed' | 'id';
|
|
134
|
+
// The locale of the messages, or a function that renders every message.
|
|
135
|
+
messages?: Messages;
|
|
69
136
|
}
|
|
70
137
|
|
|
71
|
-
|
|
138
|
+
// The options of parse: those of check, and `strip` for the keys the schema
|
|
139
|
+
// does not have, which the output then leaves out.
|
|
140
|
+
export interface ParseOptions extends Omit<CheckOptions, 'unknown'> {
|
|
141
|
+
unknown?: 'reject' | 'ignore' | 'strip';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// What parse returns: the output only for a valid value, the issues always.
|
|
145
|
+
export type ParseResult<T> =
|
|
146
|
+
| { valid: true; value: T; issues: [] }
|
|
147
|
+
| { valid: false; value: undefined; issues: ValidationIssue[] };
|
|
148
|
+
|
|
149
|
+
// The options of serialize: `check` validates the result with the other
|
|
150
|
+
// options as the options of check.
|
|
151
|
+
export interface SerializeOptions extends CheckOptions {
|
|
152
|
+
check?: boolean;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export interface Serialized<T> {
|
|
156
|
+
value: T;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface SerializeChecked<T> {
|
|
72
160
|
valid: boolean;
|
|
161
|
+
value: T;
|
|
162
|
+
issues: ValidationIssue[];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface ResultOptions {
|
|
166
|
+
// The label the error lines start with (the schema name in a check).
|
|
167
|
+
root?: string;
|
|
168
|
+
messages?: Messages;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Messages by dotted path: `formErrors` for the value itself, `fieldErrors`
|
|
172
|
+
// for its fields.
|
|
173
|
+
export interface FlatIssues {
|
|
174
|
+
formErrors: string[];
|
|
175
|
+
fieldErrors: Record<string, string[]>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Messages as a tree that follows the value.
|
|
179
|
+
export interface IssueTree {
|
|
73
180
|
errors: string[];
|
|
181
|
+
properties?: Record<string, IssueTree>;
|
|
182
|
+
items?: IssueTree[];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export class ValidationResult {
|
|
74
186
|
issues: ValidationIssue[];
|
|
75
|
-
|
|
187
|
+
readonly valid: boolean;
|
|
188
|
+
// The messages with their location, rendered on first use.
|
|
189
|
+
readonly errors: string[];
|
|
190
|
+
readonly summary: string;
|
|
191
|
+
constructor(options?: ResultOptions);
|
|
76
192
|
add(error: ValidationReturn, code?: IssueCode): this;
|
|
77
|
-
|
|
78
|
-
|
|
193
|
+
flatten(): FlatIssues;
|
|
194
|
+
tree(): IssueTree;
|
|
195
|
+
static issuesOf(error: ValidationReturn, path?: PropertyKey[], code?: IssueCode): ValidationIssue[];
|
|
79
196
|
static isInstance(error: unknown): boolean;
|
|
80
197
|
}
|
|
81
198
|
|
|
82
|
-
|
|
199
|
+
// The state of one check, threaded through the `check` of every field.
|
|
200
|
+
export interface CheckContext {
|
|
201
|
+
issues: ValidationIssue[];
|
|
202
|
+
count: number;
|
|
203
|
+
limit: number;
|
|
204
|
+
// How many embedded records the check is inside, and how many it may be
|
|
205
|
+
// before a record is a `depth` issue (128 by default).
|
|
206
|
+
depth: number;
|
|
207
|
+
maxDepth: number;
|
|
208
|
+
path: PropertyKey[];
|
|
209
|
+
seen: Set<object> | null;
|
|
210
|
+
// The value the check started from, when the schema can reach it again
|
|
211
|
+
// through a reference; null otherwise.
|
|
212
|
+
source: unknown;
|
|
213
|
+
// 'strip' in a parse, where the check behind it treats it as 'ignore'.
|
|
214
|
+
unknown: 'reject' | 'ignore' | 'strip';
|
|
215
|
+
references: 'kind' | 'embed' | 'id';
|
|
216
|
+
root: string;
|
|
217
|
+
messages: Messages;
|
|
218
|
+
// The validators that returned a promise, settled after the walk by
|
|
219
|
+
// checkAsync and parseAsync; null in a synchronous check.
|
|
220
|
+
async: unknown[] | null;
|
|
221
|
+
// The context parse tries the branches of a plain union in; null in a check.
|
|
222
|
+
scratch: CheckContext | null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// A validator may return a promise of its result to checkAsync and parseAsync;
|
|
226
|
+
// to check and parse a promise is a TypeError with the code ERR_ASYNC_VALIDATOR.
|
|
227
|
+
export type Validator = (value: any, path: string) => ValidationReturn | Promise<ValidationReturn>;
|
|
83
228
|
|
|
84
229
|
export type CalculatedField = (value: any) => unknown;
|
|
85
230
|
|
|
@@ -88,8 +233,20 @@ export type CalculatedField = (value: any) => unknown;
|
|
|
88
233
|
export interface FieldType {
|
|
89
234
|
readonly type: string;
|
|
90
235
|
required: boolean;
|
|
236
|
+
// The value may be null; the key is still required unless `required` is false.
|
|
237
|
+
nullable?: boolean;
|
|
238
|
+
// On a reference: check and render it as the record (true) or its id
|
|
239
|
+
// (false) whatever the kind of its target.
|
|
240
|
+
embed?: boolean;
|
|
91
241
|
validate?: Validator;
|
|
92
|
-
|
|
242
|
+
// The decoder parse runs on the validated value, and the serializer
|
|
243
|
+
// serialize runs on the value of the program, when the definition gave them.
|
|
244
|
+
parse?: (value: any) => unknown;
|
|
245
|
+
serialize?: (value: any) => unknown;
|
|
246
|
+
// What parse fills a missing key with: a value, or a function called for one.
|
|
247
|
+
default?: unknown;
|
|
248
|
+
// Records the problems of a value into the context of the current check.
|
|
249
|
+
check(value: unknown, context: CheckContext): void;
|
|
93
250
|
toJSON(): object;
|
|
94
251
|
[key: string]: unknown;
|
|
95
252
|
}
|
|
@@ -111,9 +268,10 @@ export interface TypeEntry {
|
|
|
111
268
|
js?: string;
|
|
112
269
|
metadata?: Record<string, unknown>;
|
|
113
270
|
kind?: 'scalar' | 'struct';
|
|
271
|
+
// The rules the type accepts in a field definition (`length`, `min`, `max`).
|
|
114
272
|
rules?: string[];
|
|
115
273
|
construct?(def: object, preprocessor: object): void;
|
|
116
|
-
checkType?(value: any, path: string): ValidationReturn
|
|
274
|
+
checkType?(value: any, path: string): ValidationReturn | Promise<ValidationReturn>;
|
|
117
275
|
[key: string]: unknown;
|
|
118
276
|
}
|
|
119
277
|
|
|
@@ -129,6 +287,8 @@ export interface KindMetadata {
|
|
|
129
287
|
store: Store;
|
|
130
288
|
allow: Allow;
|
|
131
289
|
parent?: string;
|
|
290
|
+
// The default of `check` for keys the schema does not have.
|
|
291
|
+
unknown?: 'reject' | 'ignore';
|
|
132
292
|
[key: string]: unknown;
|
|
133
293
|
}
|
|
134
294
|
|
|
@@ -144,15 +304,25 @@ export type DefinitionErrorCode =
|
|
|
144
304
|
| 'ERR_UNKNOWN_TYPE'
|
|
145
305
|
| 'ERR_MISSING_SCHEMA'
|
|
146
306
|
| 'ERR_INVALID_TUPLE'
|
|
307
|
+
| 'ERR_INVALID_UNION'
|
|
147
308
|
| 'ERR_PROJECTION'
|
|
148
309
|
| 'ERR_INVALID_CUSTOM_TYPE'
|
|
149
310
|
| 'ERR_INVALID_ENUM'
|
|
150
311
|
| 'ERR_INVALID_LENGTH'
|
|
312
|
+
| 'ERR_INVALID_RULE'
|
|
151
313
|
| 'ERR_INVALID_REFERENCE'
|
|
152
314
|
| 'ERR_RESERVED_KEY'
|
|
153
315
|
| 'ERR_TYPE_REGISTERED'
|
|
154
316
|
| 'ERR_UNKNOWN_JS_TYPE'
|
|
155
|
-
| 'ERR_INVALID_OPTIONS'
|
|
317
|
+
| 'ERR_INVALID_OPTIONS'
|
|
318
|
+
| 'ERR_UNREPRESENTABLE'
|
|
319
|
+
// Thrown by `compile` and `register` of the `@alexify/metaschema/compile`
|
|
320
|
+
// subpath with `jit: true` where code generation from strings is not allowed.
|
|
321
|
+
| 'ERR_NO_EVAL'
|
|
322
|
+
// The check of a schema could not be generated (the generator's error is the
|
|
323
|
+
// `cause`): thrown with `jit: true`, reported as `error` and to `onFallback`
|
|
324
|
+
// in the default mode.
|
|
325
|
+
| 'ERR_JIT_BUILD';
|
|
156
326
|
|
|
157
327
|
export class SchemaDefinitionError extends TypeError {
|
|
158
328
|
code: DefinitionErrorCode;
|
|
@@ -162,6 +332,11 @@ export class SchemaDefinitionError extends TypeError {
|
|
|
162
332
|
locate(schema: string, field: string): this;
|
|
163
333
|
}
|
|
164
334
|
|
|
335
|
+
// The code of the TypeError a synchronous check or parse throws when a
|
|
336
|
+
// validator, a custom checkType or a decoder returns a promise: the call,
|
|
337
|
+
// not the definition, is wrong, so it is a plain TypeError with `code`.
|
|
338
|
+
export type RuntimeErrorCode = 'ERR_ASYNC_VALIDATOR';
|
|
339
|
+
|
|
165
340
|
export const KIND: Array<string>;
|
|
166
341
|
export const KIND_STORED: Array<string>;
|
|
167
342
|
export const KIND_MEMORY: Array<string>;
|
|
@@ -174,10 +349,568 @@ export function getKindMetadata(
|
|
|
174
349
|
meta?: Record<string, unknown>,
|
|
175
350
|
root?: Schema,
|
|
176
351
|
): { defs: Record<string, unknown>; metadata: KindMetadata };
|
|
177
|
-
|
|
352
|
+
// ---------------------------------------------------------------------------
|
|
353
|
+
// The generators: JSON Schema and TypeScript. The functions live in the
|
|
354
|
+
// `@alexify/metaschema/generators` subpath (`toJSONSchema`, `toTypeScript`,
|
|
355
|
+
// `saveTypes`); its `register()` installs the methods declared on `Schema`
|
|
356
|
+
// and `Model` below and the Standard JSON Schema converter. The option
|
|
357
|
+
// types are here so that the core and the subpath share them.
|
|
358
|
+
// ---------------------------------------------------------------------------
|
|
359
|
+
|
|
360
|
+
// The options of `toTypeScript`, `toInterface` and `saveTypes`.
|
|
361
|
+
export interface InterfaceOptions {
|
|
362
|
+
// Give a nested struct and an enum a type of their own, named after the
|
|
363
|
+
// entity and the field (`interface AccountFullName`, `type IdentifierStorage`),
|
|
364
|
+
// rendered before the interface that uses them.
|
|
365
|
+
named?: boolean;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// The JSON Schema export renders the rules of `check` as a document of a
|
|
369
|
+
// target dialect; see the JSON Schema guide for the mapping.
|
|
370
|
+
export type JSONSchemaTarget = 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | 'mongodb';
|
|
371
|
+
|
|
372
|
+
export type JSONSchema = Record<string, unknown>;
|
|
373
|
+
|
|
374
|
+
export interface JSONSchemaOptions {
|
|
375
|
+
// The dialect of the document: JSON Schema draft 2020-12 (the default) or
|
|
376
|
+
// draft-07, an OpenAPI 3.0 schema object, or a MongoDB `$jsonSchema`
|
|
377
|
+
// (`bsonType`, no `$ref`, references inline).
|
|
378
|
+
target?: JSONSchemaTarget;
|
|
379
|
+
// 'strict' renders the dialect of LLM structured outputs (OpenAI, Anthropic):
|
|
380
|
+
// an object at the root, every property listed as required (an optional one
|
|
381
|
+
// accepts null), `additionalProperties: false` on every object, and the
|
|
382
|
+
// structure only (no `length`, `pattern`, `min` or `max`). For the
|
|
383
|
+
// draft-2020-12 and draft-07 targets.
|
|
384
|
+
profile?: 'strict';
|
|
385
|
+
// Which side of the wire the document describes: 'input' (the default) is
|
|
386
|
+
// the JSON a value is parsed from (a `date` is a date-time string), 'output'
|
|
387
|
+
// the value itself (a `date`, `set` or `map` has no JSON form).
|
|
388
|
+
io?: 'input' | 'output';
|
|
389
|
+
// What to do with a type that has no form in the target: throw
|
|
390
|
+
// `ERR_UNREPRESENTABLE` (the default), or render it as `{}`.
|
|
391
|
+
unrepresentable?: 'throw' | 'any';
|
|
392
|
+
// How a reference renders, as in `check`: by the kind of its target ('kind',
|
|
393
|
+
// the default: a stored kind as an id, a memory kind as a `$ref` to its
|
|
394
|
+
// definition), every reference as the record ('embed') or as an id ('id').
|
|
395
|
+
references?: 'kind' | 'embed' | 'id';
|
|
396
|
+
// The pointer the `$ref` of a definition starts with, and where the
|
|
397
|
+
// definitions are placed in the document: '#/$defs/' for draft-2020-12,
|
|
398
|
+
// '#/definitions/' for draft-07, '#/components/schemas/' for openapi-3.0.
|
|
399
|
+
definitions?: string;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export interface ModelJSONSchemaOptions extends JSONSchemaOptions {
|
|
403
|
+
// The entity at the root of the document, with the definitions it needs;
|
|
404
|
+
// without it the document holds every entity as a definition, in
|
|
405
|
+
// dependency order (one document per entity for the mongodb target).
|
|
406
|
+
root?: string;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// Standard JSON Schema (https://standardschema.dev/json-schema): the converter
|
|
410
|
+
// `schema['~standard'].jsonSchema`, whose `input` and `output` take the
|
|
411
|
+
// target and the other options of `toJSONSchema` as `libraryOptions`. The
|
|
412
|
+
// mongodb target is not a JSON Schema and is refused here. Installed by
|
|
413
|
+
// `register()` of the generators subpath.
|
|
414
|
+
export interface StandardJSONSchemaOptions {
|
|
415
|
+
readonly target: 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | ({} & string);
|
|
416
|
+
readonly libraryOptions?: Omit<JSONSchemaOptions, 'target' | 'io'> | undefined;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
export interface StandardConverter {
|
|
420
|
+
readonly input: (options: StandardJSONSchemaOptions) => JSONSchema;
|
|
421
|
+
readonly output: (options: StandardJSONSchemaOptions) => JSONSchema;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// ---------------------------------------------------------------------------
|
|
425
|
+
// Static inference: the TypeScript type of a value a definition accepts.
|
|
426
|
+
//
|
|
427
|
+
// `Infer<D>` reads a definition type the way `Schema` reads the definition
|
|
428
|
+
// object, so the literal types matter: write the definition inline in
|
|
429
|
+
// `Schema.from(...)` / `new Schema(...)` (const type parameters keep them) or
|
|
430
|
+
// declare it `as const`. A definition object has no key order in TypeScript,
|
|
431
|
+
// so where the runtime reads the first key, `Infer` reads the keys that are
|
|
432
|
+
// there: an object with a kind key is a struct, one with a `type` string is
|
|
433
|
+
// the long form, one with a collection key (`array`, `set`, `object`, `map`,
|
|
434
|
+
// `enum`, `tuple`, `union`, `schema`, `one`, `many`) is that shorthand, and
|
|
435
|
+
// anything else is a struct. A struct with a field named like one of those
|
|
436
|
+
// says so with a kind (`{ Struct: {}, ... }`), as the runtime needs it to.
|
|
437
|
+
// ---------------------------------------------------------------------------
|
|
438
|
+
|
|
439
|
+
// The value types of custom types, by type name, for module augmentation:
|
|
440
|
+
// declare module '@alexify/metaschema' { interface CustomTypes { datetime: string } }
|
|
441
|
+
// A type name that is neither built in nor declared here infers as `unknown`.
|
|
442
|
+
export interface CustomTypes {}
|
|
443
|
+
|
|
444
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
445
|
+
|
|
446
|
+
type IsUnion<T, U = T> = T extends unknown ? ([U] extends [T] ? false : true) : never;
|
|
447
|
+
|
|
448
|
+
// The value a definition describes is read twice: as `check` and `parse`
|
|
449
|
+
// accept it ('input') and as `parse` gives it ('output'), where a decoder
|
|
450
|
+
// replaces the value of its field and a default makes its key present.
|
|
451
|
+
type Mode = 'input' | 'output';
|
|
452
|
+
|
|
453
|
+
type ScalarName =
|
|
454
|
+
| 'string'
|
|
455
|
+
| 'number'
|
|
456
|
+
| 'integer'
|
|
457
|
+
| 'bigint'
|
|
458
|
+
| 'boolean'
|
|
459
|
+
| 'date'
|
|
460
|
+
| 'null'
|
|
461
|
+
| 'any'
|
|
462
|
+
| 'unknown'
|
|
463
|
+
| 'json';
|
|
464
|
+
|
|
465
|
+
// The types whose shorthand is `{ <name>: <argument> }`.
|
|
466
|
+
type CompoundName =
|
|
467
|
+
| 'array'
|
|
468
|
+
| 'set'
|
|
469
|
+
| 'object'
|
|
470
|
+
| 'map'
|
|
471
|
+
| 'enum'
|
|
472
|
+
| 'tuple'
|
|
473
|
+
| 'union'
|
|
474
|
+
| 'schema'
|
|
475
|
+
| 'one'
|
|
476
|
+
| 'many';
|
|
477
|
+
|
|
478
|
+
type TypeName = ScalarName | CompoundName | 'reference' | keyof CustomTypes;
|
|
479
|
+
|
|
480
|
+
type ScalarOf<N extends string> = N extends 'string'
|
|
481
|
+
? string
|
|
482
|
+
: N extends 'number' | 'integer'
|
|
483
|
+
? number
|
|
484
|
+
: N extends 'bigint'
|
|
485
|
+
? bigint
|
|
486
|
+
: N extends 'boolean'
|
|
487
|
+
? boolean
|
|
488
|
+
: N extends 'date'
|
|
489
|
+
? Date
|
|
490
|
+
: N extends 'null'
|
|
491
|
+
? null
|
|
492
|
+
: N extends 'any'
|
|
493
|
+
? any
|
|
494
|
+
: N extends 'unknown' | 'json'
|
|
495
|
+
? unknown
|
|
496
|
+
: N extends keyof CustomTypes
|
|
497
|
+
? CustomTypes[N]
|
|
498
|
+
: unknown;
|
|
499
|
+
|
|
500
|
+
// Whether the first character is an uppercase letter: a reference when it is
|
|
501
|
+
// the first character of a type name, a kind when it is the first of a key.
|
|
502
|
+
type IsCapitalized<K> = K extends `${infer C}${string}`
|
|
503
|
+
? C extends Uppercase<C>
|
|
504
|
+
? C extends Lowercase<C>
|
|
505
|
+
? false
|
|
506
|
+
: true
|
|
507
|
+
: false
|
|
508
|
+
: false;
|
|
509
|
+
|
|
510
|
+
type StoredKind =
|
|
511
|
+
| 'entity'
|
|
512
|
+
| 'registry'
|
|
513
|
+
| 'dictionary'
|
|
514
|
+
| 'journal'
|
|
515
|
+
| 'details'
|
|
516
|
+
| 'relation'
|
|
517
|
+
| 'view';
|
|
518
|
+
|
|
519
|
+
type KindKey<D> = { [K in keyof D]: IsCapitalized<K> extends true ? K : never }[keyof D];
|
|
520
|
+
|
|
521
|
+
// The store of an entity definition: its kind metadata, else the default of
|
|
522
|
+
// its kind; a definition without a kind is a struct, held in memory.
|
|
523
|
+
type StoreOf<Def> =
|
|
524
|
+
Def extends Schema<infer X>
|
|
525
|
+
? StoreOf<X>
|
|
526
|
+
: KindKey<Def> extends infer K
|
|
527
|
+
? [K] extends [never]
|
|
528
|
+
? 'memory'
|
|
529
|
+
: K extends keyof Def
|
|
530
|
+
? Def[K] extends { store: infer S extends string }
|
|
531
|
+
? S
|
|
532
|
+
: Lowercase<K & string> extends StoredKind
|
|
533
|
+
? 'persistent'
|
|
534
|
+
: 'memory'
|
|
535
|
+
: 'memory'
|
|
536
|
+
: never;
|
|
537
|
+
|
|
538
|
+
// A reference holds the record (the graph view) when the field says `embed:
|
|
539
|
+
// true` or its target is a memory kind, else its id (the storage view): the
|
|
540
|
+
// rule of `check` and of the generated dts. The record is known only through
|
|
541
|
+
// the entity map `E`; without the target it is `unknown`.
|
|
542
|
+
type Embedded<Name extends string, Embed, E> = Embed extends boolean
|
|
543
|
+
? Embed
|
|
544
|
+
: Name extends keyof E
|
|
545
|
+
? StoreOf<E[Name]> extends 'persistent'
|
|
546
|
+
? false
|
|
547
|
+
: true
|
|
548
|
+
: false;
|
|
549
|
+
|
|
550
|
+
type Referenced<Name extends string, Embed, E, M extends Mode> =
|
|
551
|
+
Embedded<Name, Embed, E> extends true
|
|
552
|
+
? Name extends keyof E
|
|
553
|
+
? Value<E[Name], E, M>
|
|
554
|
+
: unknown
|
|
555
|
+
: string;
|
|
556
|
+
|
|
557
|
+
type EmbedOf<D> = D extends { embed: infer B } ? B : undefined;
|
|
558
|
+
|
|
559
|
+
type KeyTypeOf<N> = N extends 'string'
|
|
560
|
+
? string
|
|
561
|
+
: N extends 'number'
|
|
562
|
+
? number
|
|
563
|
+
: N extends 'symbol'
|
|
564
|
+
? symbol
|
|
565
|
+
: N extends 'bigint'
|
|
566
|
+
? bigint
|
|
567
|
+
: N extends 'boolean'
|
|
568
|
+
? boolean
|
|
569
|
+
: PropertyKey;
|
|
570
|
+
|
|
571
|
+
// The key and value definitions of an `object` or `map`: `{ object: { string:
|
|
572
|
+
// 'number' } }` or the long form's `key` and `value`.
|
|
573
|
+
type Pair<D, N extends string> = D extends { [P in N]: infer Short }
|
|
574
|
+
? Short extends object
|
|
575
|
+
? [keyof Short & string, Short[keyof Short]]
|
|
576
|
+
: never
|
|
577
|
+
: D extends { key: infer K; value: infer V }
|
|
578
|
+
? [K, V]
|
|
579
|
+
: never;
|
|
580
|
+
|
|
581
|
+
type ArgOf<D, N extends string, Alt extends string> = D extends { [P in N]: infer X }
|
|
582
|
+
? X
|
|
583
|
+
: D extends { [P in Alt]: infer X }
|
|
584
|
+
? X
|
|
585
|
+
: never;
|
|
586
|
+
|
|
587
|
+
type ElementsOf<D, N extends string, Alt extends string> =
|
|
588
|
+
ArgOf<D, N, Alt> extends infer X ? (X extends readonly unknown[] ? X : never) : never;
|
|
589
|
+
|
|
590
|
+
type TupleOf<T extends readonly unknown[], E, M extends Mode> = {
|
|
591
|
+
-readonly [I in keyof T]: Element<T[I], E, M>;
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
type UnionOf<T extends readonly unknown[], E, M extends Mode> = {
|
|
595
|
+
[I in keyof T]: Value<T[I], E, M>;
|
|
596
|
+
}[number];
|
|
597
|
+
|
|
598
|
+
// A one-key object holding a type name names a tuple element (`{ x: 'number'
|
|
599
|
+
// }`); the key must not itself be `type`, a type name or a kind.
|
|
600
|
+
type NamedKey<T> = keyof T extends infer K
|
|
601
|
+
? [K] extends [string]
|
|
602
|
+
? IsUnion<K> extends true
|
|
603
|
+
? never
|
|
604
|
+
: K extends `${infer F}?`
|
|
605
|
+
? F extends TypeName | 'type'
|
|
606
|
+
? never
|
|
607
|
+
: IsCapitalized<F> extends true
|
|
608
|
+
? never
|
|
609
|
+
: K
|
|
610
|
+
: K extends TypeName | 'type'
|
|
611
|
+
? never
|
|
612
|
+
: IsCapitalized<K> extends true
|
|
613
|
+
? never
|
|
614
|
+
: K
|
|
615
|
+
: never
|
|
616
|
+
: never;
|
|
617
|
+
|
|
618
|
+
type Element<T, E, M extends Mode> = T extends string
|
|
619
|
+
? Value<T, E, M>
|
|
620
|
+
: T extends readonly unknown[]
|
|
621
|
+
? Value<T, E, M>
|
|
622
|
+
: T extends object
|
|
623
|
+
? NamedKey<T> extends infer K
|
|
624
|
+
? [K] extends [never]
|
|
625
|
+
? Value<T, E, M>
|
|
626
|
+
: K extends keyof T
|
|
627
|
+
? T[K] extends string
|
|
628
|
+
? K extends `${string}?`
|
|
629
|
+
? Value<T[K], E, M> | null | undefined
|
|
630
|
+
: Value<T[K], E, M>
|
|
631
|
+
: Value<T, E, M>
|
|
632
|
+
: Value<T, E, M>
|
|
633
|
+
: never
|
|
634
|
+
: Value<T, E, M>;
|
|
635
|
+
|
|
636
|
+
// The value type of a type by name, with the definition it is read from.
|
|
637
|
+
type TypeOf<N extends string, D, E, M extends Mode> =
|
|
638
|
+
IsCapitalized<N> extends true
|
|
639
|
+
? Referenced<N, EmbedOf<D>, E, M>
|
|
640
|
+
: N extends 'array'
|
|
641
|
+
? Value<ArgOf<D, 'array', 'value'>, E, M>[]
|
|
642
|
+
: N extends 'set'
|
|
643
|
+
? Set<Value<ArgOf<D, 'set', 'value'>, E, M>>
|
|
644
|
+
: N extends 'object'
|
|
645
|
+
? Pair<D, 'object'> extends [infer K, infer V]
|
|
646
|
+
? Record<KeyTypeOf<K> & PropertyKey, Value<V, E, M>>
|
|
647
|
+
: Record<string, unknown>
|
|
648
|
+
: N extends 'map'
|
|
649
|
+
? Pair<D, 'map'> extends [infer K, infer V]
|
|
650
|
+
? Map<KeyTypeOf<K>, Value<V, E, M>>
|
|
651
|
+
: Map<unknown, unknown>
|
|
652
|
+
: N extends 'enum'
|
|
653
|
+
? D extends { enum: readonly (infer V)[] }
|
|
654
|
+
? V
|
|
655
|
+
: unknown
|
|
656
|
+
: N extends 'tuple'
|
|
657
|
+
? TupleOf<ElementsOf<D, 'tuple', 'value'>, E, M>
|
|
658
|
+
: N extends 'union'
|
|
659
|
+
? UnionOf<ElementsOf<D, 'union', 'union'>, E, M>
|
|
660
|
+
: N extends 'schema'
|
|
661
|
+
? D extends { schema: infer S }
|
|
662
|
+
? Value<S, E, M>
|
|
663
|
+
: unknown
|
|
664
|
+
: N extends 'one'
|
|
665
|
+
? D extends { one: infer T extends string }
|
|
666
|
+
? Referenced<T, EmbedOf<D>, E, M>
|
|
667
|
+
: unknown
|
|
668
|
+
: N extends 'many'
|
|
669
|
+
? D extends { many: infer T extends string }
|
|
670
|
+
? Referenced<T, EmbedOf<D>, E, M>[]
|
|
671
|
+
: unknown
|
|
672
|
+
: N extends keyof CustomTypes
|
|
673
|
+
? CustomTypes[N]
|
|
674
|
+
: N extends ScalarName
|
|
675
|
+
? ScalarOf<N>
|
|
676
|
+
: D extends { schema: infer S extends object }
|
|
677
|
+
? Value<S, E, M>
|
|
678
|
+
: unknown;
|
|
679
|
+
|
|
680
|
+
// On the output side a decoder (`parse`) gives the value of its field; a
|
|
681
|
+
// promise of one is awaited.
|
|
682
|
+
type Decoded<T, D, M extends Mode> = M extends 'output'
|
|
683
|
+
? D extends { parse: (...args: never[]) => infer R }
|
|
684
|
+
? Awaited<R>
|
|
685
|
+
: T
|
|
686
|
+
: T;
|
|
687
|
+
|
|
688
|
+
type Nullable<T, D> = D extends { nullable: true } ? T | null : T;
|
|
689
|
+
|
|
690
|
+
type Optional<T, D> = D extends { required: false } ? T | null | undefined : T;
|
|
691
|
+
|
|
692
|
+
// `{ type: 'name', ... }`: the type, `| null` for `nullable: true`, and `|
|
|
693
|
+
// null | undefined` for `'?name'` or `required: false`.
|
|
694
|
+
type LongForm<T extends string, D, E, M extends Mode> = T extends `?${infer N}`
|
|
695
|
+
? Nullable<Decoded<TypeOf<N, D, E, M>, D, M>, D> | null | undefined
|
|
696
|
+
: Optional<Nullable<Decoded<TypeOf<T, D, E, M>, D, M>, D>, D>;
|
|
697
|
+
|
|
698
|
+
type Shorthand<D, E, M extends Mode> = D extends { array: unknown }
|
|
699
|
+
? LongForm<'array', D, E, M>
|
|
700
|
+
: D extends { set: unknown }
|
|
701
|
+
? LongForm<'set', D, E, M>
|
|
702
|
+
: D extends { object: object }
|
|
703
|
+
? LongForm<'object', D, E, M>
|
|
704
|
+
: D extends { map: object }
|
|
705
|
+
? LongForm<'map', D, E, M>
|
|
706
|
+
: D extends { enum: readonly unknown[] }
|
|
707
|
+
? LongForm<'enum', D, E, M>
|
|
708
|
+
: D extends { tuple: readonly unknown[] }
|
|
709
|
+
? LongForm<'tuple', D, E, M>
|
|
710
|
+
: D extends { union: readonly unknown[] }
|
|
711
|
+
? LongForm<'union', D, E, M>
|
|
712
|
+
: D extends { schema: object }
|
|
713
|
+
? LongForm<'schema', D, E, M>
|
|
714
|
+
: D extends { one: string }
|
|
715
|
+
? LongForm<'one', D, E, M>
|
|
716
|
+
: D extends { many: string }
|
|
717
|
+
? LongForm<'many', D, E, M>
|
|
718
|
+
: Decoded<Struct<D, E, M>, D, M>;
|
|
719
|
+
|
|
720
|
+
// What is not a field of a struct: a function (a calculated field, or the
|
|
721
|
+
// schema-level validate/parse/serialize/format) and an index definition.
|
|
722
|
+
type IsIndex<X> = X extends
|
|
723
|
+
| { index: readonly unknown[] }
|
|
724
|
+
| { primary: readonly unknown[] }
|
|
725
|
+
| { unique: readonly unknown[] }
|
|
726
|
+
? true
|
|
727
|
+
: false;
|
|
728
|
+
|
|
729
|
+
type IsField<X> = X extends (...args: never[]) => unknown
|
|
730
|
+
? false
|
|
731
|
+
: IsIndex<X> extends true
|
|
732
|
+
? false
|
|
733
|
+
: true;
|
|
734
|
+
|
|
735
|
+
type IsOptionalDef<X> = X extends `?${string}`
|
|
736
|
+
? true
|
|
737
|
+
: X extends { type: `?${string}` }
|
|
738
|
+
? true
|
|
739
|
+
: X extends { required: false }
|
|
740
|
+
? true
|
|
741
|
+
: false;
|
|
742
|
+
|
|
743
|
+
// Whether a key is optional by its name (`'key?'`) or its definition.
|
|
744
|
+
type IsOptionalKey<K, X> = K extends `${string}?` ? true : IsOptionalDef<X>;
|
|
745
|
+
|
|
746
|
+
// Whether parse fills the key when it is missing.
|
|
747
|
+
type Defaulted<X> = X extends { default: infer V } ? ([V] extends [undefined] ? false : true) : false;
|
|
748
|
+
|
|
749
|
+
type FieldKeys<D> = { [K in keyof D]: IsField<D[K]> extends true ? K : never }[keyof D];
|
|
750
|
+
|
|
751
|
+
// The keys that may be left out: the optional ones, and on the input side
|
|
752
|
+
// those with a default, which parse fills; on the output side a defaulted
|
|
753
|
+
// key is always there.
|
|
754
|
+
type OptionalKeys<D, M extends Mode> = {
|
|
755
|
+
[K in FieldKeys<D>]: M extends 'output'
|
|
756
|
+
? Defaulted<D[K]> extends true
|
|
757
|
+
? never
|
|
758
|
+
: IsOptionalKey<K, D[K]> extends true
|
|
759
|
+
? K
|
|
760
|
+
: never
|
|
761
|
+
: IsOptionalKey<K, D[K]> extends true
|
|
762
|
+
? K
|
|
763
|
+
: Defaulted<D[K]> extends true
|
|
764
|
+
? K
|
|
765
|
+
: never;
|
|
766
|
+
}[FieldKeys<D>];
|
|
767
|
+
|
|
768
|
+
type RequiredKeys<D, M extends Mode> = Exclude<FieldKeys<D>, OptionalKeys<D, M>>;
|
|
769
|
+
|
|
770
|
+
// An optional property accepts null and undefined, as check does; a key that
|
|
771
|
+
// is optional only through its default accepts a missing value.
|
|
772
|
+
type OptionalProperty<K, X, E, M extends Mode> =
|
|
773
|
+
IsOptionalKey<K, X> extends true ? Value<X, E, M> | null | undefined : Value<X, E, M>;
|
|
774
|
+
|
|
775
|
+
// On the output side a defaulted key is present: null stays where the key
|
|
776
|
+
// was optional, undefined is gone.
|
|
777
|
+
type RequiredProperty<K, X, E, M extends Mode> =
|
|
778
|
+
Defaulted<X> extends true
|
|
779
|
+
? Exclude<Value<X, E, M>, undefined> | (IsOptionalKey<K, X> extends true ? null : never)
|
|
780
|
+
: Value<X, E, M>;
|
|
781
|
+
|
|
782
|
+
type FieldName<K> = K extends `${infer F}?` ? F : K;
|
|
783
|
+
|
|
784
|
+
type Simplify<T> = { [K in keyof T]: T[K] };
|
|
785
|
+
|
|
786
|
+
// The fields of a struct: optional ones (`'key?'`, `'?type'`, `required:
|
|
787
|
+
// false`) are optional properties that accept null and undefined, as `check`
|
|
788
|
+
// does.
|
|
789
|
+
type Struct<D, E, M extends Mode> = Simplify<
|
|
790
|
+
{
|
|
791
|
+
-readonly [K in RequiredKeys<D, M> as FieldName<K>]-?: RequiredProperty<K, D[K], E, M>;
|
|
792
|
+
} & {
|
|
793
|
+
-readonly [K in OptionalKeys<D, M> as FieldName<K>]?: OptionalProperty<K, D[K], E, M>;
|
|
794
|
+
}
|
|
795
|
+
>;
|
|
796
|
+
|
|
797
|
+
// A projection copies fields from its parent, known only through `E`.
|
|
798
|
+
type Projection<D, E, M extends Mode> = D extends {
|
|
799
|
+
Projection: { schema: infer S extends string; fields: readonly (infer F)[] };
|
|
800
|
+
}
|
|
801
|
+
? S extends keyof E
|
|
802
|
+
? Pick<Value<E[S], E, M>, Extract<F, keyof Value<E[S], E, M>>>
|
|
803
|
+
: unknown
|
|
804
|
+
: unknown;
|
|
805
|
+
|
|
806
|
+
type WithKind<D, K extends keyof D, E, M extends Mode> = K extends 'Projection'
|
|
807
|
+
? Projection<D, E, M>
|
|
808
|
+
: Struct<Omit<D, K>, E, M>;
|
|
809
|
+
|
|
810
|
+
type ObjectDef<D, E, M extends Mode> =
|
|
811
|
+
KindKey<D> extends infer K
|
|
812
|
+
? [K] extends [never]
|
|
813
|
+
? D extends { type: infer T extends string }
|
|
814
|
+
? LongForm<T, D, E, M>
|
|
815
|
+
: Shorthand<D, E, M>
|
|
816
|
+
: K extends keyof D
|
|
817
|
+
? Decoded<WithKind<D, K, E, M>, D, M>
|
|
818
|
+
: never
|
|
819
|
+
: never;
|
|
820
|
+
|
|
821
|
+
// The id field a stored kind adds, named after the entity.
|
|
822
|
+
type IdOf<Name extends string, Def> =
|
|
823
|
+
StoreOf<Def> extends 'persistent' ? { [K in `${Uncapitalize<Name>}Id`]?: string } : {};
|
|
824
|
+
|
|
825
|
+
type Value<D, E, M extends Mode> =
|
|
826
|
+
IsAny<D> extends true
|
|
827
|
+
? any
|
|
828
|
+
: D extends string
|
|
829
|
+
? D extends `?${infer N}`
|
|
830
|
+
? Value<N, E, M> | null | undefined
|
|
831
|
+
: IsCapitalized<D> extends true
|
|
832
|
+
? Referenced<D, undefined, E, M>
|
|
833
|
+
: ScalarOf<D>
|
|
834
|
+
: D extends Schema<infer X>
|
|
835
|
+
? Value<X, E, M>
|
|
836
|
+
: D extends readonly unknown[]
|
|
837
|
+
? TupleOf<D, E, M>
|
|
838
|
+
: D extends object
|
|
839
|
+
? ObjectDef<D, E, M>
|
|
840
|
+
: unknown;
|
|
841
|
+
|
|
842
|
+
// The type of a value `check` and `parse` accept for the definition `D` (a
|
|
843
|
+
// key with a `default` may be left out, parse fills it). `E` is an optional
|
|
844
|
+
// map of entity definitions by name (the second argument of `Model` as an
|
|
845
|
+
// object): with it a reference to a memory kind is the record and a
|
|
846
|
+
// projection has its parent's fields; without it a memory reference is
|
|
847
|
+
// `unknown` and a stored one is its id. A stored kind's own id field depends
|
|
848
|
+
// on the entity's name, which a definition does not carry: `InferEntity`
|
|
849
|
+
// adds it.
|
|
850
|
+
export type Infer<D, E = {}> = Value<D, E, 'input'>;
|
|
851
|
+
|
|
852
|
+
// The type of the value `parse` gives for the definition `D`: `Infer<D, E>`
|
|
853
|
+
// with the result of every `parse` function (a decoder of a field, the
|
|
854
|
+
// schema's own) in place of the value it decodes, and every defaulted key
|
|
855
|
+
// present.
|
|
856
|
+
export type InferOutput<D, E = {}> = Value<D, E, 'output'>;
|
|
857
|
+
|
|
858
|
+
// The value type of a `Schema<D>` instance: `InferSchema<typeof schema>`.
|
|
859
|
+
export type InferSchema<S> = S extends Schema<infer D> ? Infer<D> : never;
|
|
860
|
+
|
|
861
|
+
// The value type of the entity `Name` of the entity map `E`, with the id
|
|
862
|
+
// field its kind adds (`accountId?: string` for a stored `Account`).
|
|
863
|
+
export type InferEntity<E, Name extends keyof E & string> = Simplify<
|
|
864
|
+
Infer<E[Name], E> & IdOf<Name, E[Name]>
|
|
865
|
+
>;
|
|
866
|
+
|
|
867
|
+
// Standard Schema v1 (https://standardschema.dev): the interface every schema
|
|
868
|
+
// exposes as `schema['~standard']`, so tRPC, TanStack Form, Hono and the other
|
|
869
|
+
// consumers of the specification accept it without an adapter. The shape is
|
|
870
|
+
// written out here because this file cannot import `@standard-schema/spec` (a
|
|
871
|
+
// devDependency); tests/types/standard.test-d.ts pins that a `Schema<D>` is a
|
|
872
|
+
// `StandardSchemaV1<Infer<D>, Infer<D>>` of that package.
|
|
873
|
+
|
|
874
|
+
// The vendor-specific options of a `validate` call are the options of `parse`.
|
|
875
|
+
export interface StandardOptions {
|
|
876
|
+
readonly libraryOptions?: ParseOptions | undefined;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// The result of parse as the specification reads it: the value parse gives
|
|
880
|
+
// (the value given, when there was nothing to apply), or the issues of an
|
|
881
|
+
// invalid one, the result's own (`message` and `path`, as the specification
|
|
882
|
+
// reads them, plus `code` and `params`).
|
|
883
|
+
export type StandardResult<T> =
|
|
884
|
+
| { readonly value: T; readonly issues?: undefined }
|
|
885
|
+
| { readonly issues: ReadonlyArray<ValidationIssue> };
|
|
886
|
+
|
|
887
|
+
export interface StandardProps<D = string | object> {
|
|
888
|
+
readonly version: 1;
|
|
889
|
+
readonly vendor: 'alexify.metaschema';
|
|
890
|
+
// `parse` with the policies of the schema; a promise of the result for a
|
|
891
|
+
// schema with an async validator, decoder or custom type.
|
|
892
|
+
readonly validate: (
|
|
893
|
+
value: unknown,
|
|
894
|
+
options?: StandardOptions,
|
|
895
|
+
) => StandardResult<InferOutput<D>> | Promise<StandardResult<InferOutput<D>>>;
|
|
896
|
+
// Standard JSON Schema: `input(options)` and `output(options)` by target.
|
|
897
|
+
// Present once `register()` of the generators subpath has run.
|
|
898
|
+
readonly jsonSchema: StandardConverter;
|
|
899
|
+
// What parse accepts and what it gives. Type-level only: no property of
|
|
900
|
+
// the object holds it.
|
|
901
|
+
readonly types?: { readonly input: Infer<D>; readonly output: InferOutput<D> } | undefined;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// `D` is the definition the schema was built from, kept as a type only (no
|
|
905
|
+
// instance property holds it): `Schema.from({ name: 'string' })` is a
|
|
906
|
+
// `Schema<{ readonly name: 'string' }>`, and `InferSchema` reads `D` from it.
|
|
907
|
+
// The const type parameter keeps the literal types of an inline definition.
|
|
908
|
+
export class Schema<const D extends string | object = string | object> {
|
|
909
|
+
readonly '~definition'?: D;
|
|
910
|
+
// Standard Schema v1, built on first use; see `StandardProps`.
|
|
911
|
+
readonly '~standard': StandardProps<D>;
|
|
178
912
|
|
|
179
|
-
|
|
180
|
-
static from(raw: string | object, namespaces?: Array<Model>): Schema;
|
|
913
|
+
static from<const D extends string | object>(raw: D, namespaces?: Array<Model>): Schema<D>;
|
|
181
914
|
static extractSchema(def: object): Schema | null;
|
|
182
915
|
|
|
183
916
|
kind: Kind;
|
|
@@ -185,6 +918,9 @@ export class Schema {
|
|
|
185
918
|
store: Store;
|
|
186
919
|
allow: Allow;
|
|
187
920
|
parent: string;
|
|
921
|
+
// What `check` does with keys the schema does not have unless the call says
|
|
922
|
+
// otherwise: `{ Struct: { unknown: 'ignore' } }` sets it.
|
|
923
|
+
unknown: 'reject' | 'ignore';
|
|
188
924
|
indexes: Record<string, object>;
|
|
189
925
|
options: SchemaOptions;
|
|
190
926
|
custom: Record<string, unknown>;
|
|
@@ -195,18 +931,45 @@ export class Schema {
|
|
|
195
931
|
namespaces: Set<Model>;
|
|
196
932
|
references: Set<string>;
|
|
197
933
|
relations: Set<Relation>;
|
|
934
|
+
// Lint warnings of the definition, `Warning [code]: text`: unknown field
|
|
935
|
+
// options, a pattern without length.max, an index over a missing field.
|
|
936
|
+
// Computed on first use.
|
|
937
|
+
readonly warnings: Array<string>;
|
|
198
938
|
|
|
199
|
-
constructor(name: string, raw:
|
|
939
|
+
constructor(name: string, raw: D, namespaces?: Array<Model>);
|
|
200
940
|
get types(): TypeTable;
|
|
201
941
|
checkConsistency(): Array<string>;
|
|
202
942
|
findReference(name: string): Schema | null;
|
|
203
|
-
check(value: unknown,
|
|
204
|
-
|
|
943
|
+
check(value: unknown, options?: CheckOptions): ValidationResult;
|
|
944
|
+
// The same check, with the validators that return a promise settled after
|
|
945
|
+
// the walk, in field order.
|
|
946
|
+
checkAsync(value: unknown, options?: CheckOptions): Promise<ValidationResult>;
|
|
947
|
+
// Validates a value and builds the value of the program from it: defaults,
|
|
948
|
+
// the check, `unknown: 'strip'`, the decoders of the fields (parts before
|
|
949
|
+
// the whole) and the schema's own `parse`. The value given is never
|
|
950
|
+
// written to; it is the output itself when there was nothing to apply.
|
|
951
|
+
parse(value: unknown, options?: ParseOptions): ParseResult<InferOutput<D>>;
|
|
952
|
+
parseAsync(value: unknown, options?: ParseOptions): Promise<ParseResult<InferOutput<D>>>;
|
|
953
|
+
// The other direction: the schema's own `serialize`, then the serializers
|
|
954
|
+
// of the fields, from the value down to its parts; with `check` the
|
|
955
|
+
// verdict of check over the result as well.
|
|
956
|
+
serialize(
|
|
957
|
+
value: InferOutput<D>,
|
|
958
|
+
options: SerializeOptions & { check: true },
|
|
959
|
+
): SerializeChecked<Infer<D>>;
|
|
960
|
+
serialize(value: InferOutput<D>, options?: SerializeOptions): Serialized<Infer<D>>;
|
|
961
|
+
// The two methods below exist once `register()` of the generators subpath
|
|
962
|
+
// has run; the subpath's `toJSONSchema(schema)` and `toTypeScript(schema)`
|
|
963
|
+
// are the same without it.
|
|
964
|
+
// The schema as a JSON Schema document of the target dialect.
|
|
965
|
+
toJSONSchema(options?: JSONSchemaOptions): JSONSchema;
|
|
966
|
+
// The schema as a TypeScript interface, the description of a field as JSDoc.
|
|
967
|
+
toInterface(options?: InterfaceOptions): string;
|
|
205
968
|
attach(...namespaces: Array<Model>): void;
|
|
206
969
|
detach(...namespaces: Array<Model>): void;
|
|
207
970
|
toString(): string;
|
|
208
971
|
toJSON(): object;
|
|
209
|
-
validate(value: unknown, path
|
|
972
|
+
validate(value: unknown, path?: string): ValidationResult | null;
|
|
210
973
|
}
|
|
211
974
|
|
|
212
975
|
export class Model {
|
|
@@ -214,7 +977,10 @@ export class Model {
|
|
|
214
977
|
entities: Map<string, Schema>;
|
|
215
978
|
database: Record<string, unknown> | null;
|
|
216
979
|
order: Set<string>;
|
|
217
|
-
warnings
|
|
980
|
+
// The warnings of every entity, plus references that do not resolve
|
|
981
|
+
// (missing-reference) and recursive dependencies (recursive-reference).
|
|
982
|
+
// Computed on first use.
|
|
983
|
+
readonly warnings: Array<string>;
|
|
218
984
|
|
|
219
985
|
constructor(
|
|
220
986
|
types: Record<string, TypeEntry>,
|
|
@@ -222,5 +988,14 @@ export class Model {
|
|
|
222
988
|
database?: Record<string, unknown> | null,
|
|
223
989
|
options?: ModelOptions,
|
|
224
990
|
);
|
|
991
|
+
// The members below exist once `register()` of the generators subpath has
|
|
992
|
+
// run; the subpath's `toJSONSchema(model)` and `toTypeScript(model)` are the
|
|
993
|
+
// same without it.
|
|
994
|
+
// Every entity as a JSON Schema definition (`$defs`, `definitions` or
|
|
995
|
+
// `components.schemas` by target), or the document of `options.root`.
|
|
996
|
+
toJSONSchema(options?: ModelJSONSchemaOptions): JSONSchema;
|
|
997
|
+
// The interfaces of every entity in dependency order; `dts` is the same
|
|
998
|
+
// without options.
|
|
999
|
+
toTypeScript(options?: InterfaceOptions): string;
|
|
225
1000
|
get dts(): string;
|
|
226
1001
|
}
|