@alexify/metaschema 3.3.0 → 3.4.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +186 -1
  2. package/README.md +24 -16
  3. package/SECURITY.md +24 -1
  4. package/index.d.ts +224 -28
  5. package/package.json +13 -3
  6. package/src/checks.js +789 -0
  7. package/src/codec.js +37 -13
  8. package/src/coerce/index.js +34 -1
  9. package/src/compile/emit.js +87 -28
  10. package/src/compile/index.js +9 -4
  11. package/src/definition/index.d.ts +19 -0
  12. package/src/definition/index.js +27 -0
  13. package/src/derive/index.js +15 -8
  14. package/src/errors.js +23 -0
  15. package/src/generators/fromjsonschema.js +14 -0
  16. package/src/generators/index.js +1 -1
  17. package/src/generators/jsonschema.js +44 -7
  18. package/src/generators/typescript.js +11 -0
  19. package/src/index.js +3 -2
  20. package/src/issues.js +9 -7
  21. package/src/kinds.js +4 -1
  22. package/src/lint.js +15 -1
  23. package/src/locales/en.js +6 -0
  24. package/src/locales/uk.js +6 -0
  25. package/src/metadata.js +2 -24
  26. package/src/model/data.js +50 -10
  27. package/src/model/diff.js +7 -4
  28. package/src/model/index.js +1 -1
  29. package/src/model/infer.js +10 -3
  30. package/src/model/merge.js +7 -3
  31. package/src/model/restore.js +52 -7
  32. package/src/model/sample.js +26 -1
  33. package/src/model.js +25 -53
  34. package/src/mongodb/bson.js +15 -3
  35. package/src/mongodb/document.js +10 -2
  36. package/src/preprocessor.js +1 -2
  37. package/src/prototypes/abstract.js +32 -64
  38. package/src/prototypes/collections.js +3 -90
  39. package/src/prototypes/flags.js +34 -0
  40. package/src/prototypes/json.js +0 -11
  41. package/src/prototypes/reference.js +0 -76
  42. package/src/prototypes/scalars.js +17 -96
  43. package/src/prototypes/schema.js +1 -10
  44. package/src/prototypes/tuple.js +0 -26
  45. package/src/prototypes/union.js +12 -82
  46. package/src/rules.js +33 -157
  47. package/src/schema.js +105 -233
  48. package/src/snapshot.js +216 -41
  49. package/src/stream/index.js +1 -1
  50. package/src/struct.js +6 -91
  51. package/src/types.js +24 -6
  52. package/src/util.js +43 -0
  53. package/src/validation.js +313 -0
package/index.d.ts CHANGED
@@ -38,6 +38,7 @@ export type IssueCode =
38
38
  | 'range'
39
39
  | 'pattern'
40
40
  | 'format'
41
+ | 'decimals'
41
42
  | 'union'
42
43
  | 'reference'
43
44
  | 'circular'
