pema 0.3.0 → 0.5.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 (178) hide show
  1. package/lib/private/ArrayType.d.ts +1 -1
  2. package/lib/private/ArrayType.js +14 -22
  3. package/lib/private/BigIntType.d.ts +2 -2
  4. package/lib/private/BigUintType.d.ts +2 -2
  5. package/lib/private/BlobType.d.ts +2 -2
  6. package/lib/private/BlobType.js +2 -2
  7. package/lib/private/BooleanType.d.ts +2 -2
  8. package/lib/private/BooleanType.js +4 -4
  9. package/lib/private/BuiltinType.d.ts +1 -1
  10. package/lib/private/BuiltinType.js +3 -5
  11. package/lib/private/ConstructorType.d.ts +1 -1
  12. package/lib/private/ConstructorType.js +3 -5
  13. package/lib/private/DataType.d.ts +0 -1
  14. package/lib/private/DateType.d.ts +2 -2
  15. package/lib/private/DateType.js +2 -2
  16. package/lib/private/FileType.d.ts +2 -2
  17. package/lib/private/FileType.js +2 -2
  18. package/lib/private/GenericType.d.ts +1 -1
  19. package/lib/private/InferInputSchema.d.ts +7 -17
  20. package/lib/private/InferStore.d.ts +1 -1
  21. package/lib/private/IntType.d.ts +2 -2
  22. package/lib/private/Issue.d.ts +1 -1
  23. package/lib/private/LiteralType.d.ts +1 -2
  24. package/lib/private/LiteralType.js +3 -5
  25. package/lib/private/NormalizeSchema.d.ts +4 -6
  26. package/lib/private/NullType.js +3 -5
  27. package/lib/private/NumericType.d.ts +2 -2
  28. package/lib/private/ObjectType.d.ts +1 -1
  29. package/lib/private/ObjectType.js +9 -9
  30. package/lib/private/OmitType.d.ts +20 -0
  31. package/lib/private/OmitType.js +43 -0
  32. package/lib/private/ParseError.d.ts +1 -1
  33. package/lib/private/ParseError.js +2 -2
  34. package/lib/private/ParseOptions.d.ts +1 -1
  35. package/lib/private/Parsed.d.ts +1 -1
  36. package/lib/private/PartialType.js +6 -9
  37. package/lib/private/Partialable.d.ts +1 -1
  38. package/lib/private/PrimitiveType.js +18 -9
  39. package/lib/private/PureType.d.ts +1 -1
  40. package/lib/private/RecordType.js +8 -14
  41. package/lib/private/Schema.d.ts +1 -1
  42. package/lib/private/Serialized.d.ts +4 -4
  43. package/lib/private/{Storeable.d.ts → Storable.d.ts} +2 -2
  44. package/lib/private/{Storeable.js → Storable.js} +2 -2
  45. package/lib/private/StoreSchema.d.ts +3 -6
  46. package/lib/private/StoreType.d.ts +4 -12
  47. package/lib/private/StoreType.js +23 -34
  48. package/lib/private/StringType.d.ts +2 -2
  49. package/lib/private/StringType.js +2 -2
  50. package/lib/private/TupleType.d.ts +2 -2
  51. package/lib/private/TupleType.js +17 -14
  52. package/lib/private/Type.d.ts +1 -1
  53. package/lib/private/URLType.d.ts +2 -2
  54. package/lib/private/URLType.js +2 -2
  55. package/lib/private/UintType.d.ts +2 -2
  56. package/lib/private/UnionType.d.ts +3 -3
  57. package/lib/private/UnionType.js +11 -13
  58. package/lib/private/VirtualType.js +2 -3
  59. package/lib/private/coerce/bigint.js +3 -5
  60. package/lib/private/coerce/int.js +2 -3
  61. package/lib/private/constructor.d.ts +1 -1
  62. package/lib/private/dict.d.ts +11 -0
  63. package/lib/private/dict.js +6 -0
  64. package/lib/private/fail.d.ts +4 -0
  65. package/lib/private/fail.js +6 -0
  66. package/lib/private/index.d.ts +6 -2
  67. package/lib/private/index.js +10 -2
  68. package/lib/private/json/JSONPayload.d.ts +1 -1
  69. package/lib/private/normalize.js +2 -3
  70. package/lib/private/object.d.ts +2 -3
  71. package/lib/private/omit.d.ts +6 -0
  72. package/lib/private/omit.js +5 -0
  73. package/lib/private/path/join.d.ts +1 -1
  74. package/lib/private/path/next.d.ts +1 -1
  75. package/lib/private/path/rebase.d.ts +1 -1
  76. package/lib/private/test/paths-of.d.ts +1 -1
  77. package/lib/private/validator/integer.js +2 -2
  78. package/lib/private/validator/length.js +2 -2
  79. package/lib/private/validator/max.js +2 -2
  80. package/lib/private/validator/min.js +2 -2
  81. package/lib/private/validator/range.js +2 -2
  82. package/lib/private/validator/values.d.ts +1 -1
  83. package/lib/private/validator/values.js +3 -2
  84. package/lib/public/index.d.ts +22 -0
  85. package/lib/public/index.js +10 -0
  86. package/package.json +13 -11
  87. package/lib/private/DecrementDepth.d.ts +0 -3
  88. package/lib/private/DecrementDepth.js +0 -2
  89. package/lib/private/InferSchema.d.ts +0 -20
  90. package/lib/private/InferSchema.js +0 -2
  91. package/lib/private/PrimaryType.d.ts +0 -14
  92. package/lib/private/PrimaryType.js +0 -26
  93. package/lib/private/primary.d.ts +0 -4
  94. package/lib/private/primary.js +0 -3
  95. package/lib/public/DataType.d.ts +0 -2
  96. package/lib/public/DataType.js +0 -2
  97. package/lib/public/Id.d.ts +0 -2
  98. package/lib/public/Id.js +0 -2
  99. package/lib/public/InferStore.d.ts +0 -2
  100. package/lib/public/InferStore.js +0 -2
  101. package/lib/public/InferStoreOut.d.ts +0 -2
  102. package/lib/public/InferStoreOut.js +0 -2
  103. package/lib/public/Issue.d.ts +0 -2
  104. package/lib/public/Issue.js +0 -2
  105. package/lib/public/JSONPayload.d.ts +0 -2
  106. package/lib/public/JSONPayload.js +0 -2
  107. package/lib/public/Schema.d.ts +0 -2
  108. package/lib/public/Schema.js +0 -2
  109. package/lib/public/Serialized.d.ts +0 -2
  110. package/lib/public/Serialized.js +0 -2
  111. package/lib/public/StoreId.d.ts +0 -2
  112. package/lib/public/StoreId.js +0 -2
  113. package/lib/public/StoreSchema.d.ts +0 -2
  114. package/lib/public/StoreSchema.js +0 -2
  115. package/lib/public/array.d.ts +0 -3
  116. package/lib/public/array.js +0 -3
  117. package/lib/public/bigint.d.ts +0 -2
  118. package/lib/public/bigint.js +0 -2
  119. package/lib/public/biguint.d.ts +0 -2
  120. package/lib/public/biguint.js +0 -2
  121. package/lib/public/blob.d.ts +0 -2
  122. package/lib/public/blob.js +0 -2
  123. package/lib/public/boolean.d.ts +0 -3
  124. package/lib/public/boolean.js +0 -3
  125. package/lib/public/constructor.d.ts +0 -3
  126. package/lib/public/constructor.js +0 -3
  127. package/lib/public/date.d.ts +0 -2
  128. package/lib/public/date.js +0 -2
  129. package/lib/public/f32.d.ts +0 -2
  130. package/lib/public/f32.js +0 -2
  131. package/lib/public/f64.d.ts +0 -2
  132. package/lib/public/f64.js +0 -2
  133. package/lib/public/file.d.ts +0 -2
  134. package/lib/public/file.js +0 -2
  135. package/lib/public/i128.d.ts +0 -2
  136. package/lib/public/i128.js +0 -2
  137. package/lib/public/i16.d.ts +0 -2
  138. package/lib/public/i16.js +0 -2
  139. package/lib/public/i32.d.ts +0 -2
  140. package/lib/public/i32.js +0 -2
  141. package/lib/public/i64.d.ts +0 -2
  142. package/lib/public/i64.js +0 -2
  143. package/lib/public/i8.d.ts +0 -2
  144. package/lib/public/i8.js +0 -2
  145. package/lib/public/int.d.ts +0 -2
  146. package/lib/public/int.js +0 -2
  147. package/lib/public/number.d.ts +0 -3
  148. package/lib/public/number.js +0 -3
  149. package/lib/public/object.d.ts +0 -2
  150. package/lib/public/object.js +0 -2
  151. package/lib/public/optional.d.ts +0 -2
  152. package/lib/public/optional.js +0 -2
  153. package/lib/public/primary.d.ts +0 -2
  154. package/lib/public/primary.js +0 -2
  155. package/lib/public/pure.d.ts +0 -2
  156. package/lib/public/pure.js +0 -2
  157. package/lib/public/record.d.ts +0 -3
  158. package/lib/public/record.js +0 -3
  159. package/lib/public/string.d.ts +0 -3
  160. package/lib/public/string.js +0 -3
  161. package/lib/public/symbol.d.ts +0 -2
  162. package/lib/public/symbol.js +0 -2
  163. package/lib/public/u128.d.ts +0 -2
  164. package/lib/public/u128.js +0 -2
  165. package/lib/public/u16.d.ts +0 -2
  166. package/lib/public/u16.js +0 -2
  167. package/lib/public/u32.d.ts +0 -2
  168. package/lib/public/u32.js +0 -2
  169. package/lib/public/u64.d.ts +0 -2
  170. package/lib/public/u64.js +0 -2
  171. package/lib/public/u8.d.ts +0 -2
  172. package/lib/public/u8.js +0 -2
  173. package/lib/public/uint.d.ts +0 -3
  174. package/lib/public/uint.js +0 -3
  175. package/lib/public/union.d.ts +0 -3
  176. package/lib/public/union.js +0 -3
  177. package/lib/public/unknown.d.ts +0 -3
  178. package/lib/public/unknown.js +0 -2
