@alexify/metaschema 1.0.0 → 2.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 +228 -1
- package/README.md +124 -28
- package/SECURITY.md +6 -4
- package/index.d.ts +558 -19
- package/package.json +11 -2
- package/src/context.js +95 -0
- package/src/index.js +1 -1
- package/src/issues.js +196 -0
- package/src/lint.js +90 -0
- package/src/locales/en.d.ts +5 -0
- package/src/locales/en.js +58 -0
- package/src/locales/uk.d.ts +5 -0
- package/src/locales/uk.js +57 -0
- package/src/metadata.js +25 -76
- package/src/model.js +23 -7
- package/src/preprocessor.js +42 -19
- package/src/prototypes/abstract.js +86 -49
- package/src/prototypes/collections.js +86 -38
- package/src/prototypes/json.js +9 -5
- package/src/prototypes/reference.js +77 -20
- package/src/prototypes/scalars.js +126 -16
- package/src/prototypes/schema.js +19 -6
- package/src/prototypes/tuple.js +52 -29
- package/src/prototypes/union.js +100 -0
- package/src/result.js +124 -0
- package/src/rules.js +156 -0
- package/src/schema.js +133 -39
- package/src/struct.js +89 -32
- package/src/types.js +9 -3
- package/src/util.js +5 -53
package/index.d.ts
CHANGED
|
@@ -35,22 +35,60 @@ export type IssueCode =
|
|
|
35
35
|
| 'unexpected'
|
|
36
36
|
| 'enum'
|
|
37
37
|
| 'length'
|
|
38
|
+
| 'range'
|
|
39
|
+
| 'pattern'
|
|
40
|
+
| 'union'
|
|
38
41
|
| 'reference'
|
|
39
42
|
| 'circular'
|
|
40
43
|
| 'exception'
|
|
41
44
|
| 'custom';
|
|
42
45
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
// The params of the issues the library produces, by code.
|
|
47
|
+
export interface IssueParams {
|
|
48
|
+
required: {};
|
|
49
|
+
// `key` is set when a key of an `object` or `map`, not a value, has the wrong type.
|
|
50
|
+
type: { expected: string; received: string; key?: PropertyKey };
|
|
51
|
+
unexpected: { keys: string[] };
|
|
52
|
+
enum: { values: unknown[] };
|
|
53
|
+
length: { min: number | undefined; max: number | undefined; actual: number };
|
|
54
|
+
// A `min`/`max` rule failed; the bounds are what the definition gave.
|
|
55
|
+
range: { min: number | bigint | undefined; max: number | bigint | undefined; actual: number | bigint };
|
|
56
|
+
// The source of the pattern the string did not match.
|
|
57
|
+
pattern: { pattern: string };
|
|
58
|
+
// No branch of a union matched: the branch names, or the discriminator
|
|
59
|
+
// values when the union has a discriminator (the path then ends with it).
|
|
60
|
+
union: { expected: unknown[]; discriminator: string | undefined };
|
|
61
|
+
reference: { entity: string };
|
|
62
|
+
circular: {};
|
|
63
|
+
exception: { error: unknown };
|
|
64
|
+
// A validator's own issue carries the params it gave, if any.
|
|
65
|
+
custom: Record<string, unknown>;
|
|
47
66
|
}
|
|
48
67
|
|
|
49
|
-
//
|
|
68
|
+
// An issue is data: the code, the path of keys from the root of the value, the
|
|
69
|
+
// message about the problem (without the location) and the params the message
|
|
70
|
+
// was rendered from. A validator's own issue carries the code and params it
|
|
71
|
+
// gave; `IssueOf<Code>` is the issue of one of the library's codes.
|
|
72
|
+
export type ValidationIssue =
|
|
73
|
+
| {
|
|
74
|
+
[Code in IssueCode]: {
|
|
75
|
+
code: Code;
|
|
76
|
+
path: PropertyKey[];
|
|
77
|
+
message: string;
|
|
78
|
+
params: IssueParams[Code];
|
|
79
|
+
};
|
|
80
|
+
}[IssueCode]
|
|
81
|
+
| { code: string; path: PropertyKey[]; message: string; params: Record<string, unknown> };
|
|
82
|
+
|
|
83
|
+
export type IssueOf<Code extends IssueCode> = Extract<ValidationIssue, { code: Code }>;
|
|
84
|
+
|
|
85
|
+
// What a validator may return: an issue of its own needs only a message. Its
|
|
86
|
+
// path is relative to the field the validator belongs to.
|
|
50
87
|
export interface IssueInput {
|
|
51
88
|
code?: string;
|
|
52
|
-
path?:
|
|
89
|
+
path?: PropertyKey | PropertyKey[];
|
|
53
90
|
message: string;
|
|
91
|
+
params?: Record<string, unknown>;
|
|
54
92
|
}
|
|
55
93
|
|
|
56
94
|
export type ValidationReturn =
|
|
@@ -63,22 +101,82 @@ export type ValidationReturn =
|
|
|
63
101
|
| undefined
|
|
64
102
|
| void;
|
|
65
103
|
|
|
104
|
+
// The messages of a locale: a renderer per issue code, from the params of the
|
|
105
|
+
// issue to the text that follows the field, and `field` for the location
|
|
106
|
+
// prefix of an error line. A partial table falls back to English.
|
|
107
|
+
export type Locale = {
|
|
108
|
+
[Code in IssueCode]?: (params: IssueParams[Code], issue?: ValidationIssue) => string;
|
|
109
|
+
} & {
|
|
110
|
+
field?: (path: string) => string;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type Messages = Locale | ((issue: ValidationIssue) => string);
|
|
114
|
+
|
|
66
115
|
export interface CheckOptions {
|
|
67
|
-
//
|
|
116
|
+
// The label the error lines start with: the schema name by default, '' for
|
|
117
|
+
// none. Issue paths are relative to the value and do not include it.
|
|
118
|
+
root?: string;
|
|
119
|
+
// Stop collecting after this many issues (at least 1).
|
|
68
120
|
maxErrors?: number;
|
|
121
|
+
// What to do with keys the schema does not have: report them as one
|
|
122
|
+
// `unexpected` issue per struct, or ignore them. The default is the
|
|
123
|
+
// schema's own `unknown` metadata, 'reject' unless it says otherwise.
|
|
124
|
+
unknown?: 'reject' | 'ignore';
|
|
125
|
+
// How a reference is checked: as its target's kind says ('kind', the
|
|
126
|
+
// default: a stored kind as an id, a memory kind as the record), every
|
|
127
|
+
// reference as the record ('embed') or every reference as an id ('id').
|
|
128
|
+
references?: 'kind' | 'embed' | 'id';
|
|
129
|
+
// The locale of the messages, or a function that renders every message.
|
|
130
|
+
messages?: Messages;
|
|
69
131
|
}
|
|
70
132
|
|
|
71
|
-
export
|
|
72
|
-
|
|
133
|
+
export interface ResultOptions {
|
|
134
|
+
// The label the error lines start with (the schema name in a check).
|
|
135
|
+
root?: string;
|
|
136
|
+
messages?: Messages;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Messages by dotted path: `formErrors` for the value itself, `fieldErrors`
|
|
140
|
+
// for its fields.
|
|
141
|
+
export interface FlatIssues {
|
|
142
|
+
formErrors: string[];
|
|
143
|
+
fieldErrors: Record<string, string[]>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Messages as a tree that follows the value.
|
|
147
|
+
export interface IssueTree {
|
|
73
148
|
errors: string[];
|
|
149
|
+
properties?: Record<string, IssueTree>;
|
|
150
|
+
items?: IssueTree[];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export class ValidationResult {
|
|
74
154
|
issues: ValidationIssue[];
|
|
75
|
-
|
|
155
|
+
readonly valid: boolean;
|
|
156
|
+
// The messages with their location, rendered on first use.
|
|
157
|
+
readonly errors: string[];
|
|
158
|
+
readonly summary: string;
|
|
159
|
+
constructor(options?: ResultOptions);
|
|
76
160
|
add(error: ValidationReturn, code?: IssueCode): this;
|
|
77
|
-
|
|
78
|
-
|
|
161
|
+
flatten(): FlatIssues;
|
|
162
|
+
tree(): IssueTree;
|
|
163
|
+
static issuesOf(error: ValidationReturn, path?: PropertyKey[], code?: IssueCode): ValidationIssue[];
|
|
79
164
|
static isInstance(error: unknown): boolean;
|
|
80
165
|
}
|
|
81
166
|
|
|
167
|
+
// The state of one check, threaded through the `check` of every field.
|
|
168
|
+
export interface CheckContext {
|
|
169
|
+
issues: ValidationIssue[];
|
|
170
|
+
count: number;
|
|
171
|
+
limit: number;
|
|
172
|
+
path: PropertyKey[];
|
|
173
|
+
seen: Set<object> | null;
|
|
174
|
+
unknown: 'reject' | 'ignore';
|
|
175
|
+
references: 'kind' | 'embed' | 'id';
|
|
176
|
+
root: string;
|
|
177
|
+
messages: Messages;
|
|
178
|
+
}
|
|
179
|
+
|
|
82
180
|
export type Validator = (value: any, path: string) => ValidationReturn;
|
|
83
181
|
|
|
84
182
|
export type CalculatedField = (value: any) => unknown;
|
|
@@ -88,8 +186,14 @@ export type CalculatedField = (value: any) => unknown;
|
|
|
88
186
|
export interface FieldType {
|
|
89
187
|
readonly type: string;
|
|
90
188
|
required: boolean;
|
|
189
|
+
// The value may be null; the key is still required unless `required` is false.
|
|
190
|
+
nullable?: boolean;
|
|
191
|
+
// On a reference: check and render it as the record (true) or its id
|
|
192
|
+
// (false) whatever the kind of its target.
|
|
193
|
+
embed?: boolean;
|
|
91
194
|
validate?: Validator;
|
|
92
|
-
|
|
195
|
+
// Records the problems of a value into the context of the current check.
|
|
196
|
+
check(value: unknown, context: CheckContext): void;
|
|
93
197
|
toJSON(): object;
|
|
94
198
|
[key: string]: unknown;
|
|
95
199
|
}
|
|
@@ -111,6 +215,7 @@ export interface TypeEntry {
|
|
|
111
215
|
js?: string;
|
|
112
216
|
metadata?: Record<string, unknown>;
|
|
113
217
|
kind?: 'scalar' | 'struct';
|
|
218
|
+
// The rules the type accepts in a field definition (`length`, `min`, `max`).
|
|
114
219
|
rules?: string[];
|
|
115
220
|
construct?(def: object, preprocessor: object): void;
|
|
116
221
|
checkType?(value: any, path: string): ValidationReturn;
|
|
@@ -129,6 +234,8 @@ export interface KindMetadata {
|
|
|
129
234
|
store: Store;
|
|
130
235
|
allow: Allow;
|
|
131
236
|
parent?: string;
|
|
237
|
+
// The default of `check` for keys the schema does not have.
|
|
238
|
+
unknown?: 'reject' | 'ignore';
|
|
132
239
|
[key: string]: unknown;
|
|
133
240
|
}
|
|
134
241
|
|
|
@@ -144,10 +251,12 @@ export type DefinitionErrorCode =
|
|
|
144
251
|
| 'ERR_UNKNOWN_TYPE'
|
|
145
252
|
| 'ERR_MISSING_SCHEMA'
|
|
146
253
|
| 'ERR_INVALID_TUPLE'
|
|
254
|
+
| 'ERR_INVALID_UNION'
|
|
147
255
|
| 'ERR_PROJECTION'
|
|
148
256
|
| 'ERR_INVALID_CUSTOM_TYPE'
|
|
149
257
|
| 'ERR_INVALID_ENUM'
|
|
150
258
|
| 'ERR_INVALID_LENGTH'
|
|
259
|
+
| 'ERR_INVALID_RULE'
|
|
151
260
|
| 'ERR_INVALID_REFERENCE'
|
|
152
261
|
| 'ERR_RESERVED_KEY'
|
|
153
262
|
| 'ERR_TYPE_REGISTERED'
|
|
@@ -176,8 +285,428 @@ export function getKindMetadata(
|
|
|
176
285
|
): { defs: Record<string, unknown>; metadata: KindMetadata };
|
|
177
286
|
export function saveTypes(outputFile: string, model: Model): Promise<void>;
|
|
178
287
|
|
|
179
|
-
|
|
180
|
-
|
|
288
|
+
// ---------------------------------------------------------------------------
|
|
289
|
+
// Static inference: the TypeScript type of a value a definition accepts.
|
|
290
|
+
//
|
|
291
|
+
// `Infer<D>` reads a definition type the way `Schema` reads the definition
|
|
292
|
+
// object, so the literal types matter: write the definition inline in
|
|
293
|
+
// `Schema.from(...)` / `new Schema(...)` (const type parameters keep them) or
|
|
294
|
+
// declare it `as const`. A definition object has no key order in TypeScript,
|
|
295
|
+
// so where the runtime reads the first key, `Infer` reads the keys that are
|
|
296
|
+
// there: an object with a kind key is a struct, one with a `type` string is
|
|
297
|
+
// the long form, one with a collection key (`array`, `set`, `object`, `map`,
|
|
298
|
+
// `enum`, `tuple`, `union`, `schema`, `one`, `many`) is that shorthand, and
|
|
299
|
+
// anything else is a struct. A struct with a field named like one of those
|
|
300
|
+
// says so with a kind (`{ Struct: {}, ... }`), as the runtime needs it to.
|
|
301
|
+
// ---------------------------------------------------------------------------
|
|
302
|
+
|
|
303
|
+
// The value types of custom types, by type name, for module augmentation:
|
|
304
|
+
// declare module '@alexify/metaschema' { interface CustomTypes { datetime: string } }
|
|
305
|
+
// A type name that is neither built in nor declared here infers as `unknown`.
|
|
306
|
+
export interface CustomTypes {}
|
|
307
|
+
|
|
308
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
309
|
+
|
|
310
|
+
type IsUnion<T, U = T> = T extends unknown ? ([U] extends [T] ? false : true) : never;
|
|
311
|
+
|
|
312
|
+
type ScalarName =
|
|
313
|
+
| 'string'
|
|
314
|
+
| 'number'
|
|
315
|
+
| 'integer'
|
|
316
|
+
| 'bigint'
|
|
317
|
+
| 'boolean'
|
|
318
|
+
| 'date'
|
|
319
|
+
| 'null'
|
|
320
|
+
| 'any'
|
|
321
|
+
| 'unknown'
|
|
322
|
+
| 'json';
|
|
323
|
+
|
|
324
|
+
// The types whose shorthand is `{ <name>: <argument> }`.
|
|
325
|
+
type CompoundName =
|
|
326
|
+
| 'array'
|
|
327
|
+
| 'set'
|
|
328
|
+
| 'object'
|
|
329
|
+
| 'map'
|
|
330
|
+
| 'enum'
|
|
331
|
+
| 'tuple'
|
|
332
|
+
| 'union'
|
|
333
|
+
| 'schema'
|
|
334
|
+
| 'one'
|
|
335
|
+
| 'many';
|
|
336
|
+
|
|
337
|
+
type TypeName = ScalarName | CompoundName | 'reference' | keyof CustomTypes;
|
|
338
|
+
|
|
339
|
+
type ScalarOf<N extends string> = N extends 'string'
|
|
340
|
+
? string
|
|
341
|
+
: N extends 'number' | 'integer'
|
|
342
|
+
? number
|
|
343
|
+
: N extends 'bigint'
|
|
344
|
+
? bigint
|
|
345
|
+
: N extends 'boolean'
|
|
346
|
+
? boolean
|
|
347
|
+
: N extends 'date'
|
|
348
|
+
? Date
|
|
349
|
+
: N extends 'null'
|
|
350
|
+
? null
|
|
351
|
+
: N extends 'any'
|
|
352
|
+
? any
|
|
353
|
+
: N extends 'unknown' | 'json'
|
|
354
|
+
? unknown
|
|
355
|
+
: N extends keyof CustomTypes
|
|
356
|
+
? CustomTypes[N]
|
|
357
|
+
: unknown;
|
|
358
|
+
|
|
359
|
+
// Whether the first character is an uppercase letter: a reference when it is
|
|
360
|
+
// the first character of a type name, a kind when it is the first of a key.
|
|
361
|
+
type IsCapitalized<K> = K extends `${infer C}${string}`
|
|
362
|
+
? C extends Uppercase<C>
|
|
363
|
+
? C extends Lowercase<C>
|
|
364
|
+
? false
|
|
365
|
+
: true
|
|
366
|
+
: false
|
|
367
|
+
: false;
|
|
368
|
+
|
|
369
|
+
type StoredKind =
|
|
370
|
+
| 'entity'
|
|
371
|
+
| 'registry'
|
|
372
|
+
| 'dictionary'
|
|
373
|
+
| 'journal'
|
|
374
|
+
| 'details'
|
|
375
|
+
| 'relation'
|
|
376
|
+
| 'view';
|
|
377
|
+
|
|
378
|
+
type KindKey<D> = { [K in keyof D]: IsCapitalized<K> extends true ? K : never }[keyof D];
|
|
379
|
+
|
|
380
|
+
// The store of an entity definition: its kind metadata, else the default of
|
|
381
|
+
// its kind; a definition without a kind is a struct, held in memory.
|
|
382
|
+
type StoreOf<Def> =
|
|
383
|
+
Def extends Schema<infer X>
|
|
384
|
+
? StoreOf<X>
|
|
385
|
+
: KindKey<Def> extends infer K
|
|
386
|
+
? [K] extends [never]
|
|
387
|
+
? 'memory'
|
|
388
|
+
: K extends keyof Def
|
|
389
|
+
? Def[K] extends { store: infer S extends string }
|
|
390
|
+
? S
|
|
391
|
+
: Lowercase<K & string> extends StoredKind
|
|
392
|
+
? 'persistent'
|
|
393
|
+
: 'memory'
|
|
394
|
+
: 'memory'
|
|
395
|
+
: never;
|
|
396
|
+
|
|
397
|
+
// A reference holds the record (the graph view) when the field says `embed:
|
|
398
|
+
// true` or its target is a memory kind, else its id (the storage view): the
|
|
399
|
+
// rule of `check` and of the generated dts. The record is known only through
|
|
400
|
+
// the entity map `E`; without the target it is `unknown`.
|
|
401
|
+
type Embedded<Name extends string, Embed, E> = Embed extends boolean
|
|
402
|
+
? Embed
|
|
403
|
+
: Name extends keyof E
|
|
404
|
+
? StoreOf<E[Name]> extends 'persistent'
|
|
405
|
+
? false
|
|
406
|
+
: true
|
|
407
|
+
: false;
|
|
408
|
+
|
|
409
|
+
type Referenced<Name extends string, Embed, E> =
|
|
410
|
+
Embedded<Name, Embed, E> extends true
|
|
411
|
+
? Name extends keyof E
|
|
412
|
+
? Infer<E[Name], E>
|
|
413
|
+
: unknown
|
|
414
|
+
: string;
|
|
415
|
+
|
|
416
|
+
type EmbedOf<D> = D extends { embed: infer B } ? B : undefined;
|
|
417
|
+
|
|
418
|
+
type KeyTypeOf<N> = N extends 'string'
|
|
419
|
+
? string
|
|
420
|
+
: N extends 'number'
|
|
421
|
+
? number
|
|
422
|
+
: N extends 'symbol'
|
|
423
|
+
? symbol
|
|
424
|
+
: N extends 'bigint'
|
|
425
|
+
? bigint
|
|
426
|
+
: N extends 'boolean'
|
|
427
|
+
? boolean
|
|
428
|
+
: PropertyKey;
|
|
429
|
+
|
|
430
|
+
// The key and value definitions of an `object` or `map`: `{ object: { string:
|
|
431
|
+
// 'number' } }` or the long form's `key` and `value`.
|
|
432
|
+
type Pair<D, N extends string> = D extends { [P in N]: infer Short }
|
|
433
|
+
? Short extends object
|
|
434
|
+
? [keyof Short & string, Short[keyof Short]]
|
|
435
|
+
: never
|
|
436
|
+
: D extends { key: infer K; value: infer V }
|
|
437
|
+
? [K, V]
|
|
438
|
+
: never;
|
|
439
|
+
|
|
440
|
+
type ArgOf<D, N extends string, Alt extends string> = D extends { [P in N]: infer X }
|
|
441
|
+
? X
|
|
442
|
+
: D extends { [P in Alt]: infer X }
|
|
443
|
+
? X
|
|
444
|
+
: never;
|
|
445
|
+
|
|
446
|
+
type ElementsOf<D, N extends string, Alt extends string> =
|
|
447
|
+
ArgOf<D, N, Alt> extends infer X ? (X extends readonly unknown[] ? X : never) : never;
|
|
448
|
+
|
|
449
|
+
type TupleOf<T extends readonly unknown[], E> = { -readonly [I in keyof T]: Element<T[I], E> };
|
|
450
|
+
|
|
451
|
+
type UnionOf<T extends readonly unknown[], E> = { [I in keyof T]: Infer<T[I], E> }[number];
|
|
452
|
+
|
|
453
|
+
// A one-key object holding a type name names a tuple element (`{ x: 'number'
|
|
454
|
+
// }`); the key must not itself be `type`, a type name or a kind.
|
|
455
|
+
type NamedKey<T> = keyof T extends infer K
|
|
456
|
+
? [K] extends [string]
|
|
457
|
+
? IsUnion<K> extends true
|
|
458
|
+
? never
|
|
459
|
+
: K extends `${infer F}?`
|
|
460
|
+
? F extends TypeName | 'type'
|
|
461
|
+
? never
|
|
462
|
+
: IsCapitalized<F> extends true
|
|
463
|
+
? never
|
|
464
|
+
: K
|
|
465
|
+
: K extends TypeName | 'type'
|
|
466
|
+
? never
|
|
467
|
+
: IsCapitalized<K> extends true
|
|
468
|
+
? never
|
|
469
|
+
: K
|
|
470
|
+
: never
|
|
471
|
+
: never;
|
|
472
|
+
|
|
473
|
+
type Element<T, E> = T extends string
|
|
474
|
+
? Infer<T, E>
|
|
475
|
+
: T extends readonly unknown[]
|
|
476
|
+
? Infer<T, E>
|
|
477
|
+
: T extends object
|
|
478
|
+
? NamedKey<T> extends infer K
|
|
479
|
+
? [K] extends [never]
|
|
480
|
+
? Infer<T, E>
|
|
481
|
+
: K extends keyof T
|
|
482
|
+
? T[K] extends string
|
|
483
|
+
? K extends `${string}?`
|
|
484
|
+
? Infer<T[K], E> | null | undefined
|
|
485
|
+
: Infer<T[K], E>
|
|
486
|
+
: Infer<T, E>
|
|
487
|
+
: Infer<T, E>
|
|
488
|
+
: never
|
|
489
|
+
: Infer<T, E>;
|
|
490
|
+
|
|
491
|
+
// The value type of a type by name, with the definition it is read from.
|
|
492
|
+
type TypeOf<N extends string, D, E> =
|
|
493
|
+
IsCapitalized<N> extends true
|
|
494
|
+
? Referenced<N, EmbedOf<D>, E>
|
|
495
|
+
: N extends 'array'
|
|
496
|
+
? Infer<ArgOf<D, 'array', 'value'>, E>[]
|
|
497
|
+
: N extends 'set'
|
|
498
|
+
? Set<Infer<ArgOf<D, 'set', 'value'>, E>>
|
|
499
|
+
: N extends 'object'
|
|
500
|
+
? Pair<D, 'object'> extends [infer K, infer V]
|
|
501
|
+
? Record<KeyTypeOf<K> & PropertyKey, Infer<V, E>>
|
|
502
|
+
: Record<string, unknown>
|
|
503
|
+
: N extends 'map'
|
|
504
|
+
? Pair<D, 'map'> extends [infer K, infer V]
|
|
505
|
+
? Map<KeyTypeOf<K>, Infer<V, E>>
|
|
506
|
+
: Map<unknown, unknown>
|
|
507
|
+
: N extends 'enum'
|
|
508
|
+
? D extends { enum: readonly (infer V)[] }
|
|
509
|
+
? V
|
|
510
|
+
: unknown
|
|
511
|
+
: N extends 'tuple'
|
|
512
|
+
? TupleOf<ElementsOf<D, 'tuple', 'value'>, E>
|
|
513
|
+
: N extends 'union'
|
|
514
|
+
? UnionOf<ElementsOf<D, 'union', 'union'>, E>
|
|
515
|
+
: N extends 'schema'
|
|
516
|
+
? D extends { schema: infer S }
|
|
517
|
+
? Infer<S, E>
|
|
518
|
+
: unknown
|
|
519
|
+
: N extends 'one'
|
|
520
|
+
? D extends { one: infer T extends string }
|
|
521
|
+
? Referenced<T, EmbedOf<D>, E>
|
|
522
|
+
: unknown
|
|
523
|
+
: N extends 'many'
|
|
524
|
+
? D extends { many: infer T extends string }
|
|
525
|
+
? Referenced<T, EmbedOf<D>, E>[]
|
|
526
|
+
: unknown
|
|
527
|
+
: N extends keyof CustomTypes
|
|
528
|
+
? CustomTypes[N]
|
|
529
|
+
: N extends ScalarName
|
|
530
|
+
? ScalarOf<N>
|
|
531
|
+
: D extends { schema: infer S extends object }
|
|
532
|
+
? Infer<S, E>
|
|
533
|
+
: unknown;
|
|
534
|
+
|
|
535
|
+
type Nullable<T, D> = D extends { nullable: true } ? T | null : T;
|
|
536
|
+
|
|
537
|
+
type Optional<T, D> = D extends { required: false } ? T | null | undefined : T;
|
|
538
|
+
|
|
539
|
+
// `{ type: 'name', ... }`: the type, `| null` for `nullable: true`, and `|
|
|
540
|
+
// null | undefined` for `'?name'` or `required: false`.
|
|
541
|
+
type LongForm<T extends string, D, E> = T extends `?${infer N}`
|
|
542
|
+
? Nullable<TypeOf<N, D, E>, D> | null | undefined
|
|
543
|
+
: Optional<Nullable<TypeOf<T, D, E>, D>, D>;
|
|
544
|
+
|
|
545
|
+
type Shorthand<D, E> = D extends { array: unknown }
|
|
546
|
+
? LongForm<'array', D, E>
|
|
547
|
+
: D extends { set: unknown }
|
|
548
|
+
? LongForm<'set', D, E>
|
|
549
|
+
: D extends { object: object }
|
|
550
|
+
? LongForm<'object', D, E>
|
|
551
|
+
: D extends { map: object }
|
|
552
|
+
? LongForm<'map', D, E>
|
|
553
|
+
: D extends { enum: readonly unknown[] }
|
|
554
|
+
? LongForm<'enum', D, E>
|
|
555
|
+
: D extends { tuple: readonly unknown[] }
|
|
556
|
+
? LongForm<'tuple', D, E>
|
|
557
|
+
: D extends { union: readonly unknown[] }
|
|
558
|
+
? LongForm<'union', D, E>
|
|
559
|
+
: D extends { schema: object }
|
|
560
|
+
? LongForm<'schema', D, E>
|
|
561
|
+
: D extends { one: string }
|
|
562
|
+
? LongForm<'one', D, E>
|
|
563
|
+
: D extends { many: string }
|
|
564
|
+
? LongForm<'many', D, E>
|
|
565
|
+
: Struct<D, E>;
|
|
566
|
+
|
|
567
|
+
// What is not a field of a struct: a function (a calculated field, or the
|
|
568
|
+
// schema-level validate/parse/serialize/format) and an index definition.
|
|
569
|
+
type IsIndex<X> = X extends
|
|
570
|
+
| { index: readonly unknown[] }
|
|
571
|
+
| { primary: readonly unknown[] }
|
|
572
|
+
| { unique: readonly unknown[] }
|
|
573
|
+
? true
|
|
574
|
+
: false;
|
|
575
|
+
|
|
576
|
+
type IsField<X> = X extends (...args: never[]) => unknown
|
|
577
|
+
? false
|
|
578
|
+
: IsIndex<X> extends true
|
|
579
|
+
? false
|
|
580
|
+
: true;
|
|
581
|
+
|
|
582
|
+
type IsOptionalDef<X> = X extends `?${string}`
|
|
583
|
+
? true
|
|
584
|
+
: X extends { type: `?${string}` }
|
|
585
|
+
? true
|
|
586
|
+
: X extends { required: false }
|
|
587
|
+
? true
|
|
588
|
+
: false;
|
|
589
|
+
|
|
590
|
+
type FieldKeys<D> = { [K in keyof D]: IsField<D[K]> extends true ? K : never }[keyof D];
|
|
591
|
+
|
|
592
|
+
type OptionalKeys<D> = {
|
|
593
|
+
[K in FieldKeys<D>]: K extends `${string}?` ? K : IsOptionalDef<D[K]> extends true ? K : never;
|
|
594
|
+
}[FieldKeys<D>];
|
|
595
|
+
|
|
596
|
+
type RequiredKeys<D> = Exclude<FieldKeys<D>, OptionalKeys<D>>;
|
|
597
|
+
|
|
598
|
+
type FieldName<K> = K extends `${infer F}?` ? F : K;
|
|
599
|
+
|
|
600
|
+
type Simplify<T> = { [K in keyof T]: T[K] };
|
|
601
|
+
|
|
602
|
+
// The fields of a struct: optional ones (`'key?'`, `'?type'`, `required:
|
|
603
|
+
// false`) are optional properties that accept null and undefined, as `check`
|
|
604
|
+
// does.
|
|
605
|
+
type Struct<D, E> = Simplify<
|
|
606
|
+
{ -readonly [K in RequiredKeys<D>]-?: Infer<D[K], E> } & {
|
|
607
|
+
-readonly [K in OptionalKeys<D> as FieldName<K>]?: Infer<D[K], E> | null | undefined;
|
|
608
|
+
}
|
|
609
|
+
>;
|
|
610
|
+
|
|
611
|
+
// A projection copies fields from its parent, known only through `E`.
|
|
612
|
+
type Projection<D, E> = D extends {
|
|
613
|
+
Projection: { schema: infer S extends string; fields: readonly (infer F)[] };
|
|
614
|
+
}
|
|
615
|
+
? S extends keyof E
|
|
616
|
+
? Pick<Infer<E[S], E>, Extract<F, keyof Infer<E[S], E>>>
|
|
617
|
+
: unknown
|
|
618
|
+
: unknown;
|
|
619
|
+
|
|
620
|
+
type WithKind<D, K extends keyof D, E> = K extends 'Projection'
|
|
621
|
+
? Projection<D, E>
|
|
622
|
+
: Struct<Omit<D, K>, E>;
|
|
623
|
+
|
|
624
|
+
type ObjectDef<D, E> =
|
|
625
|
+
KindKey<D> extends infer K
|
|
626
|
+
? [K] extends [never]
|
|
627
|
+
? D extends { type: infer T extends string }
|
|
628
|
+
? LongForm<T, D, E>
|
|
629
|
+
: Shorthand<D, E>
|
|
630
|
+
: K extends keyof D
|
|
631
|
+
? WithKind<D, K, E>
|
|
632
|
+
: never
|
|
633
|
+
: never;
|
|
634
|
+
|
|
635
|
+
// The id field a stored kind adds, named after the entity.
|
|
636
|
+
type IdOf<Name extends string, Def> =
|
|
637
|
+
StoreOf<Def> extends 'persistent' ? { [K in `${Uncapitalize<Name>}Id`]?: string } : {};
|
|
638
|
+
|
|
639
|
+
// The type of a value `check` accepts for the definition `D`. `E` is an
|
|
640
|
+
// optional map of entity definitions by name (the second argument of `Model`
|
|
641
|
+
// as an object): with it a reference to a memory kind is the record and a
|
|
642
|
+
// projection has its parent's fields; without it a memory reference is
|
|
643
|
+
// `unknown` and a stored one is its id. A stored kind's own id field depends
|
|
644
|
+
// on the entity's name, which a definition does not carry: `InferEntity`
|
|
645
|
+
// adds it.
|
|
646
|
+
export type Infer<D, E = {}> =
|
|
647
|
+
IsAny<D> extends true
|
|
648
|
+
? any
|
|
649
|
+
: D extends string
|
|
650
|
+
? D extends `?${infer N}`
|
|
651
|
+
? Infer<N, E> | null | undefined
|
|
652
|
+
: IsCapitalized<D> extends true
|
|
653
|
+
? Referenced<D, undefined, E>
|
|
654
|
+
: ScalarOf<D>
|
|
655
|
+
: D extends Schema<infer X>
|
|
656
|
+
? Infer<X, E>
|
|
657
|
+
: D extends readonly unknown[]
|
|
658
|
+
? TupleOf<D, E>
|
|
659
|
+
: D extends object
|
|
660
|
+
? ObjectDef<D, E>
|
|
661
|
+
: unknown;
|
|
662
|
+
|
|
663
|
+
// The value type of a `Schema<D>` instance: `InferSchema<typeof schema>`.
|
|
664
|
+
export type InferSchema<S> = S extends Schema<infer D> ? Infer<D> : never;
|
|
665
|
+
|
|
666
|
+
// The value type of the entity `Name` of the entity map `E`, with the id
|
|
667
|
+
// field its kind adds (`accountId?: string` for a stored `Account`).
|
|
668
|
+
export type InferEntity<E, Name extends keyof E & string> = Simplify<
|
|
669
|
+
Infer<E[Name], E> & IdOf<Name, E[Name]>
|
|
670
|
+
>;
|
|
671
|
+
|
|
672
|
+
// Standard Schema v1 (https://standardschema.dev): the interface every schema
|
|
673
|
+
// exposes as `schema['~standard']`, so tRPC, TanStack Form, Hono and the other
|
|
674
|
+
// consumers of the specification accept it without an adapter. The shape is
|
|
675
|
+
// written out here because this file cannot import `@standard-schema/spec` (a
|
|
676
|
+
// devDependency); tests/types/standard.test-d.ts pins that a `Schema<D>` is a
|
|
677
|
+
// `StandardSchemaV1<Infer<D>, Infer<D>>` of that package.
|
|
678
|
+
|
|
679
|
+
// The vendor-specific options of a `validate` call are the options of `check`.
|
|
680
|
+
export interface StandardOptions {
|
|
681
|
+
readonly libraryOptions?: CheckOptions | undefined;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// A verdict, not a transformation: a valid value comes back as it was given,
|
|
685
|
+
// and the issues of an invalid one are the result's own (`message` and
|
|
686
|
+
// `path`, as the specification reads them, plus `code` and `params`).
|
|
687
|
+
export type StandardResult<T> =
|
|
688
|
+
| { readonly value: T; readonly issues?: undefined }
|
|
689
|
+
| { readonly issues: ReadonlyArray<ValidationIssue> };
|
|
690
|
+
|
|
691
|
+
export interface StandardProps<D = string | object> {
|
|
692
|
+
readonly version: 1;
|
|
693
|
+
readonly vendor: 'alexify.metaschema';
|
|
694
|
+
readonly validate: (value: unknown, options?: StandardOptions) => StandardResult<Infer<D>>;
|
|
695
|
+
// Nothing is transformed, so the input type is the output type. Type-level
|
|
696
|
+
// only: no property of the object holds it.
|
|
697
|
+
readonly types?: { readonly input: Infer<D>; readonly output: Infer<D> } | undefined;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// `D` is the definition the schema was built from, kept as a type only (no
|
|
701
|
+
// instance property holds it): `Schema.from({ name: 'string' })` is a
|
|
702
|
+
// `Schema<{ readonly name: 'string' }>`, and `InferSchema` reads `D` from it.
|
|
703
|
+
// The const type parameter keeps the literal types of an inline definition.
|
|
704
|
+
export class Schema<const D extends string | object = string | object> {
|
|
705
|
+
readonly '~definition'?: D;
|
|
706
|
+
// Standard Schema v1, built on first use; see `StandardProps`.
|
|
707
|
+
readonly '~standard': StandardProps<D>;
|
|
708
|
+
|
|
709
|
+
static from<const D extends string | object>(raw: D, namespaces?: Array<Model>): Schema<D>;
|
|
181
710
|
static extractSchema(def: object): Schema | null;
|
|
182
711
|
|
|
183
712
|
kind: Kind;
|
|
@@ -185,6 +714,9 @@ export class Schema {
|
|
|
185
714
|
store: Store;
|
|
186
715
|
allow: Allow;
|
|
187
716
|
parent: string;
|
|
717
|
+
// What `check` does with keys the schema does not have unless the call says
|
|
718
|
+
// otherwise: `{ Struct: { unknown: 'ignore' } }` sets it.
|
|
719
|
+
unknown: 'reject' | 'ignore';
|
|
188
720
|
indexes: Record<string, object>;
|
|
189
721
|
options: SchemaOptions;
|
|
190
722
|
custom: Record<string, unknown>;
|
|
@@ -195,18 +727,22 @@ export class Schema {
|
|
|
195
727
|
namespaces: Set<Model>;
|
|
196
728
|
references: Set<string>;
|
|
197
729
|
relations: Set<Relation>;
|
|
730
|
+
// Lint warnings of the definition, `Warning [code]: text`: unknown field
|
|
731
|
+
// options, a pattern without length.max, an index over a missing field.
|
|
732
|
+
// Computed on first use.
|
|
733
|
+
readonly warnings: Array<string>;
|
|
198
734
|
|
|
199
|
-
constructor(name: string, raw:
|
|
735
|
+
constructor(name: string, raw: D, namespaces?: Array<Model>);
|
|
200
736
|
get types(): TypeTable;
|
|
201
737
|
checkConsistency(): Array<string>;
|
|
202
738
|
findReference(name: string): Schema | null;
|
|
203
|
-
check(value: unknown,
|
|
739
|
+
check(value: unknown, options?: CheckOptions): ValidationResult;
|
|
204
740
|
toInterface(): string;
|
|
205
741
|
attach(...namespaces: Array<Model>): void;
|
|
206
742
|
detach(...namespaces: Array<Model>): void;
|
|
207
743
|
toString(): string;
|
|
208
744
|
toJSON(): object;
|
|
209
|
-
validate(value: unknown, path
|
|
745
|
+
validate(value: unknown, path?: string): ValidationResult | null;
|
|
210
746
|
}
|
|
211
747
|
|
|
212
748
|
export class Model {
|
|
@@ -214,7 +750,10 @@ export class Model {
|
|
|
214
750
|
entities: Map<string, Schema>;
|
|
215
751
|
database: Record<string, unknown> | null;
|
|
216
752
|
order: Set<string>;
|
|
217
|
-
warnings
|
|
753
|
+
// The warnings of every entity, plus references that do not resolve
|
|
754
|
+
// (missing-reference) and recursive dependencies (recursive-reference).
|
|
755
|
+
// Computed on first use.
|
|
756
|
+
readonly warnings: Array<string>;
|
|
218
757
|
|
|
219
758
|
constructor(
|
|
220
759
|
types: Record<string, TypeEntry>,
|