@@ -59,6 +60,9 @@ export interface IssueParams {
59
60
  pattern: { pattern: string };
60
61
  // The name of the format the string is not of.
61
62
  format: { format: string };
63
+ // The decimals the number may have: it has more, or it is not finite
64
+ // (`received` names NaN or the infinity then).
65
+ decimals: { decimals: number; received?: string };
62
66
  // No branch of a union matched: the branch names, or the discriminator
63
67
  // values when the union has a discriminator (the path then ends with it).
64
68
  union: { expected: unknown[]; discriminator: string | undefined };
@@ -314,7 +318,12 @@ export interface TypeMetadata {
314
318
  }
315
319
 
316
320
  // An entry of the table passed to Model: metadata for a built-in, an alias
317
- // (`js`), or a prototype with its own `construct` and `checkType`.
321
+ // (`js`), or a prototype with its own `construct` and `checkType`. A
322
+ // prototype may bring a `compile` in place of `checkType` (it returns the
323
+ // check, `(value, context, key) => void`): it runs on the first check of a
324
+ // field, not when the schema is built, so it must not be where the
325
+ // definition is checked (`construct` is); what it throws is
326
+ // ERR_INVALID_CUSTOM_TYPE with the error as the cause.
318
327
  export interface TypeEntry {
319
328
  js?: string;
320
329
  metadata?: TypeMetadata;
@@ -440,12 +449,20 @@ export type RuntimeErrorCode =
440
449
  | 'ERR_NOT_REGISTERED';
441
450
 
442
451
  // ---------------------------------------------------------------------------
443
- // The snapshot: `model.toJSON()`, what JSON.stringify(model) writes. The
444
- // entities in dependency order with a fixed key order, the types the
445
- // entities use, and the `database` metadata as given. A function is
446
- // `{ $function: true }` wherever it stands, a bigint `{ $bigint }`, a Date
447
- // `{ $date }`, a RegExp its source (or `{ source, flags }` with flags beyond
448
- // `u`). `fromJSON` of the `@alexify/metaschema/model` subpath reads it back.
452
+ // The snapshot: `model.toJSON()`, what JSON.stringify(model) writes, and
453
+ // `schema.toSnapshot()`, the same for one schema and the entities it
454
+ // reaches. The entities in dependency order with a fixed key order, the
455
+ // types the entities use, and a copy of the `database` metadata. A function
456
+ // is `{ $function: true }` wherever it stands, a bigint `{ $bigint }`, a
457
+ // Date `{ $date }`, bytes `{ $bytes }` (base64), a Map `{ $map }` (its
458
+ // entries), a Set `{ $set }` (its values), a RegExp its source (or
459
+ // `{ source, flags }` with flags beyond `u`). `fromJSON` of the `@alexify/metaschema/model` subpath reads
460
+ // it back. Format 1 is a contract: the fields of a struct come in the order
461
+ // of the definition (the id field a stored kind adds after them, unless the
462
+ // definition has a field of that name), the entities each after the ones
463
+ // they reference (`model.order` for a model, the order a walk from the
464
+ // schema meets them for one schema), and a key is only ever added to it;
465
+ // anything else is a major release.
449
466
  // ---------------------------------------------------------------------------
450
467
 
451
468
  export interface FunctionSnapshot {
@@ -453,14 +470,170 @@ export interface FunctionSnapshot {
453
470
  }
454
471
 
455
472
  // A field of a snapshot: its type and whether it is required first, then
456
- // the other keys of its definition, sorted (a nested struct under `schema`,
457
- // an element under `value`, branches under `union`).
458
- export interface FieldSnapshot {
459
- type: string;
473
+ // the other keys of its definition, sorted. The type decides what else it
474
+ // holds (narrow on `type`): the rules of its type, the element of a
475
+ // collection, the elements of a tuple, the nested struct, the branches of a
476
+ // union, the entity of a reference (its name is the type, capitalized), or
477
+ // the options of a custom type, whose names are those `CustomTypes` declares
478
+ // (as `Infer` reads them). Any field may carry the keys of
479
+ // `FieldSnapshotBase`; a key the definition gave beyond them is kept too.
480
+ export interface FieldSnapshotBase {
460
481
  required: boolean;
482
+ nullable?: boolean;
483
+ coerce?: boolean;
484
+ // A value, `{ $function: true }` for a factory, `{ $bigint }`, `{ $date }`,
485
+ // `{ $bytes }`, `{ $map }`, `{ $set }` for the values that have no JSON form.
486
+ default?: unknown;
487
+ aliases?: string[];
488
+ deprecated?: boolean | string;
489
+ description?: string;
490
+ examples?: unknown[];
491
+ title?: string;
492
+ validate?: FunctionSnapshot;
493
+ parse?: FunctionSnapshot;
494
+ serialize?: FunctionSnapshot;
461
495
  [key: string]: unknown;
462
496
  }
463
497
 
498
+ export interface BigintSnapshot {
499
+ $bigint: string;
500
+ }
501
+
502
+ export interface DateSnapshot {
503
+ $date: string;
504
+ }
505
+
506
+ // Bytes as base64 text, a Map as its entries and a Set as its values, each
507
+ // value of them written as any value of a snapshot is.
508
+ export interface BytesSnapshot {
509
+ $bytes: string;
510
+ }
511
+
512
+ export interface MapSnapshot {
513
+ $map: [unknown, unknown][];
514
+ }
515
+
516
+ export interface SetSnapshot {
517
+ $set: unknown[];
518
+ }
519
+
520
+ // A `length` rule: the bounds it has.
521
+ export interface LengthSnapshot {
522
+ min?: number;
523
+ max?: number;
524
+ }
525
+
526
+ export interface StringFieldSnapshot extends FieldSnapshotBase {
527
+ type: 'string';
528
+ length?: LengthSnapshot;
529
+ unicode?: boolean;
530
+ // The source of a pattern of the `u` flag alone, else its source and flags.
531
+ pattern?: string | { source: string; flags: string };
532
+ format?: string;
533
+ }
534
+
535
+ export interface NumberFieldSnapshot extends FieldSnapshotBase {
536
+ type: 'number';
537
+ min?: number | BigintSnapshot;
538
+ max?: number | BigintSnapshot;
539
+ decimals?: number;
540
+ }
541
+
542
+ export interface IntegerFieldSnapshot extends FieldSnapshotBase {
543
+ type: 'integer' | 'bigint';
544
+ min?: number | BigintSnapshot;
545
+ max?: number | BigintSnapshot;
546
+ }
547
+
548
+ export interface ScalarFieldSnapshot extends FieldSnapshotBase {
549
+ type: 'boolean' | 'date' | 'null' | 'any' | 'unknown' | 'json';
550
+ }
551
+
552
+ // Binary data, its `length` the number of bytes.
553
+ export interface BytesFieldSnapshot extends FieldSnapshotBase {
554
+ type: 'bytes';
555
+ length?: LengthSnapshot;
556
+ }
557
+
558
+ export interface EnumFieldSnapshot extends FieldSnapshotBase {
559
+ type: 'enum';
560
+ enum: unknown[];
561
+ }
562
+
563
+ export interface FlagsFieldSnapshot extends FieldSnapshotBase {
564
+ type: 'flags';
565
+ flags: string[];
566
+ }
567
+
568
+ export interface ListFieldSnapshot extends FieldSnapshotBase {
569
+ type: 'array' | 'set';
570
+ value: FieldSnapshot;
571
+ length?: LengthSnapshot;
572
+ }
573
+
574
+ export interface KeyedFieldSnapshot extends FieldSnapshotBase {
575
+ type: 'object' | 'map';
576
+ key: 'string' | 'number';
577
+ value: FieldSnapshot;
578
+ length?: LengthSnapshot;
579
+ }
580
+
581
+ // An element of a tuple, `name` the one a named element was given.
582
+ export type TupleElementSnapshot = FieldSnapshot & { name?: string };
583
+
584
+ export interface TupleFieldSnapshot extends FieldSnapshotBase {
585
+ type: 'tuple';
586
+ value: TupleElementSnapshot[];
587
+ }
588
+
589
+ // A nested struct, or a Schema of one type held as a field.
590
+ export interface StructFieldSnapshot extends FieldSnapshotBase {
591
+ type: 'schema';
592
+ schema: FieldsSnapshot | FieldSnapshot;
593
+ }
594
+
595
+ export interface UnionFieldSnapshot extends FieldSnapshotBase {
596
+ type: 'union';
597
+ union: FieldSnapshot[];
598
+ discriminator?: string;
599
+ }
600
+
601
+ type UpperLetter =
602
+ | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M'
603
+ | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
604
+
605
+ // A reference: the type is the name of the entity, under `one` or `many`.
606
+ export interface ReferenceFieldSnapshot extends FieldSnapshotBase {
607
+ type: `${UpperLetter}${string}`;
608
+ one?: string;
609
+ many?: string;
610
+ embed?: boolean;
611
+ }
612
+
613
+ // A field of a custom type, by the names `CustomTypes` declares: a snapshot
614
+ // of a model whose custom types are not declared there holds fields of type
615
+ // names no member of `FieldSnapshot` has, which an exhaustive `switch` over
616
+ // `type` does not see; give it a `default`.
617
+ export interface CustomFieldSnapshot extends FieldSnapshotBase {
618
+ type: keyof CustomTypes & string;
619
+ }
620
+
621
+ export type FieldSnapshot =
622
+ | StringFieldSnapshot
623
+ | NumberFieldSnapshot
624
+ | IntegerFieldSnapshot
625
+ | ScalarFieldSnapshot
626
+ | BytesFieldSnapshot
627
+ | EnumFieldSnapshot
628
+ | FlagsFieldSnapshot
629
+ | ListFieldSnapshot
630
+ | KeyedFieldSnapshot
631
+ | TupleFieldSnapshot
632
+ | StructFieldSnapshot
633
+ | UnionFieldSnapshot
634
+ | ReferenceFieldSnapshot
635
+ | CustomFieldSnapshot;
636
+
464
637
  // The fields of a struct in their order: a calculated field, the validate,
465
638
  // parse or serialize of the struct are `{ $function: true }`.
466
639
  export type FieldsSnapshot = Record<string, FieldSnapshot | FunctionSnapshot>;
@@ -495,11 +668,21 @@ export interface TypeSnapshot {
495
668
 
496
669
  export interface ModelSnapshot {
497
670
  format: 1;
671
+ // The entity a snapshot of one schema is about (`schema.toSnapshot()`);
672
+ // a model's snapshot has none.
673
+ root?: string;
498
674
  database: Record<string, unknown> | null;
499
675
  types: Record<string, TypeSnapshot>;
500
676
  entities: Record<string, EntitySnapshot>;
501
677
  }
502
678
 
679
+ // `schema.toSnapshot()`: the schema (under its name, '' when anonymous)
680
+ // and every entity it reaches through its references, in dependency order.
681
+ export interface SchemaSnapshot extends ModelSnapshot {
682
+ root: string;
683
+ database: null;
684
+ }
685
+
503
686
  // The options of `fromJSON` of the `@alexify/metaschema/model` subpath.
504
687
  export interface RestoreOptions {
505
688
  // Entries of the table Model takes, over the types of the snapshot: the
@@ -821,6 +1004,7 @@ type ScalarName =
821
1004
  | 'bigint'
822
1005
  | 'boolean'
823
1006
  | 'date'
1007
+ | 'bytes'
824
1008
  | 'null'
825
1009
  | 'any'
826
1010
  | 'unknown'
@@ -836,6 +1020,7 @@ type CompoundName =
836
1020
  | 'tuple'
837
1021
  | 'union'
838
1022
  | 'schema'
1023
+ | 'flags'
839
1024
  | 'one'
840
1025
  | 'many';
841
1026
 
@@ -851,15 +1036,17 @@ type ScalarOf<N extends string> = N extends 'string'
851
1036
  ? boolean
852
1037
  : N extends 'date'
853
1038
  ? Date
854
- : N extends 'null'
855
- ? null
856
- : N extends 'any'
857
- ? any
858
- : N extends 'unknown' | 'json'
859
- ? unknown
860
- : N extends keyof CustomTypes
861
- ? CustomTypes[N]
862
- : unknown;
1039
+ : N extends 'bytes'
1040
+ ? Uint8Array
1041
+ : N extends 'null'
1042
+ ? null
1043
+ : N extends 'any'
1044
+ ? any
1045
+ : N extends 'unknown' | 'json'
1046
+ ? unknown
1047
+ : N extends keyof CustomTypes
1048
+ ? CustomTypes[N]
1049
+ : unknown;
863
1050
 
864
1051
  // Whether the first character is an uppercase letter: a reference when it is
865
1052
  // the first character of a type name, a kind when it is the first of a key.
@@ -1033,13 +1220,17 @@ type TypeOf<N extends string, D, E, M extends Mode> =
1033
1220
  ? D extends { many: infer T extends string }
1034
1221
  ? Referenced<T, EmbedOf<D>, E, M>[]
1035
1222
  : unknown
1036
- : N extends keyof CustomTypes
1037
- ? CustomTypes[N]
1038
- : N extends ScalarName
1039
- ? ScalarOf<N>
1040
- : D extends { schema: infer S extends object }
1041
- ? Value<S, E, M>
1042
- : unknown;
1223
+ : N extends 'flags'
1224
+ ? D extends { flags: readonly (infer F extends string)[] }
1225
+ ? { [K in F]?: boolean }
1226
+ : Record<string, boolean>
1227
+ : N extends keyof CustomTypes
1228
+ ? CustomTypes[N]
1229
+ : N extends ScalarName
1230
+ ? ScalarOf<N>
1231
+ : D extends { schema: infer S extends object }
1232
+ ? Value<S, E, M>
1233
+ : unknown;
1043
1234
 
1044
1235
  // On the output side a decoder (`parse`) gives the value of its field; a
1045
1236
  // promise of one is awaited.
@@ -1079,7 +1270,9 @@ type Shorthand<D, E, M extends Mode> = D extends { array: unknown }
1079
1270
  ? LongForm<'one', D, E, M>
1080
1271
  : D extends { many: string }
1081
1272
  ? LongForm<'many', D, E, M>
1082
- : Decoded<Struct<D, E, M>, D, M>;
1273
+ : D extends { flags: readonly string[] }
1274
+ ? LongForm<'flags', D, E, M>
1275
+ : Decoded<Struct<D, E, M>, D, M>;
1083
1276
 
1084
1277
  // What is not a field of a struct: a function (a calculated field, or the
1085
1278
  // schema-level validate/parse/serialize/format) and an index definition.
@@ -1252,6 +1445,7 @@ type IsShorthand<D> = D extends { array: unknown }
1252
1445
  | { schema: object }
1253
1446
  | { one: string }
1254
1447
  | { many: string }
1448
+ | { flags: readonly string[] }
1255
1449
  ? true
1256
1450
  : false;
1257
1451
 
@@ -1667,6 +1861,8 @@ export class Schema<const D extends string | object = string | object> {
1667
1861
  detach(...namespaces: Array<Model>): void;
1668
1862
  toString(): string;
1669
1863
  toJSON(): object;
1864
+ // The schema as plain JSON: see `SchemaSnapshot`.
1865
+ toSnapshot(): SchemaSnapshot;
1670
1866
  validate(value: unknown, path?: string): ValidationResult | null;
1671
1867
  }
1672
1868
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexify/metaschema",
3
- "version": "3.3.0",
3
+ "version": "3.4.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>",
@@ -66,6 +66,10 @@
66
66
  "./mongodb": {
67
67
  "types": "./src/mongodb/index.d.ts",
68
68
  "default": "./src/mongodb/index.js"
69
+ },
70
+ "./definition": {
71
+ "types": "./src/definition/index.d.ts",
72
+ "default": "./src/definition/index.js"
69
73
  }
70
74
  },
71
75
  "directories": {
@@ -81,7 +85,12 @@
81
85
  "LICENSE",
82
86
  "SECURITY.md"
83
87
  ],
84
- "sideEffects": false,
88
+ "sideEffects": [
89
+ "./index.js",
90
+ "./browser.js",
91
+ "./src/index.js",
92
+ "./src/validation.js"
93
+ ],
85
94
  "publishConfig": {
86
95
  "access": "public"
87
96
  },
@@ -150,11 +159,12 @@
150
159
  "test:nojit": "node --disallow-code-generation-from-strings --require ./tests/helpers/jit.js --test tests/unit/*.test.js",
151
160
  "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",
152
161
  "test:types": "tsd",
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",
162
+ "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 src/definition/index.d.ts",
154
163
  "lint": "oxlint index.js browser.js src tests scripts bench",
155
164
  "format": "oxfmt index.js browser.js src tests scripts bench",
156
165
  "format:check": "oxfmt --check index.js browser.js src tests scripts bench",
157
166
  "size": "node scripts/size.js",
167
+ "size:gate": "node scripts/size.js --max-gzip 22 --max-gzip-generators 8 --max-gzip-compile 5 --max-gzip-model 13 --max-gzip-stream 1.5 --max-gzip-formats 5 --max-gzip-derive 2.5 --max-gzip-coerce 2 --max-gzip-mongodb 4 --max-gzip-definition 11",
158
168
  "bench": "node bench/bench.js",
159
169
  "bench:compare": "node bench/compare.js",
160
170
  "bench:releases": "node bench/releases.js",