@alexify/metaschema 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +122 -1
- package/README.md +155 -79
- package/index.d.ts +377 -1
- package/package.json +17 -2
- package/src/codec.js +164 -33
- package/src/coerce/index.d.ts +34 -0
- package/src/coerce/index.js +352 -0
- package/src/context.js +6 -1
- package/src/derive/index.d.ts +86 -0
- package/src/derive/index.js +266 -0
- package/src/errors.js +23 -4
- package/src/formats/index.d.ts +20 -8
- package/src/formats/index.js +450 -39
- package/src/generators/fromjsonschema.js +7 -2
- package/src/generators/jsonschema.js +46 -14
- package/src/kinds.js +11 -2
- package/src/lint.js +24 -0
- package/src/metadata.js +4 -0
- package/src/model/data.js +17 -0
- package/src/model/merge.js +9 -6
- package/src/model/sample.js +2 -3
- package/src/model.js +29 -3
- package/src/mongodb/bson.js +118 -0
- package/src/mongodb/document.js +209 -0
- package/src/mongodb/index.d.ts +133 -0
- package/src/mongodb/index.js +24 -0
- package/src/mongodb/indexes.js +79 -0
- package/src/mongodb/update.js +258 -0
- package/src/prototypes/abstract.js +9 -3
- package/src/schema.js +36 -23
- package/src/types.js +20 -1
package/index.d.ts
CHANGED
|
@@ -142,8 +142,31 @@ export interface CheckOptions {
|
|
|
142
142
|
// does not have, which the output then leaves out.
|
|
143
143
|
export interface ParseOptions extends Omit<CheckOptions, 'unknown'> {
|
|
144
144
|
unknown?: 'reject' | 'ignore' | 'strip';
|
|
145
|
+
// Whether parse converts the input to the types of the fields before the
|
|
146
|
+
// check (`'42'` to a number, an ISO string to a Date): the schema's
|
|
147
|
+
// `coerce` metadata unless the call says otherwise, a field's own
|
|
148
|
+
// `coerce` key for its subtree. The converters are those `register()` of
|
|
149
|
+
// the `@alexify/metaschema/coerce` subpath installed; `check` never
|
|
150
|
+
// converts.
|
|
151
|
+
coerce?: boolean;
|
|
145
152
|
}
|
|
146
153
|
|
|
154
|
+
// The options of `coerce(schema, value)` of the `@alexify/metaschema/coerce`
|
|
155
|
+
// subpath: how references are followed and how deep, and an array given the
|
|
156
|
+
// path of every key the pass changed (a value converted, a default filled).
|
|
157
|
+
export interface CoerceOptions {
|
|
158
|
+
references?: CheckOptions['references'];
|
|
159
|
+
maxDepth?: number;
|
|
160
|
+
report?: PropertyKey[][];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// A converter of coercion: the value of a field of its type read from what
|
|
164
|
+
// the input gave, or the value itself when it cannot be read (the check then
|
|
165
|
+
// reports it as given). Registered process-wide by `register()` of the
|
|
166
|
+
// `@alexify/metaschema/coerce` subpath, or given to a model for its own type
|
|
167
|
+
// names (`ModelOptions.coercions`).
|
|
168
|
+
export type Coercion = (value: unknown, field: FieldType) => unknown;
|
|
169
|
+
|
|
147
170
|
// What parse returns: the output only for a valid value, the issues always.
|
|
148
171
|
export type ParseResult<T> =
|
|
149
172
|
| { valid: true; value: T; issues: [] }
|
|
@@ -243,6 +266,9 @@ export interface FieldType {
|
|
|
243
266
|
required: boolean;
|
|
244
267
|
// The value may be null; the key is still required unless `required` is false.
|
|
245
268
|
nullable?: boolean;
|
|
269
|
+
// Whether parse converts the input of this field and what is under it,
|
|
270
|
+
// whatever the policy of the call or the schema.
|
|
271
|
+
coerce?: boolean;
|
|
246
272
|
// On a reference: check and render it as the record (true) or its id
|
|
247
273
|
// (false) whatever the kind of its target.
|
|
248
274
|
embed?: boolean;
|
|
@@ -304,6 +330,10 @@ export interface ModelOptions {
|
|
|
304
330
|
// 'shared' (the default) registers the types into the process-wide registry;
|
|
305
331
|
// 'isolated' gives the model its own copy of the built-in types to register into.
|
|
306
332
|
registry?: 'shared' | 'isolated';
|
|
333
|
+
// The converters of coercion for the model's own type names, read before
|
|
334
|
+
// the process-wide table for the fields of its entities: what an isolated
|
|
335
|
+
// registry is to the types.
|
|
336
|
+
coercions?: Record<string, Coercion>;
|
|
307
337
|
}
|
|
308
338
|
|
|
309
339
|
export interface KindMetadata {
|
|
@@ -314,6 +344,8 @@ export interface KindMetadata {
|
|
|
314
344
|
parent?: string;
|
|
315
345
|
// The default of `check` for keys the schema does not have.
|
|
316
346
|
unknown?: 'reject' | 'ignore';
|
|
347
|
+
// The default of `parse` (and so of `~standard.validate`) for coercion.
|
|
348
|
+
coerce?: boolean;
|
|
317
349
|
[key: string]: unknown;
|
|
318
350
|
}
|
|
319
351
|
|
|
@@ -364,6 +396,14 @@ export type DefinitionErrorCode =
|
|
|
364
396
|
// test.
|
|
365
397
|
| 'ERR_INVALID_FORMAT'
|
|
366
398
|
| 'ERR_FORMAT_REGISTERED'
|
|
399
|
+
// Thrown by the functions of the `@alexify/metaschema/derive` subpath for
|
|
400
|
+
// a key the schema has no field of, located in the schema.
|
|
401
|
+
| 'ERR_UNKNOWN_FIELD'
|
|
402
|
+
// Thrown by `register` of the `@alexify/metaschema/coerce` subpath: an
|
|
403
|
+
// entry that is not a converter of a type, and a name registered again
|
|
404
|
+
// with another function.
|
|
405
|
+
| 'ERR_INVALID_COERCION'
|
|
406
|
+
| 'ERR_COERCION_REGISTERED'
|
|
367
407
|
// Thrown by `compile` and `register` of the `@alexify/metaschema/compile`
|
|
368
408
|
// subpath with `jit: true` where code generation from strings is not allowed.
|
|
369
409
|
| 'ERR_NO_EVAL'
|
|
@@ -386,7 +426,18 @@ export class SchemaDefinitionError extends TypeError {
|
|
|
386
426
|
// `ERR_INVALID_TARGET` is the same kind of error from a function of a subpath
|
|
387
427
|
// given something other than what it takes (a Schema, a Model, a snapshot, a
|
|
388
428
|
// list of changes).
|
|
389
|
-
export type RuntimeErrorCode =
|
|
429
|
+
export type RuntimeErrorCode =
|
|
430
|
+
| 'ERR_ASYNC_VALIDATOR'
|
|
431
|
+
| 'ERR_INVALID_TARGET'
|
|
432
|
+
// An option of a call that is not what it must be (`maxErrors`, the
|
|
433
|
+
// `coerce` of a parse, the options of the stream, derive and mongodb
|
|
434
|
+
// subpaths): the call is wrong, not the definition.
|
|
435
|
+
| 'ERR_INVALID_OPTION'
|
|
436
|
+
| 'ERR_INVALID_RECORD'
|
|
437
|
+
// A parse that coerces before `register()` of the `@alexify/metaschema/coerce`
|
|
438
|
+
// subpath ran (there is no converter to run), and a document written with
|
|
439
|
+
// ObjectIds before `register({ bson })` of `@alexify/metaschema/mongodb`.
|
|
440
|
+
| 'ERR_NOT_REGISTERED';
|
|
390
441
|
|
|
391
442
|
// ---------------------------------------------------------------------------
|
|
392
443
|
// The snapshot: `model.toJSON()`, what JSON.stringify(model) writes. The
|
|
@@ -510,6 +561,31 @@ export interface MergeOptions {
|
|
|
510
561
|
metadata?: Partial<KindMetadata>;
|
|
511
562
|
}
|
|
512
563
|
|
|
564
|
+
// The options of every function of the `@alexify/metaschema/derive` subpath.
|
|
565
|
+
export interface DeriveOptions {
|
|
566
|
+
// The name of the derived schema: the source's by default.
|
|
567
|
+
name?: string;
|
|
568
|
+
// The kind metadata of the derived schema, over the source's (a stored
|
|
569
|
+
// kind or a projection is a struct, keeping its `unknown`).
|
|
570
|
+
metadata?: Partial<KindMetadata>;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// The options of `partial`: the fields to make optional (every one by
|
|
574
|
+
// default), `deep` into nested structs, and `defaults` to keep the defaults
|
|
575
|
+
// a patch would otherwise fill.
|
|
576
|
+
export interface PartialOptions<K extends string = string> extends DeriveOptions {
|
|
577
|
+
keys?: readonly K[];
|
|
578
|
+
deep?: boolean;
|
|
579
|
+
defaults?: boolean;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// The options of `required`: the fields to make required (every one by
|
|
583
|
+
// default) and `deep` into nested structs.
|
|
584
|
+
export interface RequiredOptions<K extends string = string> extends DeriveOptions {
|
|
585
|
+
keys?: readonly K[];
|
|
586
|
+
deep?: boolean;
|
|
587
|
+
}
|
|
588
|
+
|
|
513
589
|
// The options of `infer` of the `@alexify/metaschema/model` subpath (the
|
|
514
590
|
// name sits beside the type `Infer<D>`: the function reads samples, the type
|
|
515
591
|
// reads a definition).
|
|
@@ -655,6 +731,11 @@ export interface JSONSchemaOptions {
|
|
|
655
731
|
// the default: a stored kind as an id, a memory kind as a `$ref` to its
|
|
656
732
|
// definition), every reference as the record ('embed') or as an id ('id').
|
|
657
733
|
references?: 'kind' | 'embed' | 'id';
|
|
734
|
+
// The mongodb target only: the ids of stored records as the database holds
|
|
735
|
+
// them ('objectId': `_id` an ObjectId, required at the root, and every
|
|
736
|
+
// reference an ObjectId, as `toDocument` of `@alexify/metaschema/mongodb`
|
|
737
|
+
// writes them), or as the program does ('string', the default).
|
|
738
|
+
ids?: 'string' | 'objectId';
|
|
658
739
|
// The pointer the `$ref` of a definition starts with, and where the
|
|
659
740
|
// definitions are placed in the document: '#/$defs/' for draft-2020-12,
|
|
660
741
|
// '#/definitions/' for draft-07, '#/components/schemas/' for openapi-3.0.
|
|
@@ -1147,6 +1228,253 @@ export type InferEntity<E, Name extends keyof E & string> = Simplify<
|
|
|
1147
1228
|
Infer<E[Name], E> & IdOf<Name, E[Name]>
|
|
1148
1229
|
>;
|
|
1149
1230
|
|
|
1231
|
+
// The definitions of derived schemas (the `@alexify/metaschema/derive`
|
|
1232
|
+
// subpath): what `pick`, `omit`, `partial`, `required` and `extend` build,
|
|
1233
|
+
// as a definition `Infer` reads. A derived definition always has a kind as
|
|
1234
|
+
// its first key, so its fields read as a struct whatever their names.
|
|
1235
|
+
|
|
1236
|
+
// The definition a schema type was built from.
|
|
1237
|
+
export type DefinitionOf<S> = S extends Schema<infer D> ? D : never;
|
|
1238
|
+
|
|
1239
|
+
// A schema whose definition is not known (`Schema`, an entity of a model):
|
|
1240
|
+
// what is derived from it is not known either.
|
|
1241
|
+
type IsWide<D> = string extends D ? true : object extends D ? true : false;
|
|
1242
|
+
|
|
1243
|
+
type IsShorthand<D> = D extends { array: unknown }
|
|
1244
|
+
? true
|
|
1245
|
+
: D extends
|
|
1246
|
+
| { set: unknown }
|
|
1247
|
+
| { object: object }
|
|
1248
|
+
| { map: object }
|
|
1249
|
+
| { enum: readonly unknown[] }
|
|
1250
|
+
| { tuple: readonly unknown[] }
|
|
1251
|
+
| { union: readonly unknown[] }
|
|
1252
|
+
| { schema: object }
|
|
1253
|
+
| { one: string }
|
|
1254
|
+
| { many: string }
|
|
1255
|
+
? true
|
|
1256
|
+
: false;
|
|
1257
|
+
|
|
1258
|
+
// Whether a definition is a struct, as `Infer` reads it.
|
|
1259
|
+
type IsStructDef<D> = D extends string
|
|
1260
|
+
? false
|
|
1261
|
+
: D extends readonly unknown[]
|
|
1262
|
+
? false
|
|
1263
|
+
: D extends Schema<infer X>
|
|
1264
|
+
? IsStructDef<X>
|
|
1265
|
+
: D extends object
|
|
1266
|
+
? [KindKey<D>] extends [never]
|
|
1267
|
+
? D extends { type: string }
|
|
1268
|
+
? false
|
|
1269
|
+
: IsShorthand<D> extends true
|
|
1270
|
+
? false
|
|
1271
|
+
: true
|
|
1272
|
+
: true
|
|
1273
|
+
: false;
|
|
1274
|
+
|
|
1275
|
+
// What a derive function takes: a struct, or a schema of an unknown one.
|
|
1276
|
+
export type StructGuardOf<D> =
|
|
1277
|
+
IsWide<D> extends true ? unknown : IsStructDef<D> extends true ? unknown : never;
|
|
1278
|
+
|
|
1279
|
+
type OptionKey = 'validate' | 'parse' | 'serialize' | 'format';
|
|
1280
|
+
|
|
1281
|
+
type Fn = (...args: never[]) => unknown;
|
|
1282
|
+
|
|
1283
|
+
// The keys of the fields of a struct definition, as written ('nick?').
|
|
1284
|
+
type FieldKeyOf<D> = Exclude<FieldKeys<D>, KindKey<D>>;
|
|
1285
|
+
|
|
1286
|
+
// The keys of its calculated fields.
|
|
1287
|
+
type CalculatedKeyOf<D> = {
|
|
1288
|
+
[K in keyof D]: D[K] extends Fn ? (K extends OptionKey ? never : K) : never;
|
|
1289
|
+
}[keyof D];
|
|
1290
|
+
|
|
1291
|
+
// The names a derive function takes: the fields, the calculated fields and,
|
|
1292
|
+
// for a stored kind, its id (named after the entity, which the definition
|
|
1293
|
+
// does not carry).
|
|
1294
|
+
export type DeriveKeyOf<D> =
|
|
1295
|
+
IsWide<D> extends true
|
|
1296
|
+
? string
|
|
1297
|
+
: Extract<
|
|
1298
|
+
| FieldName<FieldKeyOf<D>>
|
|
1299
|
+
| CalculatedKeyOf<D>
|
|
1300
|
+
| (StoreOf<D> extends 'persistent' ? `${string}Id` | 'id' : never),
|
|
1301
|
+
string
|
|
1302
|
+
>;
|
|
1303
|
+
|
|
1304
|
+
type KindName<D> = [KindKey<D>] extends [never] ? 'Struct' : KindKey<D>;
|
|
1305
|
+
|
|
1306
|
+
// The kind of a derived definition: the one of the options, else a struct
|
|
1307
|
+
// for a kindless definition, a stored kind and a projection, else the
|
|
1308
|
+
// source's kind with its metadata.
|
|
1309
|
+
type DerivedKind<D, O> = O extends { metadata: { kind: infer K extends string } }
|
|
1310
|
+
? { [P in Capitalize<K>]: {} }
|
|
1311
|
+
: KindName<D> extends 'Struct' | 'Projection'
|
|
1312
|
+
? { Struct: {} }
|
|
1313
|
+
: Lowercase<KindName<D> & string> extends StoredKind
|
|
1314
|
+
? { Struct: {} }
|
|
1315
|
+
: { [P in KindName<D> & keyof D]: D[P] };
|
|
1316
|
+
|
|
1317
|
+
// A derived schema: of an unknown definition when the source's is unknown.
|
|
1318
|
+
export type DerivedSchema<D, R> = IsWide<D> extends true ? Schema : Schema<Simplify<R>>;
|
|
1319
|
+
|
|
1320
|
+
type Keep<D, K> = {
|
|
1321
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1322
|
+
? FieldName<P> extends K
|
|
1323
|
+
? P
|
|
1324
|
+
: never
|
|
1325
|
+
: P extends CalculatedKeyOf<D>
|
|
1326
|
+
? P extends K
|
|
1327
|
+
? P
|
|
1328
|
+
: never
|
|
1329
|
+
: never]: D[P];
|
|
1330
|
+
};
|
|
1331
|
+
|
|
1332
|
+
type Drop<D, K> = {
|
|
1333
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1334
|
+
? FieldName<P> extends K
|
|
1335
|
+
? never
|
|
1336
|
+
: P
|
|
1337
|
+
: P extends CalculatedKeyOf<D>
|
|
1338
|
+
? P extends K
|
|
1339
|
+
? never
|
|
1340
|
+
: P
|
|
1341
|
+
: never]: D[P];
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1344
|
+
export type PickDefinition<D, K, O = {}> = DerivedKind<D, O> & Keep<D, K>;
|
|
1345
|
+
|
|
1346
|
+
export type OmitDefinition<D, K, O = {}> = DerivedKind<D, O> & Drop<D, K>;
|
|
1347
|
+
|
|
1348
|
+
type KeysOf<O, D> = O extends { keys: readonly (infer K)[] } ? K : FieldName<FieldKeyOf<D>>;
|
|
1349
|
+
|
|
1350
|
+
type IsDeep<O> = O extends { deep: true } ? true : false;
|
|
1351
|
+
|
|
1352
|
+
// The fields of a nested struct as a definition of their own, in each form
|
|
1353
|
+
// a field may hold one in; never for anything else.
|
|
1354
|
+
type NestedOf<X> =
|
|
1355
|
+
X extends Schema<infer S>
|
|
1356
|
+
? IsStructDef<S> extends true
|
|
1357
|
+
? S
|
|
1358
|
+
: never
|
|
1359
|
+
: X extends { schema: infer S }
|
|
1360
|
+
? IsStructDef<S> extends true
|
|
1361
|
+
? S
|
|
1362
|
+
: never
|
|
1363
|
+
: IsStructDef<X> extends true
|
|
1364
|
+
? Omit<X, KindKey<X>>
|
|
1365
|
+
: never;
|
|
1366
|
+
|
|
1367
|
+
// A field holding a nested struct with the fields `F` in its place.
|
|
1368
|
+
type Reshaped<X, F> =
|
|
1369
|
+
X extends Schema<any>
|
|
1370
|
+
? F
|
|
1371
|
+
: X extends { schema: unknown }
|
|
1372
|
+
? Omit<X, 'schema' | 'validate' | 'parse' | 'serialize'> & { schema: F }
|
|
1373
|
+
: { [K in KindKey<X> & keyof X]: X[K] } & F;
|
|
1374
|
+
|
|
1375
|
+
type NoDefault<X, O> = O extends { defaults: true }
|
|
1376
|
+
? X
|
|
1377
|
+
: IsStructDef<X> extends true
|
|
1378
|
+
? X
|
|
1379
|
+
: Omit<X, 'default'>;
|
|
1380
|
+
|
|
1381
|
+
type OptionalDef<X, O> = X extends string
|
|
1382
|
+
? X
|
|
1383
|
+
: IsDeep<O> extends true
|
|
1384
|
+
? [NestedOf<X>] extends [never]
|
|
1385
|
+
? NoDefault<X, O>
|
|
1386
|
+
: Simplify<Reshaped<NoDefault<X, O>, PartialFields<NestedOf<X>, O>>>
|
|
1387
|
+
: NoDefault<X, O>;
|
|
1388
|
+
|
|
1389
|
+
type PartialFields<D, O> = {
|
|
1390
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1391
|
+
? `${FieldName<P> & string}?`
|
|
1392
|
+
: P extends CalculatedKeyOf<D>
|
|
1393
|
+
? P
|
|
1394
|
+
: never]: P extends FieldKeyOf<D> ? OptionalDef<D[P], O> : D[P];
|
|
1395
|
+
};
|
|
1396
|
+
|
|
1397
|
+
type PartialOf<D, O> = {
|
|
1398
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1399
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1400
|
+
? `${FieldName<P> & string}?`
|
|
1401
|
+
: P
|
|
1402
|
+
: P extends CalculatedKeyOf<D>
|
|
1403
|
+
? P
|
|
1404
|
+
: never]: P extends FieldKeyOf<D>
|
|
1405
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1406
|
+
? OptionalDef<D[P], O>
|
|
1407
|
+
: D[P]
|
|
1408
|
+
: D[P];
|
|
1409
|
+
};
|
|
1410
|
+
|
|
1411
|
+
export type PartialDefinition<D, O = {}> = DerivedKind<D, O> & PartialOf<D, O>;
|
|
1412
|
+
|
|
1413
|
+
type RequiredDef<X, O> = X extends `?${infer N}`
|
|
1414
|
+
? N
|
|
1415
|
+
: X extends string
|
|
1416
|
+
? X
|
|
1417
|
+
: Simplify<
|
|
1418
|
+
(X extends { type: `?${infer N}` }
|
|
1419
|
+
? Omit<X, 'type' | 'required'> & { type: N }
|
|
1420
|
+
: IsStructDef<X> extends true
|
|
1421
|
+
? X
|
|
1422
|
+
: Omit<X, 'required'>) extends infer Y
|
|
1423
|
+
? IsDeep<O> extends true
|
|
1424
|
+
? [NestedOf<X>] extends [never]
|
|
1425
|
+
? Y
|
|
1426
|
+
: X extends { schema: unknown }
|
|
1427
|
+
? Omit<Y, 'schema'> & { schema: RequiredFields<NestedOf<X>, O> }
|
|
1428
|
+
: X extends Schema<any>
|
|
1429
|
+
? RequiredFields<NestedOf<X>, O>
|
|
1430
|
+
: { [K in KindKey<X> & keyof X]: X[K] } & RequiredFields<NestedOf<X>, O>
|
|
1431
|
+
: Y
|
|
1432
|
+
: never
|
|
1433
|
+
>;
|
|
1434
|
+
|
|
1435
|
+
type RequiredFields<D, O> = {
|
|
1436
|
+
[P in keyof D as P extends FieldKeyOf<D> ? FieldName<P> : P]: P extends FieldKeyOf<D>
|
|
1437
|
+
? RequiredDef<D[P], O>
|
|
1438
|
+
: D[P];
|
|
1439
|
+
};
|
|
1440
|
+
|
|
1441
|
+
type RequiredOf<D, O> = {
|
|
1442
|
+
[P in keyof D as P extends FieldKeyOf<D>
|
|
1443
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1444
|
+
? FieldName<P>
|
|
1445
|
+
: P
|
|
1446
|
+
: P extends CalculatedKeyOf<D> | OptionKey
|
|
1447
|
+
? P
|
|
1448
|
+
: never]: P extends FieldKeyOf<D>
|
|
1449
|
+
? FieldName<P> extends KeysOf<O, D>
|
|
1450
|
+
? RequiredDef<D[P], O>
|
|
1451
|
+
: D[P]
|
|
1452
|
+
: D[P];
|
|
1453
|
+
};
|
|
1454
|
+
|
|
1455
|
+
export type RequiredDefinition<D, O = {}> = DerivedKind<D, O> & RequiredOf<D, O>;
|
|
1456
|
+
|
|
1457
|
+
// What an extension adds: its fields (a Schema's, without its kind).
|
|
1458
|
+
type ExtensionOf<X> = X extends Schema<infer S> ? Omit<S, KindKey<S>> : X;
|
|
1459
|
+
|
|
1460
|
+
// An extension whose fields are not known (a `Schema`, an entity of a
|
|
1461
|
+
// model): what it extends is not known either.
|
|
1462
|
+
export type ExtendedSchema<D, X, R> =
|
|
1463
|
+
X extends Schema<infer S> ? (IsWide<S> extends true ? Schema : DerivedSchema<D, R>) : DerivedSchema<D, R>;
|
|
1464
|
+
|
|
1465
|
+
export type ExtendDefinition<D, X, O = {}> = DerivedKind<D, O> & {
|
|
1466
|
+
[P in keyof D as P extends KindKey<D>
|
|
1467
|
+
? never
|
|
1468
|
+
: P extends FieldKeyOf<D>
|
|
1469
|
+
? FieldName<P> extends FieldName<keyof ExtensionOf<X>>
|
|
1470
|
+
? never
|
|
1471
|
+
: P
|
|
1472
|
+
: P]: D[P];
|
|
1473
|
+
} & ExtensionOf<X>;
|
|
1474
|
+
|
|
1475
|
+
// The names of the fields of a struct definition, as a definition of an enum.
|
|
1476
|
+
export type KeyofDefinition<D> = { enum: Array<FieldName<FieldKeyOf<D>>> };
|
|
1477
|
+
|
|
1150
1478
|
// Standard Schema v1 (https://standardschema.dev): the interface every schema
|
|
1151
1479
|
// exposes as `schema['~standard']`, so tRPC, TanStack Form, Hono and the other
|
|
1152
1480
|
// consumers of the specification accept it without an adapter. The shape is
|
|
@@ -1262,6 +1590,51 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
1262
1590
|
// of the `@alexify/metaschema/model` subpath has run; its `sample(schema,
|
|
1263
1591
|
// options)` is the same without it.
|
|
1264
1592
|
sample(options?: SampleOptions): Infer<D>;
|
|
1593
|
+
// The value with the conversions of the schema and its defaults applied,
|
|
1594
|
+
// nothing checked: exists once `register()` of the
|
|
1595
|
+
// `@alexify/metaschema/coerce` subpath has run; its `coerce(schema,
|
|
1596
|
+
// value)` is the same without it.
|
|
1597
|
+
coerce(value: unknown, options?: CoerceOptions): unknown;
|
|
1598
|
+
// The six members below exist once `register()` of the
|
|
1599
|
+
// `@alexify/metaschema/derive` subpath has run; its `pick(schema, keys)`
|
|
1600
|
+
// and kin are the same without it. They are typed through `this`, never
|
|
1601
|
+
// through `D`: a member that maps `D` makes every comparison of two Schema
|
|
1602
|
+
// types instantiate the mapping ("excessively deep").
|
|
1603
|
+
pick<
|
|
1604
|
+
S extends Schema,
|
|
1605
|
+
const K extends readonly DeriveKeyOf<DefinitionOf<S>>[],
|
|
1606
|
+
const O extends DeriveOptions = {},
|
|
1607
|
+
>(
|
|
1608
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1609
|
+
keys: K,
|
|
1610
|
+
options?: O,
|
|
1611
|
+
): DerivedSchema<DefinitionOf<S>, PickDefinition<DefinitionOf<S>, K[number], O>>;
|
|
1612
|
+
omit<
|
|
1613
|
+
S extends Schema,
|
|
1614
|
+
const K extends readonly DeriveKeyOf<DefinitionOf<S>>[],
|
|
1615
|
+
const O extends DeriveOptions = {},
|
|
1616
|
+
>(
|
|
1617
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1618
|
+
keys: K,
|
|
1619
|
+
options?: O,
|
|
1620
|
+
): DerivedSchema<DefinitionOf<S>, OmitDefinition<DefinitionOf<S>, K[number], O>>;
|
|
1621
|
+
partial<S extends Schema, const O extends PartialOptions<DeriveKeyOf<DefinitionOf<S>>> = {}>(
|
|
1622
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1623
|
+
options?: O,
|
|
1624
|
+
): DerivedSchema<DefinitionOf<S>, PartialDefinition<DefinitionOf<S>, O>>;
|
|
1625
|
+
required<S extends Schema, const O extends RequiredOptions<DeriveKeyOf<DefinitionOf<S>>> = {}>(
|
|
1626
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1627
|
+
options?: O,
|
|
1628
|
+
): DerivedSchema<DefinitionOf<S>, RequiredDefinition<DefinitionOf<S>, O>>;
|
|
1629
|
+
extend<S extends Schema, const X extends object, const O extends DeriveOptions = {}>(
|
|
1630
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1631
|
+
extension: X,
|
|
1632
|
+
options?: O,
|
|
1633
|
+
): ExtendedSchema<DefinitionOf<S>, X, ExtendDefinition<DefinitionOf<S>, X, O>>;
|
|
1634
|
+
keyof<S extends Schema>(
|
|
1635
|
+
this: S & StructGuardOf<DefinitionOf<S>>,
|
|
1636
|
+
options?: DeriveOptions,
|
|
1637
|
+
): DerivedSchema<DefinitionOf<S>, KeyofDefinition<DefinitionOf<S>>>;
|
|
1265
1638
|
// The five members below exist once `register()` of the
|
|
1266
1639
|
// `@alexify/metaschema/stream` subpath has run; its `checkEach(schema,
|
|
1267
1640
|
// records, options)` and kin are the same without it.
|
|
@@ -1299,6 +1672,9 @@ export class Schema<const D extends string | object = string | object> {
|
|
|
1299
1672
|
|
|
1300
1673
|
export class Model {
|
|
1301
1674
|
types: TypeTable;
|
|
1675
|
+
// The converters of coercion the model was given (`ModelOptions.coercions`),
|
|
1676
|
+
// frozen, or null.
|
|
1677
|
+
readonly coercions: Readonly<Record<string, Coercion>> | null;
|
|
1302
1678
|
entities: Map<string, Schema>;
|
|
1303
1679
|
database: Record<string, unknown> | null;
|
|
1304
1680
|
order: Set<string>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexify/metaschema",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "Fast, zero-dependency metadata schema and interface definition language (IDL) for Node.js and browsers — validation and parsing with a JIT backend, domain models, TypeScript and JSON Schema",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alex Dolid <dolid.sasha@gmail.com>",
|
|
@@ -54,6 +54,18 @@
|
|
|
54
54
|
"./formats": {
|
|
55
55
|
"types": "./src/formats/index.d.ts",
|
|
56
56
|
"default": "./src/formats/index.js"
|
|
57
|
+
},
|
|
58
|
+
"./derive": {
|
|
59
|
+
"types": "./src/derive/index.d.ts",
|
|
60
|
+
"default": "./src/derive/index.js"
|
|
61
|
+
},
|
|
62
|
+
"./coerce": {
|
|
63
|
+
"types": "./src/coerce/index.d.ts",
|
|
64
|
+
"default": "./src/coerce/index.js"
|
|
65
|
+
},
|
|
66
|
+
"./mongodb": {
|
|
67
|
+
"types": "./src/mongodb/index.d.ts",
|
|
68
|
+
"default": "./src/mongodb/index.js"
|
|
57
69
|
}
|
|
58
70
|
},
|
|
59
71
|
"directories": {
|
|
@@ -77,6 +89,8 @@
|
|
|
77
89
|
"directory": "tests/types"
|
|
78
90
|
},
|
|
79
91
|
"keywords": [
|
|
92
|
+
"mongodb",
|
|
93
|
+
"bson",
|
|
80
94
|
"schema",
|
|
81
95
|
"metaschema",
|
|
82
96
|
"metadata",
|
|
@@ -106,6 +120,7 @@
|
|
|
106
120
|
"ajv": "^8.20.0",
|
|
107
121
|
"ajv-formats": "^3.0.1",
|
|
108
122
|
"arktype": "^2.2.8",
|
|
123
|
+
"bson": "7.3.3",
|
|
109
124
|
"c8": "^12.0.0",
|
|
110
125
|
"cytoscape": "^3.34.3",
|
|
111
126
|
"cytoscape-cose-bilkent": "^4.1.0",
|
|
@@ -135,7 +150,7 @@
|
|
|
135
150
|
"test:nojit": "node --disallow-code-generation-from-strings --require ./tests/helpers/jit.js --test tests/unit/*.test.js",
|
|
136
151
|
"test:coverage": "c8 --all --include 'src/**' --check-coverage --lines 98 --statements 98 --branches 90 --functions 100 --reporter text --reporter lcov node --test tests/unit/*.test.js",
|
|
137
152
|
"test:types": "tsd",
|
|
138
|
-
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts src/locales/en.d.ts src/locales/uk.d.ts src/generators/index.d.ts src/compile/index.d.ts src/model/index.d.ts src/stream/index.d.ts src/formats/index.d.ts",
|
|
153
|
+
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts src/locales/en.d.ts src/locales/uk.d.ts src/generators/index.d.ts src/compile/index.d.ts src/model/index.d.ts src/stream/index.d.ts src/formats/index.d.ts src/derive/index.d.ts src/coerce/index.d.ts src/mongodb/index.d.ts",
|
|
139
154
|
"lint": "oxlint index.js browser.js src tests scripts bench",
|
|
140
155
|
"format": "oxfmt index.js browser.js src tests scripts bench",
|
|
141
156
|
"format:check": "oxfmt --check index.js browser.js src tests scripts bench",
|