@@ -1,11 +1,11 @@
1
1
  import DefaultType from "#DefaultType";
2
- import error from "#error";
2
+ import fail from "#fail";
3
3
  import ParsedKey from "#ParsedKey";
4
4
  import ParseError from "#ParseError";
5
5
  import join from "#path/join";
6
6
  import next from "#path/next";
7
7
  import VirtualType from "#VirtualType";
8
- import isDict from "@rcompat/is/dict";
8
+ import is from "@rcompat/is";
9
9
  export default class PartialType extends VirtualType {
10
10
  #spec;
11
11
  constructor(spec) {
@@ -22,9 +22,8 @@ export default class PartialType extends VirtualType {
22
22
  return new DefaultType(this, value);
23
23
  }
24
24
  parse(x, options = {}) {
25
- if (!isDict(x)) {
26
- throw new ParseError(error("object", x, options));
27
- }
25
+ if (!is.dict(x))
26
+ throw fail("object", x, options);
28
27
  const input = x;
29
28
  const out = {};
30
29
  const issues = [];
@@ -40,9 +39,8 @@ export default class PartialType extends VirtualType {
40
39
  catch (e) {
41
40
  if (e instanceof ParseError) {
42
41
  // child already rebased to /<key> via nextOptions -> just collect
43
- if (e.issues && e.issues.length) {
42
+ if (e.issues && e.issues.length)
44
43
  issues.push(...e.issues);
45
- }
46
44
  }
47
45
  else {
48
46
  // wrap non-ParseError into a properly-pathed issue at /<key>
@@ -57,9 +55,8 @@ export default class PartialType extends VirtualType {
57
55
  }
58
56
  }
59
57
  }
60
- if (issues.length > 0) {
58
+ if (issues.length > 0)
61
59
  throw new ParseError(issues);
62
- }
63
60
  return out;
64
61
  }
65
62
  toJSON() {
@@ -1,5 +1,5 @@
1
1
  import type Parsed from "#Parsed";
2
- import type Dict from "@rcompat/type/Dict";
2
+ import type { Dict } from "@rcompat/type";
3
3
  type Partialable = Dict<Parsed<unknown>>;
4
4
  export type { Partialable as default };
5
5
  //# sourceMappingURL=Partialable.d.ts.map
@@ -25,29 +25,38 @@ export default class PrimitiveType extends Type {
25
25
  return this.derive({ options: { coerce: true } });
26
26
  }
27
27
  parse(x, options = {}) {
28
- const $options = { ...this.#options, ...options };
29
- const validators = [...$options.validators ?? [], ...this.#validators];
28
+ // hotpath: avoid object spread when possible
29
+ const has_instance_options = this.#options.coerce !== undefined
30
+ || this.#options[ParsedKey] !== undefined;
31
+ const $options = has_instance_options
32
+ ? { ...this.#options, ...options }
33
+ : options;
34
+ // hotpath: avoid array spread when no option validators
35
+ const option_validators = $options.validators;
36
+ const validators = option_validators && option_validators.length > 0
37
+ ? option_validators.concat(this.#validators)
38
+ : this.#validators;
30
39
  const $x = $options.coerce === true ? this[CoerceKey](x) : x;
31
40
  if (typeof $x !== this.name) {
32
41
  throw new ParseError(error(this.name, $x, $options));
33
42
  }
34
43
  const base = $options[ParsedKey] ?? "";
35
- for (const v of validators) {
44
+ for (let i = 0; i < validators.length; i++) {
36
45
  try {
37
- v($x);
46
+ validators[i]($x);
38
47
  }
39
48
  catch (e) {
40
49
  if (e instanceof ParseError) {
41
50
  // rebase each issue path under `base`
42
- const rebased = (e.issues ?? []).map(i => ({
43
- ...i,
44
- path: i.path === ""
51
+ const rebased = (e.issues ?? []).map(issue => ({
52
+ ...issue,
53
+ path: issue.path === ""
45
54
  ? base
46
- : (base === "" ? i.path : (base + i.path)),
55
+ : (base === "" ? issue.path : (base + issue.path)),
47
56
  }));
48
57
  throw new ParseError(rebased);
49
58
  }
50
- // non a ParseError - wrap with proper path
59
+ // not a ParseError - wrap with proper path
51
60
  const message = e && typeof e.message === "string"
52
61
  ? e.message
53
62
  : String(e);
@@ -2,7 +2,7 @@ import type Infer from "#Infer";
2
2
  import OptionalType from "#OptionalType";
3
3
  import Parsed from "#Parsed";
4
4
  import type OptionalTrait from "#trait/Optional";
5
- import type Printable from "@rcompat/type/Printable";
5
+ import type { Printable } from "@rcompat/type";
6
6
  export default class PureType<Type, Name extends string = "PureType"> extends Parsed<Type> implements Printable, OptionalTrait {
7
7
  get name(): string;
8
8
  get Name(): Name;
@@ -1,17 +1,12 @@
1
- import error from "#error";
2
1
  import expected from "#expected";
2
+ import fail from "#fail";
3
3
  import GenericType from "#GenericType";
4
4
  import OptionalType from "#OptionalType";
5
5
  import ParsedKey from "#ParsedKey";
6
6
  import ParseError from "#ParseError";
7
7
  import join from "#path/join";
8
- const nextOptions = (k, options) => {
9
- const base = options?.[ParsedKey] ?? "";
10
- return options === undefined
11
- ? { [ParsedKey]: join("", k) }
12
- : { ...options, [ParsedKey]: join(base, k) };
13
- };
14
- const is_numeric = (string) => /^-?\d+(\.\d+)?$/.test(string);
8
+ import next from "#path/next";
9
+ import is from "@rcompat/is";
15
10
  export default class RecordType extends GenericType {
16
11
  #key;
17
12
  #value;
@@ -27,9 +22,8 @@ export default class RecordType extends GenericType {
27
22
  return "record";
28
23
  }
29
24
  parse(x, options = {}) {
30
- if (typeof x !== "object" || x === null) {
31
- throw new ParseError(error("object", x, options));
32
- }
25
+ if (!is.dict(x))
26
+ throw fail("object", x, options);
33
27
  const key_name = this.#key.name;
34
28
  const keys = Object.keys(x);
35
29
  const symbols = Object.getOwnPropertySymbols(x);
@@ -44,21 +38,21 @@ export default class RecordType extends GenericType {
44
38
  }]);
45
39
  }
46
40
  keys.forEach(k => {
47
- if (key_name === "string" && is_numeric(k)) {
41
+ if (key_name === "string" && is.numeric(k)) {
48
42
  throw new ParseError([{
49
43
  input: x,
50
44
  message: expected("string key", +k),
51
45
  path: join(base, k),
52
46
  }]);
53
47
  }
54
- if (key_name === "number" && !is_numeric(k)) {
48
+ if (key_name === "number" && !is.numeric(k)) {
55
49
  throw new ParseError([{
56
50
  input: x,
57
51
  message: expected("number key", k),
58
52
  path: join(base, k),
59
53
  }]);
60
54
  }
61
- this.#value.parse(x[k], nextOptions(k, options));
55
+ this.#value.parse(x[k], next(k, options));
62
56
  });
63
57
  }
64
58
  if (key_name === "symbol") {
@@ -1,5 +1,5 @@
1
1
  import type Parsed from "#Parsed";
2
- import type AbstractNewable from "@rcompat/type/AbstractNewable";
2
+ import type { AbstractNewable } from "@rcompat/type";
3
3
  type Schema = Parsed<unknown> | AbstractNewable | null | undefined | string | number | boolean | Schema[] | {
4
4
  [k: string]: Schema;
5
5
  };
@@ -1,5 +1,4 @@
1
- import type Dict from "@rcompat/type/Dict";
2
- import type JSONValue from "@rcompat/type/JSONValue";
1
+ import type { Dict, JSONValue } from "@rcompat/type";
3
2
  type DT<D extends string = string> = {
4
3
  datatype?: D;
5
4
  };
@@ -63,9 +62,10 @@ type StructuralSerialized = {
63
62
  type: "schema";
64
63
  of: Serialized;
65
64
  } | {
66
- type: "pure";
65
+ type: "omit";
66
+ properties: Dict<Serialized>;
67
67
  } | {
68
- type: "primary";
68
+ type: "pure";
69
69
  };
70
70
  type Serialized = PrimitiveSerialized | NumberSerialized | BigIntSerialized | StructuralSerialized;
71
71
  export type { Serialized as default };
@@ -1,7 +1,7 @@
1
1
  import type K from "#DataKey";
2
2
  import Parsed from "#Parsed";
3
3
  import type Serialized from "#Serialized";
4
- export default abstract class Storeable<T extends K = K> extends Parsed<unknown> {
4
+ export default abstract class Storable<T extends K = K> extends Parsed<unknown> {
5
5
  abstract get datatype(): T;
6
6
  abstract toJSON(): Serialized;
7
7
  static serialize<N extends string, T extends K>(s: {
@@ -12,4 +12,4 @@ export default abstract class Storeable<T extends K = K> extends Parsed<unknown>
12
12
  datatype: T;
13
13
  };
14
14
  }
15
- //# sourceMappingURL=Storeable.d.ts.map
15
+ //# sourceMappingURL=Storable.d.ts.map
@@ -1,7 +1,7 @@
1
1
  import Parsed from "#Parsed";
2
- export default class Storeable extends Parsed {
2
+ export default class Storable extends Parsed {
3
3
  static serialize(s) {
4
4
  return { type: s.name, datatype: s.datatype };
5
5
  }
6
6
  }
7
- //# sourceMappingURL=Storeable.js.map
7
+ //# sourceMappingURL=Storable.js.map
@@ -1,8 +1,5 @@
1
- import type Storeable from "#Storeable";
2
- type StoreSchema = {
3
- [k: string]: Storeable;
4
- } & {
5
- id: Storeable;
6
- };
1
+ import type Storable from "#Storable";
2
+ import type { Dict } from "@rcompat/type";
3
+ type StoreSchema = Dict<Storable>;
7
4
  export type { StoreSchema as default };
8
5
  //# sourceMappingURL=StoreSchema.d.ts.map
@@ -1,21 +1,13 @@
1
- import GenericType from "#GenericType";
2
1
  import type Infer from "#Infer";
3
- import type InferStore from "#InferStore";
2
+ import ObjectType from "#ObjectType";
4
3
  import type ParseOptions from "#ParseOptions";
5
4
  import PartialType from "#PartialType";
6
- import type Serialized from "#Serialized";
7
5
  import type StoreSchema from "#StoreSchema";
8
- import type Dict from "@rcompat/type/Dict";
9
- export default class StoreType<T extends StoreSchema> extends GenericType<T, InferStore<T>, "StoreType"> {
6
+ export default class StoreType<S extends StoreSchema> extends ObjectType<S> {
10
7
  #private;
11
- constructor(spec: T);
8
+ constructor(properties: S, pk?: string | null);
12
9
  get name(): string;
13
- get schema(): T;
14
- partial(): PartialType<T>;
15
10
  parse(x: unknown, options?: ParseOptions): Infer<this>;
16
- toJSON(): {
17
- type: "object";
18
- properties: Dict<Serialized>;
19
- };
11
+ partial(): PartialType<any>;
20
12
  }
21
13
  //# sourceMappingURL=StoreType.d.ts.map
@@ -1,45 +1,34 @@
1
- import GenericType from "#GenericType";
2
- import ParsedKey from "#ParsedKey";
1
+ import ObjectType from "#ObjectType";
3
2
  import PartialType from "#PartialType";
4
- import join from "#path/join";
5
- export default class StoreType extends GenericType {
6
- #properties;
7
- constructor(spec) {
8
- super();
9
- this.#properties = spec;
3
+ import fail from "#fail";
4
+ import next from "#path/next";
5
+ import is from "@rcompat/is";
6
+ export default class StoreType extends ObjectType {
7
+ #pk;
8
+ constructor(properties, pk = null) {
9
+ super(properties);
10
+ this.#pk = pk;
10
11
  }
11
12
  get name() {
12
13
  return "store";
13
14
  }
14
- get schema() {
15
- return this.#properties;
16
- }
17
- partial() {
18
- return new PartialType(this.#properties);
19
- }
20
15
  parse(x, options = {}) {
21
- const spec = this.#properties;
22
- if (typeof x !== "object" || x === null) {
23
- throw new Error("Expected object");
16
+ const $options = { ...options };
17
+ if (x !== undefined && !is.dict(x))
18
+ throw fail("object", x, $options);
19
+ const input = x ?? {};
20
+ const out = {};
21
+ for (const k in this.properties) {
22
+ if (k === this.#pk && !(k in input))
23
+ continue;
24
+ const parsed = this.properties[k].parse(input[k], next(k, $options));
25
+ if (parsed !== undefined)
26
+ out[k] = parsed;
24
27
  }
25
- const result = {};
26
- for (const k in spec) {
27
- const r = spec[k].parse(x[k], {
28
- ...options, [ParsedKey]: join(options[ParsedKey] ?? "", String(k)),
29
- });
30
- // exclude undefined (optionals)
31
- if (r !== undefined) {
32
- result[k] = r;
33
- }
34
- }
35
- return x;
28
+ return out;
36
29
  }
37
- toJSON() {
38
- const properties = {};
39
- for (const [k, v] of Object.entries(this.#properties)) {
40
- properties[k] = v.toJSON();
41
- }
42
- return { type: "object", properties };
30
+ partial() {
31
+ return new PartialType(this.properties);
43
32
  }
44
33
  }
45
34
  //# sourceMappingURL=StoreType.js.map
@@ -1,6 +1,6 @@
1
1
  import PrimitiveType from "#PrimitiveType";
2
- import Storeable from "#Storeable";
3
- export default class StringType extends PrimitiveType<string, "StringType"> implements Storeable<"string"> {
2
+ import Storable from "#Storable";
3
+ export default class StringType extends PrimitiveType<string, "StringType"> implements Storable<"string"> {
4
4
  get name(): "string";
5
5
  get datatype(): "string";
6
6
  isotime(): this;
@@ -1,6 +1,6 @@
1
1
  import schemafail from "#error/schemafail";
2
2
  import PrimitiveType from "#PrimitiveType";
3
- import Storeable from "#Storeable";
3
+ import Storable from "#Storable";
4
4
  import email from "#validator/email";
5
5
  import ends_with from "#validator/ends-with";
6
6
  import isotime from "#validator/isotime";
@@ -51,7 +51,7 @@ export default class StringType extends PrimitiveType {
51
51
  return this.derive({ validators: [length(from, to)] });
52
52
  }
53
53
  toJSON() {
54
- return Storeable.serialize(this);
54
+ return Storable.serialize(this);
55
55
  }
56
56
  }
57
57
  //# sourceMappingURL=StringType.js.map
@@ -1,13 +1,13 @@
1
1
  import GenericType from "#GenericType";
2
2
  import type Infer from "#Infer";
3
- import type InferSchema from "#InferSchema";
3
+ import type NormalizeSchema from "#NormalizeSchema";
4
4
  import OptionalType from "#OptionalType";
5
5
  import type Parsed from "#Parsed";
6
6
  import type ParseOptions from "#ParseOptions";
7
7
  import type Schema from "#Schema";
8
8
  import type OptionalTrait from "#trait/Optional";
9
9
  type InferTuple<T extends Schema[]> = {
10
- [K in keyof T]: T[K] extends Schema ? InferSchema<T[K]> : "tuple-never";
10
+ [K in keyof T]: T[K] extends Schema ? NormalizeSchema<T[K]>["infer"] : "tuple-never";
11
11
  };
12
12
  export default class TupleType<T extends Parsed<unknown>[]> extends GenericType<T, InferTuple<T>, "TupleType"> implements OptionalTrait {
13
13
  #private;
@@ -1,7 +1,6 @@
1
- import error from "#error";
1
+ import fail from "#fail";
2
2
  import GenericType from "#GenericType";
3
3
  import OptionalType from "#OptionalType";
4
- import ParseError from "#ParseError";
5
4
  import next from "#path/next";
6
5
  export default class TupleType extends GenericType {
7
6
  #items;
@@ -16,19 +15,23 @@ export default class TupleType extends GenericType {
16
15
  return new OptionalType(this);
17
16
  }
18
17
  parse(x, options = {}) {
19
- if (!(!!x && Array.isArray(x))) {
20
- throw new ParseError(error("array", x, options));
21
- }
22
- this.#items.forEach((v, i) => {
23
- v.parse(x[i], next(i, options));
24
- });
25
- x.forEach((v, i) => {
26
- if (i >= this.#items.length) {
27
- throw new ParseError(error("undefined", v, next(i, options)));
28
- }
29
- });
18
+ if (!Array.isArray(x))
19
+ throw fail("array", x, options);
20
+ const items = this.#items;
21
+ const len = items.length;
22
+ // validate each expected item
23
+ for (let i = 0; i < len; i++)
24
+ items[i].parse(x[i], next(i, options));
25
+ // reject extra items
26
+ if (x.length > len)
27
+ throw fail("undefined", x[len], next(len, options));
30
28
  return x;
31
29
  }
32
- toJSON() { return { type: this.name, of: this.#items.map(i => i.toJSON()) }; }
30
+ toJSON() {
31
+ return {
32
+ type: this.name,
33
+ of: this.#items.map(i => i.toJSON()),
34
+ };
35
+ }
33
36
  }
34
37
  //# sourceMappingURL=TupleType.js.map
@@ -3,7 +3,7 @@ import OptionalType from "#OptionalType";
3
3
  import Parsed from "#Parsed";
4
4
  import type DefaultTrait from "#trait/Default";
5
5
  import type OptionalTrait from "#trait/Optional";
6
- import type Printable from "@rcompat/type/Printable";
6
+ import type { Printable } from "@rcompat/type";
7
7
  export default abstract class Type<T, Name extends string> extends Parsed<T> implements Printable, DefaultTrait<T>, OptionalTrait {
8
8
  optional(): OptionalType<this>;
9
9
  default<S extends T>(value: (() => S) | S): DefaultType<this, S>;
@@ -1,6 +1,6 @@
1
1
  import BuiltinType from "#BuiltinType";
2
- import Storeable from "#Storeable";
3
- export default class URLType extends BuiltinType<URL, "URLType"> implements Storeable<"url"> {
2
+ import Storable from "#Storable";
3
+ export default class URLType extends BuiltinType<URL, "URLType"> implements Storable<"url"> {
4
4
  get Type(): {
5
5
  new (url: string | URL, base?: string | URL): URL;
6
6
  prototype: URL;
@@ -1,5 +1,5 @@
1
1
  import BuiltinType from "#BuiltinType";
2
- import Storeable from "#Storeable";
2
+ import Storable from "#Storable";
3
3
  export default class URLType extends BuiltinType {
4
4
  get Type() {
5
5
  return URL;
@@ -11,7 +11,7 @@ export default class URLType extends BuiltinType {
11
11
  return "url";
12
12
  }
13
13
  toJSON() {
14
- return Storeable.serialize(this);
14
+ return Storable.serialize(this);
15
15
  }
16
16
  }
17
17
  //# sourceMappingURL=URLType.js.map
@@ -2,9 +2,9 @@ import coerce from "#coerce/int";
2
2
  import CoerceKey from "#CoerceKey";
3
3
  import NumericType from "#NumericType";
4
4
  import type ParseOptions from "#ParseOptions";
5
- import type Storeable from "#Storeable";
5
+ import type Storable from "#Storable";
6
6
  import type UintDataType from "#UintDataType";
7
- export default class UintType<T extends UintDataType> extends NumericType<T, number, "UintType"> implements Storeable<T> {
7
+ export default class UintType<T extends UintDataType> extends NumericType<T, number, "UintType"> implements Storable<T> {
8
8
  [CoerceKey]: typeof coerce;
9
9
  get name(): string;
10
10
  /**
@@ -1,16 +1,16 @@
1
1
  import DefaultType from "#DefaultType";
2
2
  import GenericType from "#GenericType";
3
3
  import type Infer from "#Infer";
4
- import type InferSchema from "#InferSchema";
4
+ import type NormalizeSchema from "#NormalizeSchema";
5
5
  import OptionalType from "#OptionalType";
6
6
  import type Parsed from "#Parsed";
7
7
  import type ParseOptions from "#ParseOptions";
8
8
  import type Schema from "#Schema";
9
9
  import type DefaultTrait from "#trait/Default";
10
10
  import type OptionalTrait from "#trait/Optional";
11
- import type TupleToUnion from "@rcompat/type/TupleToUnion";
11
+ import type { TupleToUnion } from "@rcompat/type";
12
12
  type InferUnion<T extends Schema[]> = TupleToUnion<{
13
- [K in keyof T]: T[K] extends Schema ? InferSchema<T[K]> : "union-never";
13
+ [K in keyof T]: T[K] extends Schema ? NormalizeSchema<T[K]>["infer"] : "union-never";
14
14
  }>;
15
15
  export default class UnionType<T extends Parsed<unknown>[]> extends GenericType<T, InferUnion<T>, "UnionType"> implements OptionalTrait, DefaultTrait<InferUnion<T>> {
16
16
  #private;
@@ -1,7 +1,6 @@
1
1
  import DefaultType from "#DefaultType";
2
- import error from "#error";
2
+ import fail from "#fail";
3
3
  import GenericType from "#GenericType";
4
- import schema from "#index";
5
4
  import isParsedType from "#is-parsed-type";
6
5
  import OptionalType from "#OptionalType";
7
6
  import ParseError from "#ParseError";
@@ -29,7 +28,7 @@ const union_error = (types) => `\`${types.map(t => isParsedType(t) ? t.name : pr
29
28
  export default class UnionType extends GenericType {
30
29
  #of;
31
30
  constructor(of) {
32
- assert(of.length > 1, "union type must have at least two members");
31
+ assert.true(of.length > 1, "union type must have at least two members");
33
32
  super();
34
33
  this.#of = of;
35
34
  }
@@ -46,21 +45,20 @@ export default class UnionType extends GenericType {
46
45
  return new DefaultType(this, value);
47
46
  }
48
47
  parse(x, options = {}) {
49
- // union parses when any of its members parses
50
- const parsed = this.#of.map(type => {
51
- const validator = isParsedType(type) ? type : schema(type);
48
+ for (const type of this.#of) {
52
49
  try {
53
- validator.parse(x, options);
54
- return true;
50
+ type.parse(x, options);
51
+ return x;
55
52
  }
56
53
  catch (e) {
57
- return e;
54
+ if (!(e instanceof ParseError)) {
55
+ throw e;
56
+ }
57
+ // continue to next
58
58
  }
59
- });
60
- if (!parsed.some(r => r === true)) {
61
- throw new ParseError(error(union_error(this.#of), x, options));
62
59
  }
63
- return x;
60
+ // all types failed
61
+ throw fail(union_error(this.#of), x, options);
64
62
  }
65
63
  toJSON() {
66
64
  return { type: this.name, of: this.#of.map(t => t.toJSON()) };
@@ -1,10 +1,9 @@
1
1
  import GenericType from "#GenericType";
2
- const storeable = (x) => !!x && typeof x === "object" && "datatype" in x;
2
+ const storable = (x) => !!x && typeof x === "object" && "datatype" in x;
3
3
  export default class VirtualType extends GenericType {
4
4
  get datatype() {
5
- if (storeable(this.schema)) {
5
+ if (storable(this.schema))
6
6
  return this.schema.datatype;
7
- }
8
7
  throw new Error("cannot be used in a store");
9
8
  }
10
9
  }
@@ -1,12 +1,10 @@
1
- import integer from "@rcompat/is/integer";
2
- import numeric from "@rcompat/is/numeric";
1
+ import is from "@rcompat/is";
3
2
  export default function coerceBigInt(x) {
4
3
  // normalize from string to number; other types unaffected
5
- const n = numeric(x) ? Number(x) : x;
4
+ const n = is.numeric(x) ? Number(x) : x;
6
5
  // normalize from number to bigint; other types unaffected
7
- if (integer(n)) {
6
+ if (is.int(n))
8
7
  return BigInt(n);
9
- }
10
8
  // bigint or invalid
11
9
  return x;
12
10
  }
@@ -1,8 +1,7 @@
1
- import numeric from "@rcompat/is/numeric";
1
+ import is from "@rcompat/is";
2
2
  export default function coerceInt(x) {
3
- if (numeric(x)) {
3
+ if (is.numeric(x))
4
4
  return Number(x);
5
- }
6
5
  return x;
7
6
  }
8
7
  //# sourceMappingURL=int.js.map
@@ -1,5 +1,5 @@
1
1
  import ConstructorType from "#ConstructorType";
2
- import type AbstractNewable from "@rcompat/type/AbstractNewable";
2
+ import type { AbstractNewable } from "@rcompat/type";
3
3
  /**
4
4
  * Value is a constructed instance of the given class.
5
5
  */
@@ -0,0 +1,11 @@
1
+ import type Parsed from "#Parsed";
2
+ import type RecordType from "#RecordType";
3
+ import type StringType from "#StringType";
4
+ /**
5
+ * Value is a dictionary (record with string keys) of the given type.
6
+ *
7
+ * @param of - The value type (defaults to p.string)
8
+ */
9
+ export default function dict(): RecordType<StringType, StringType>;
10
+ export default function dict<const Value extends Parsed<unknown>>(of: Value): RecordType<StringType, Value>;
11
+ //# sourceMappingURL=dict.d.ts.map
@@ -0,0 +1,6 @@
1
+ import record from "#record";
2
+ import string from "#string";
3
+ export default function dict(of = string) {
4
+ return record(string, of);
5
+ }
6
+ //# sourceMappingURL=dict.js.map
@@ -0,0 +1,4 @@
1
+ import error from "#error";
2
+ import ParseError from "#ParseError";
3
+ export default function fail(...args: Parameters<typeof error>): ParseError;
4
+ //# sourceMappingURL=fail.d.ts.map
@@ -0,0 +1,6 @@
1
+ import error from "#error";
2
+ import ParseError from "#ParseError";
3
+ export default function fail(...args) {
4
+ return new ParseError(error(...args));
5
+ }
6
+ //# sourceMappingURL=fail.js.map