pema 0.4.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 (179) 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 +1 -1
  31. package/lib/private/OmitType.js +6 -6
  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 +1 -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 +5 -1
  47. package/lib/private/StoreType.js +22 -2
  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 +4 -2
  67. package/lib/private/index.js +6 -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 +1 -1
  72. package/lib/private/path/join.d.ts +1 -1
  73. package/lib/private/path/next.d.ts +1 -1
  74. package/lib/private/path/rebase.d.ts +1 -1
  75. package/lib/private/test/paths-of.d.ts +1 -1
  76. package/lib/private/validator/integer.js +2 -2
  77. package/lib/private/validator/length.js +2 -2
  78. package/lib/private/validator/max.js +2 -2
  79. package/lib/private/validator/min.js +2 -2
  80. package/lib/private/validator/range.js +2 -2
  81. package/lib/private/validator/values.d.ts +1 -1
  82. package/lib/private/validator/values.js +3 -2
  83. package/lib/public/index.d.ts +20 -1
  84. package/lib/public/index.js +8 -1
  85. package/package.json +13 -11
  86. package/lib/private/DecrementDepth.d.ts +0 -3
  87. package/lib/private/DecrementDepth.js +0 -2
  88. package/lib/private/InferSchema.d.ts +0 -20
  89. package/lib/private/InferSchema.js +0 -2
  90. package/lib/private/PrimaryType.d.ts +0 -14
  91. package/lib/private/PrimaryType.js +0 -26
  92. package/lib/private/primary.d.ts +0 -4
  93. package/lib/private/primary.js +0 -3
  94. package/lib/public/DataType.d.ts +0 -2
  95. package/lib/public/DataType.js +0 -2
  96. package/lib/public/Id.d.ts +0 -2
  97. package/lib/public/Id.js +0 -2
  98. package/lib/public/InferStore.d.ts +0 -2
  99. package/lib/public/InferStore.js +0 -2
  100. package/lib/public/InferStoreOut.d.ts +0 -2
  101. package/lib/public/InferStoreOut.js +0 -2
  102. package/lib/public/Issue.d.ts +0 -2
  103. package/lib/public/Issue.js +0 -2
  104. package/lib/public/JSONPayload.d.ts +0 -2
  105. package/lib/public/JSONPayload.js +0 -2
  106. package/lib/public/OmitType.d.ts +0 -2
  107. package/lib/public/OmitType.js +0 -2
  108. package/lib/public/Schema.d.ts +0 -2
  109. package/lib/public/Schema.js +0 -2
  110. package/lib/public/Serialized.d.ts +0 -2
  111. package/lib/public/Serialized.js +0 -2
  112. package/lib/public/StoreId.d.ts +0 -2
  113. package/lib/public/StoreId.js +0 -2
  114. package/lib/public/StoreSchema.d.ts +0 -2
  115. package/lib/public/StoreSchema.js +0 -2
  116. package/lib/public/array.d.ts +0 -3
  117. package/lib/public/array.js +0 -3
  118. package/lib/public/bigint.d.ts +0 -2
  119. package/lib/public/bigint.js +0 -2
  120. package/lib/public/biguint.d.ts +0 -2
  121. package/lib/public/biguint.js +0 -2
  122. package/lib/public/blob.d.ts +0 -2
  123. package/lib/public/blob.js +0 -2
  124. package/lib/public/boolean.d.ts +0 -3
  125. package/lib/public/boolean.js +0 -3
  126. package/lib/public/constructor.d.ts +0 -3
  127. package/lib/public/constructor.js +0 -3
  128. package/lib/public/date.d.ts +0 -2
  129. package/lib/public/date.js +0 -2
  130. package/lib/public/f32.d.ts +0 -2
  131. package/lib/public/f32.js +0 -2
  132. package/lib/public/f64.d.ts +0 -2
  133. package/lib/public/f64.js +0 -2
  134. package/lib/public/file.d.ts +0 -2
  135. package/lib/public/file.js +0 -2
  136. package/lib/public/i128.d.ts +0 -2
  137. package/lib/public/i128.js +0 -2
  138. package/lib/public/i16.d.ts +0 -2
  139. package/lib/public/i16.js +0 -2
  140. package/lib/public/i32.d.ts +0 -2
  141. package/lib/public/i32.js +0 -2
  142. package/lib/public/i64.d.ts +0 -2
  143. package/lib/public/i64.js +0 -2
  144. package/lib/public/i8.d.ts +0 -2
  145. package/lib/public/i8.js +0 -2
  146. package/lib/public/int.d.ts +0 -2
  147. package/lib/public/int.js +0 -2
  148. package/lib/public/number.d.ts +0 -3
  149. package/lib/public/number.js +0 -3
  150. package/lib/public/object.d.ts +0 -2
  151. package/lib/public/object.js +0 -2
  152. package/lib/public/optional.d.ts +0 -2
  153. package/lib/public/optional.js +0 -2
  154. package/lib/public/primary.d.ts +0 -2
  155. package/lib/public/primary.js +0 -2
  156. package/lib/public/pure.d.ts +0 -2
  157. package/lib/public/pure.js +0 -2
  158. package/lib/public/record.d.ts +0 -3
  159. package/lib/public/record.js +0 -3
  160. package/lib/public/string.d.ts +0 -3
  161. package/lib/public/string.js +0 -3
  162. package/lib/public/symbol.d.ts +0 -2
  163. package/lib/public/symbol.js +0 -2
  164. package/lib/public/u128.d.ts +0 -2
  165. package/lib/public/u128.js +0 -2
  166. package/lib/public/u16.d.ts +0 -2
  167. package/lib/public/u16.js +0 -2
  168. package/lib/public/u32.d.ts +0 -2
  169. package/lib/public/u32.js +0 -2
  170. package/lib/public/u64.d.ts +0 -2
  171. package/lib/public/u64.js +0 -2
  172. package/lib/public/u8.d.ts +0 -2
  173. package/lib/public/u8.js +0 -2
  174. package/lib/public/uint.d.ts +0 -3
  175. package/lib/public/uint.js +0 -3
  176. package/lib/public/union.d.ts +0 -3
  177. package/lib/public/union.js +0 -3
  178. package/lib/public/unknown.d.ts +0 -3
  179. package/lib/public/unknown.js +0 -2
@@ -7,7 +7,7 @@ import type ParseOptions from "#ParseOptions";
7
7
  import type DefaultTrait from "#trait/Default";
8
8
  import type OptionalTrait from "#trait/Optional";
9
9
  import type Validator from "#Validator";
10
- import type Primitive from "@rcompat/type/Primitive";
10
+ import type { Primitive } from "@rcompat/type";
11
11
  type Next<T> = {
12
12
  validators?: Validator<T>[];
13
13
  };
@@ -1,6 +1,7 @@
1
1
  import DefaultType from "#DefaultType";
2
2
  import error from "#error";
3
3
  import schemafail from "#error/schemafail";
4
+ import fail from "#fail";
4
5
  import GenericType from "#GenericType";
5
6
  import OptionalType from "#OptionalType";
6
7
  import ParsedKey from "#ParsedKey";
@@ -16,7 +17,6 @@ import unique from "#validator/unique";
16
17
  function isPrimitive(x) {
17
18
  return x instanceof PrimitiveType;
18
19
  }
19
- const is = (x, validator) => validator(x);
20
20
  export default class ArrayType extends GenericType {
21
21
  #item;
22
22
  #validators;
@@ -66,38 +66,30 @@ export default class ArrayType extends GenericType {
66
66
  return this.derive({ validators: [length(from, to)] });
67
67
  }
68
68
  parse(x, options = {}) {
69
- if (!is(x, _ => !!x && Array.isArray(x))) {
70
- throw new ParseError(error("array", x, options));
71
- }
69
+ if (!Array.isArray(x))
70
+ throw fail("array", x, options);
72
71
  const base = options[ParsedKey] ?? "";
73
- let last = 0;
74
- x.forEach((v, i) => {
72
+ const item = this.#item;
73
+ const len = x.length;
74
+ for (let i = 0; i < len; i++) {
75
75
  // sparse array check
76
- if (i > last) {
76
+ if (!(i in x)) {
77
77
  throw new ParseError([{
78
- ...error(this.#item.name, undefined, options)[0],
79
- path: join(base, last),
78
+ ...error(item.name, undefined, options)[0],
79
+ path: join(base, i),
80
80
  }]);
81
81
  }
82
- const validator = this.#item;
83
- validator.parse(v, next(i, options));
84
- last++;
85
- });
86
- // sparse array with end slots
87
- if (x.length > last) {
88
- throw new ParseError([{
89
- ...error(this.#item.name, undefined, options)[0],
90
- path: join(base, last),
91
- }]);
82
+ item.parse(x[i], next(i, options));
92
83
  }
93
- for (const v of this.#validators) {
84
+ const validators = this.#validators;
85
+ for (let i = 0; i < validators.length; i++) {
94
86
  try {
95
- v(x);
87
+ validators[i](x);
96
88
  }
97
89
  catch (e) {
98
90
  if (e instanceof ParseError) {
99
91
  const rebased = (e.issues ?? [])
100
- .map(i => ({ ...i, path: rebase(base, i.path) }));
92
+ .map(issue => ({ ...issue, path: rebase(base, issue.path) }));
101
93
  throw new ParseError(rebased);
102
94
  }
103
95
  throw e;
@@ -2,8 +2,8 @@ import type BigIntDataType from "#BigIntDataType";
2
2
  import coerce from "#coerce/bigint";
3
3
  import CoerceKey from "#CoerceKey";
4
4
  import NumericType from "#NumericType";
5
- import type Storeable from "#Storeable";
6
- export default class BigIntType<T extends BigIntDataType = "i64"> extends NumericType<T, bigint, "BigIntType"> implements Storeable<T> {
5
+ import type Storable from "#Storable";
6
+ export default class BigIntType<T extends BigIntDataType = "i64"> extends NumericType<T, bigint, "BigIntType"> implements Storable<T> {
7
7
  [CoerceKey]: typeof coerce;
8
8
  get name(): string;
9
9
  }
@@ -2,8 +2,8 @@ import type BigUintDataType from "#BigUintDataType";
2
2
  import coerce from "#coerce/bigint";
3
3
  import CoerceKey from "#CoerceKey";
4
4
  import NumericType from "#NumericType";
5
- import type Storeable from "#Storeable";
6
- export default class BigUintType<T extends BigUintDataType = "u64"> extends NumericType<T, bigint, "BigUintType"> implements Storeable<T> {
5
+ import type Storable from "#Storable";
6
+ export default class BigUintType<T extends BigUintDataType = "u64"> extends NumericType<T, bigint, "BigUintType"> implements Storable<T> {
7
7
  [CoerceKey]: typeof coerce;
8
8
  get name(): string;
9
9
  }
@@ -1,6 +1,6 @@
1
1
  import BuiltinType from "#BuiltinType";
2
- import Storeable from "#Storeable";
3
- export default class BlobType extends BuiltinType<Blob, "BlobType"> implements Storeable<"blob"> {
2
+ import Storable from "#Storable";
3
+ export default class BlobType extends BuiltinType<Blob, "BlobType"> implements Storable<"blob"> {
4
4
  get Type(): {
5
5
  new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
6
6
  prototype: Blob;
@@ -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 BlobType extends BuiltinType {
4
4
  get Type() {
5
5
  return Blob;
@@ -11,7 +11,7 @@ export default class BlobType extends BuiltinType {
11
11
  return "blob";
12
12
  }
13
13
  toJSON() {
14
- return Storeable.serialize(this);
14
+ return Storable.serialize(this);
15
15
  }
16
16
  }
17
17
  //# sourceMappingURL=BlobType.js.map
@@ -1,7 +1,7 @@
1
1
  import CoerceKey from "#CoerceKey";
2
2
  import PrimitiveType from "#PrimitiveType";
3
- import Storeable from "#Storeable";
4
- export default class BooleanType extends PrimitiveType<boolean, "BooleanType"> implements Storeable<"boolean"> {
3
+ import Storable from "#Storable";
4
+ export default class BooleanType extends PrimitiveType<boolean, "BooleanType"> implements Storable<"boolean"> {
5
5
  get name(): "boolean";
6
6
  get datatype(): "boolean";
7
7
  [CoerceKey](x: unknown): unknown;
@@ -1,7 +1,7 @@
1
1
  import CoerceKey from "#CoerceKey";
2
2
  import PrimitiveType from "#PrimitiveType";
3
- import Storeable from "#Storeable";
4
- import boolish from "@rcompat/is/boolish";
3
+ import Storable from "#Storable";
4
+ import is from "@rcompat/is";
5
5
  export default class BooleanType extends PrimitiveType {
6
6
  get name() {
7
7
  return "boolean";
@@ -10,10 +10,10 @@ export default class BooleanType extends PrimitiveType {
10
10
  return "boolean";
11
11
  }
12
12
  [CoerceKey](x) {
13
- return boolish(x) ? x === "true" : x;
13
+ return is.boolish(x) ? x === "true" : x;
14
14
  }
15
15
  toJSON() {
16
- return Storeable.serialize(this);
16
+ return Storable.serialize(this);
17
17
  }
18
18
  }
19
19
  //# sourceMappingURL=BooleanType.js.map
@@ -1,7 +1,7 @@
1
1
  import type Infer from "#Infer";
2
2
  import type ParseOptions from "#ParseOptions";
3
3
  import Type from "#Type";
4
- import type AbstractNewable from "@rcompat/type/AbstractNewable";
4
+ import type { AbstractNewable } from "@rcompat/type";
5
5
  export default abstract class BuiltinType<StaticType, Name extends string> extends Type<StaticType, Name> {
6
6
  #private;
7
7
  abstract get Type(): AbstractNewable;
@@ -1,6 +1,5 @@
1
1
  import CoerceKey from "#CoerceKey";
2
- import error from "#error";
3
- import ParseError from "#ParseError";
2
+ import fail from "#fail";
4
3
  import Type from "#Type";
5
4
  export default class BuiltinType extends Type {
6
5
  #options;
@@ -18,9 +17,8 @@ export default class BuiltinType extends Type {
18
17
  parse(x, options = {}) {
19
18
  const $options = { ...this.#options, ...options };
20
19
  const $x = $options.coerce === true ? this[CoerceKey](x) : x;
21
- if (!($x instanceof this.Type)) {
22
- throw new ParseError(error(this.name, $x, $options));
23
- }
20
+ if (!($x instanceof this.Type))
21
+ throw fail(this.name, $x, $options);
24
22
  return $x;
25
23
  }
26
24
  }
@@ -3,7 +3,7 @@ import GenericType from "#GenericType";
3
3
  import type Infer from "#Infer";
4
4
  import type ParseOptions from "#ParseOptions";
5
5
  import type DefaultTrait from "#trait/Default";
6
- import type AbstractNewable from "@rcompat/type/AbstractNewable";
6
+ import type { AbstractNewable } from "@rcompat/type";
7
7
  export default class ConstructorType<C extends AbstractNewable> extends GenericType<C, InstanceType<C>, "InstanceType"> implements DefaultTrait<InstanceType<C>> {
8
8
  #private;
9
9
  constructor(t: C);
@@ -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 ParseError from "#ParseError";
5
4
  export default class ConstructorType extends GenericType {
6
5
  #type;
7
6
  constructor(t) {
@@ -15,9 +14,8 @@ export default class ConstructorType extends GenericType {
15
14
  return new DefaultType(this, value);
16
15
  }
17
16
  parse(x, options = {}) {
18
- if (!(x instanceof this.#type)) {
19
- throw new ParseError(error(this.name, x, options));
20
- }
17
+ if (!(x instanceof this.#type))
18
+ throw fail(this.name, x, options);
21
19
  return x;
22
20
  }
23
21
  toJSON() {
@@ -9,7 +9,6 @@ type DataType = {
9
9
  i32: number;
10
10
  i64: bigint;
11
11
  i8: number;
12
- primary: string;
13
12
  string: string;
14
13
  time: string;
15
14
  u128: bigint;
@@ -1,8 +1,8 @@
1
1
  import BuiltinType from "#BuiltinType";
2
2
  import coerce from "#coerce/date";
3
3
  import CoerceKey from "#CoerceKey";
4
- import Storeable from "#Storeable";
5
- export default class DateType extends BuiltinType<Date, "DateType"> implements Storeable<"datetime"> {
4
+ import Storable from "#Storable";
5
+ export default class DateType extends BuiltinType<Date, "DateType"> implements Storable<"datetime"> {
6
6
  [CoerceKey]: typeof coerce;
7
7
  get Type(): DateConstructor;
8
8
  get name(): "date";
@@ -1,7 +1,7 @@
1
1
  import BuiltinType from "#BuiltinType";
2
2
  import coerce from "#coerce/date";
3
3
  import CoerceKey from "#CoerceKey";
4
- import Storeable from "#Storeable";
4
+ import Storable from "#Storable";
5
5
  export default class DateType extends BuiltinType {
6
6
  [CoerceKey] = coerce;
7
7
  get Type() {
@@ -14,7 +14,7 @@ export default class DateType extends BuiltinType {
14
14
  return "datetime";
15
15
  }
16
16
  toJSON() {
17
- return Storeable.serialize(this);
17
+ return Storable.serialize(this);
18
18
  }
19
19
  }
20
20
  //# sourceMappingURL=DateType.js.map
@@ -1,6 +1,6 @@
1
1
  import BuiltinType from "#BuiltinType";
2
- import Storeable from "#Storeable";
3
- export default class FileType extends BuiltinType<File, "FileType"> implements Storeable<"blob"> {
2
+ import Storable from "#Storable";
3
+ export default class FileType extends BuiltinType<File, "FileType"> implements Storable<"blob"> {
4
4
  get Type(): {
5
5
  new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
6
6
  prototype: File;
@@ -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 FileType extends BuiltinType {
4
4
  get Type() {
5
5
  return File;
@@ -11,7 +11,7 @@ export default class FileType extends BuiltinType {
11
11
  return "blob";
12
12
  }
13
13
  toJSON() {
14
- return Storeable.serialize(this);
14
+ return Storable.serialize(this);
15
15
  }
16
16
  }
17
17
  //# sourceMappingURL=FileType.js.map
@@ -1,5 +1,5 @@
1
1
  import Parsed from "#Parsed";
2
- import type PrintableGeneric from "@rcompat/type/PrintableGeneric";
2
+ import type { PrintableGeneric } from "@rcompat/type";
3
3
  export default abstract class GenericType<Type, Inferred, Name extends string> extends Parsed<Inferred> implements PrintableGeneric<Type> {
4
4
  get Name(): Name;
5
5
  get Type(): Type;
@@ -1,26 +1,16 @@
1
- import type ArrayType from "#ArrayType";
2
- import type DecrementDepth from "#DecrementDepth";
3
1
  import type DefaultType from "#DefaultType";
4
- import type Infer from "#Infer";
5
- import type NullType from "#NullType";
2
+ import type NormalizeSchema from "#NormalizeSchema";
6
3
  import type ObjectType from "#ObjectType";
7
4
  import type OptionalType from "#OptionalType";
8
5
  import type Parsed from "#Parsed";
9
6
  import type Schema from "#Schema";
10
- import type TupleType from "#TupleType";
11
- import type UndefinedType from "#UndefinedType";
12
- import type ImpliedOptional from "@rcompat/type/ImpliedOptional";
13
- import type UndefinedToOptional from "@rcompat/type/UndefinedToOptional";
14
- type InferInputSchema<S, Depth extends number = 3> = [
15
- Depth
16
- ] extends [never] ? never : S extends DefaultType<infer T, unknown> ? Infer<T> | undefined : S extends OptionalType<infer T> ? Infer<T> | undefined : S extends ObjectType<infer P> ? ImpliedOptional<UndefinedToOptional<{
17
- [K in keyof P]: InferInputSchema<P[K], DecrementDepth[Depth]>;
18
- }>> : S extends Parsed<unknown> ? Infer<S> : S extends null ? Infer<NullType> : S extends undefined ? Infer<UndefinedType> : S extends [infer Only] ? Only extends Parsed<unknown> ? Infer<ArrayType<Only>> : never : S extends Schema[] ? InferInputSchema<TupleType<{
19
- [K in keyof S]: S[K] extends Parsed<unknown> ? InferInputSchema<S[K], DecrementDepth[Depth]> : never;
20
- }>> : S extends {
7
+ import type { ImpliedOptional, UndefinedToOptional } from "@rcompat/type";
8
+ type InferInputSchema<S> = S extends DefaultType<infer T, unknown> ? T["infer"] | undefined : S extends OptionalType<infer T> ? T["infer"] | undefined : S extends ObjectType<infer P> ? ImpliedOptional<UndefinedToOptional<{
9
+ [K in keyof P]: InferInputSchema<P[K]>;
10
+ }>> : S extends Parsed<unknown> ? S["infer"] : S extends {
21
11
  [key: string]: Schema;
22
12
  } ? ImpliedOptional<UndefinedToOptional<{
23
- [K in keyof S]: InferInputSchema<S[K], DecrementDepth[Depth]>;
24
- }>> : never;
13
+ [K in keyof S]: InferInputSchema<S[K]>;
14
+ }>> : NormalizeSchema<S>["infer"];
25
15
  export type { InferInputSchema as default };
26
16
  //# sourceMappingURL=InferInputSchema.d.ts.map
@@ -1,6 +1,6 @@
1
1
  import type Parsed from "#Parsed";
2
2
  import type StoreSchema from "#StoreSchema";
3
- import type UndefinedToOptional from "@rcompat/type/UndefinedToOptional";
3
+ import type { UndefinedToOptional } from "@rcompat/type";
4
4
  type StoreValue<T> = T extends Parsed<infer U> ? U : never;
5
5
  type InferStore<T extends StoreSchema> = UndefinedToOptional<{
6
6
  [K in keyof T]: StoreValue<T[K]>;
@@ -3,8 +3,8 @@ import CoerceKey from "#CoerceKey";
3
3
  import type IntDataType from "#IntDataType";
4
4
  import NumericType from "#NumericType";
5
5
  import type ParseOptions from "#ParseOptions";
6
- import type Storeable from "#Storeable";
7
- export default class IntType<T extends IntDataType> extends NumericType<T, number, "IntType"> implements Storeable<T> {
6
+ import type Storable from "#Storable";
7
+ export default class IntType<T extends IntDataType> extends NumericType<T, number, "IntType"> implements Storable<T> {
8
8
  [CoerceKey]: typeof coerce;
9
9
  get name(): string;
10
10
  parse(x: unknown, options?: ParseOptions<number>): import("./Infer.js").default<this>;
@@ -1,4 +1,4 @@
1
- import type JSONPointer from "@rcompat/type/JSONPointer";
1
+ import type { JSONPointer } from "@rcompat/type";
2
2
  export default interface Issue {
3
3
  readonly message: string;
4
4
  readonly path: JSONPointer;
@@ -2,8 +2,7 @@ import GenericType from "#GenericType";
2
2
  import type Infer from "#Infer";
3
3
  import type ParseOptions from "#ParseOptions";
4
4
  type Literal = string | boolean | number;
5
- type InferLiteral<T extends Literal> = T;
6
- export default class LiteralType<T extends Literal> extends GenericType<T, InferLiteral<T>, "LiteralType"> {
5
+ export default class LiteralType<T extends Literal> extends GenericType<T, T, "LiteralType"> {
7
6
  #private;
8
7
  constructor(literal: T);
9
8
  static new<T extends Literal>(literal: T): LiteralType<T>;
@@ -1,6 +1,5 @@
1
- import error from "#error";
1
+ import fail from "#fail";
2
2
  import GenericType from "#GenericType";
3
- import ParseError from "#ParseError";
4
3
  export default class LiteralType extends GenericType {
5
4
  #literal;
6
5
  constructor(literal) {
@@ -17,9 +16,8 @@ export default class LiteralType extends GenericType {
17
16
  return JSON.stringify(this.#literal);
18
17
  }
19
18
  parse(x, options = {}) {
20
- if (x !== this.#literal) {
21
- throw new ParseError(error(this.name, x, options));
22
- }
19
+ if (x !== this.#literal)
20
+ throw fail(this.name, x, options);
23
21
  return x;
24
22
  }
25
23
  toJSON() {
@@ -7,15 +7,13 @@ import type Parsed from "#Parsed";
7
7
  import type Schema from "#Schema";
8
8
  import type TupleType from "#TupleType";
9
9
  import type UndefinedType from "#UndefinedType";
10
- import type AbstractNewable from "@rcompat/type/AbstractNewable";
11
- import type EO from "@rcompat/type/EO";
12
- type IsEmptyObject<T> = keyof T extends never ? true : false;
10
+ import type { AbstractNewable } from "@rcompat/type";
13
11
  type NormalizeSchemaArray<T extends Schema[]> = {
14
12
  [K in keyof T]: NormalizeSchema<T[K]>;
15
13
  };
16
- type NormalizeSchemaObject<T extends Record<string, unknown>> = IsEmptyObject<T> extends true ? EO : {
17
- -readonly [K in keyof T]: T[K] extends Parsed<unknown> ? T[K] : NormalizeSchema<T[K]>;
14
+ type NormalizeSchemaObject<T extends Record<string, unknown>> = {
15
+ -readonly [K in keyof T]: NormalizeSchema<T[K]>;
18
16
  };
19
- type NormalizeSchema<S> = S extends Parsed<unknown> ? S : S extends null ? NullType : S extends undefined ? UndefinedType : S extends string ? LiteralType<S> : S extends number ? LiteralType<S> : S extends boolean ? LiteralType<S> : S extends AbstractNewable ? ConstructorType<S> : S extends [infer O] ? O extends Schema ? ArrayType<NormalizeSchema<O>> : never : S extends Schema[] ? TupleType<NormalizeSchemaArray<S>> : S extends Record<string, unknown> ? ObjectType<NormalizeSchemaObject<S>> : never;
17
+ type NormalizeSchema<S> = S extends Parsed<unknown> ? S : S extends null ? NullType : S extends undefined ? UndefinedType : S extends string | number | boolean ? LiteralType<S> : S extends AbstractNewable ? ConstructorType<S> : S extends [Schema] ? ArrayType<NormalizeSchema<S[0]>> : S extends Schema[] ? TupleType<NormalizeSchemaArray<S>> : S extends Record<string, unknown> ? ObjectType<NormalizeSchemaObject<S>> : never;
20
18
  export type { NormalizeSchema as default };
21
19
  //# sourceMappingURL=NormalizeSchema.d.ts.map
@@ -1,14 +1,12 @@
1
- import error from "#error";
2
- import ParseError from "#ParseError";
1
+ import fail from "#fail";
3
2
  import PrimitiveType from "#PrimitiveType";
4
3
  export default class NullType extends PrimitiveType {
5
4
  get name() {
6
5
  return "null";
7
6
  }
8
7
  parse(x, options = {}) {
9
- if (x !== null) {
10
- throw new ParseError(error(this.name, x, options));
11
- }
8
+ if (x !== null)
9
+ throw fail(this.name, x, options);
12
10
  return x;
13
11
  }
14
12
  toJSON() {
@@ -1,13 +1,13 @@
1
1
  import type DataKey from "#DataKey";
2
2
  import type ParseOptions from "#ParseOptions";
3
3
  import PrimitiveType from "#PrimitiveType";
4
- import type Storeable from "#Storeable";
4
+ import type Storable from "#Storable";
5
5
  import type Validator from "#Validator";
6
6
  type Next<T> = {
7
7
  options?: ParseOptions;
8
8
  validators?: Validator<T>[];
9
9
  };
10
- export default abstract class NumericType<Key extends DataKey, T extends bigint | number, Name extends string> extends PrimitiveType<T, Name> implements Storeable<Key> {
10
+ export default abstract class NumericType<Key extends DataKey, T extends bigint | number, Name extends string> extends PrimitiveType<T, Name> implements Storable<Key> {
11
11
  #private;
12
12
  constructor(datatype: Key, validators?: Validator<T>[], options?: ParseOptions);
13
13
  derive(next: Next<T>): this;
@@ -4,7 +4,7 @@ import type InferInputSchema from "#InferInputSchema";
4
4
  import type Parsed from "#Parsed";
5
5
  import type ParseOptions from "#ParseOptions";
6
6
  import type Serialized from "#Serialized";
7
- import type Dict from "@rcompat/type/Dict";
7
+ import type { Dict } from "@rcompat/type";
8
8
  export default class ObjectType<P extends Dict<Parsed<unknown>>> extends GenericType<P, {
9
9
  [K in keyof P]: P[K]["infer"];
10
10
  }, "ObjectType"> {
@@ -1,5 +1,7 @@
1
+ import fail from "#fail";
1
2
  import GenericType from "#GenericType";
2
3
  import next from "#path/next";
4
+ import is from "@rcompat/is";
3
5
  export default class ObjectType extends GenericType {
4
6
  #properties;
5
7
  #options;
@@ -20,25 +22,23 @@ export default class ObjectType extends GenericType {
20
22
  get input() {
21
23
  return undefined;
22
24
  }
23
- #derive(next) {
25
+ #derive(options) {
24
26
  const Constructor = this.constructor;
25
- return new Constructor(this.#properties, { ...this.#options, ...next });
27
+ return new Constructor(this.#properties, { ...this.#options, ...options });
26
28
  }
27
29
  get coerce() {
28
30
  return this.#derive({ coerce: true });
29
31
  }
30
32
  parse(x, options = {}) {
31
33
  const $options = { ...this.#options, ...options };
32
- let _x = x;
33
- if (typeof x !== "object" || x === null) {
34
- _x = {};
35
- }
34
+ if (x !== undefined && !is.dict(x))
35
+ throw fail("object", x, $options);
36
+ const input = x ?? {};
36
37
  const out = {};
37
38
  for (const k in this.#properties) {
38
- const parsed = this.#properties[k].parse(_x[k], next(k, $options));
39
- if (parsed !== undefined) {
39
+ const parsed = this.#properties[k].parse(input[k], next(k, $options));
40
+ if (parsed !== undefined)
40
41
  out[k] = parsed;
41
- }
42
42
  }
43
43
  return out;
44
44
  }
@@ -4,7 +4,7 @@ import type ObjectType from "#ObjectType";
4
4
  import type Parsed from "#Parsed";
5
5
  import type ParseOptions from "#ParseOptions";
6
6
  import type Serialized from "#Serialized";
7
- import type Dict from "@rcompat/type/Dict";
7
+ import type { Dict } from "@rcompat/type";
8
8
  export default class OmitType<P extends Dict<Parsed<unknown>>, K extends keyof P> extends GenericType<Omit<P, K>, Omit<{
9
9
  [Key in keyof P]: Infer<P[Key]>;
10
10
  }, K>, "OmitType"> {
@@ -1,3 +1,4 @@
1
+ import fail from "#fail";
1
2
  import GenericType from "#GenericType";
2
3
  import ParsedKey from "#ParsedKey";
3
4
  import join from "#path/join";
@@ -15,10 +16,9 @@ export default class OmitType extends GenericType {
15
16
  return "omit";
16
17
  }
17
18
  parse(x, options = {}) {
18
- if (typeof x !== "object" || x === null) {
19
- throw new Error("Expected object");
20
- }
21
- const result = {};
19
+ if (typeof x !== "object" || x === null)
20
+ throw fail("object", x, options);
21
+ const out = {};
22
22
  const props = this.#properties;
23
23
  for (const k in props) {
24
24
  const field = props[k];
@@ -26,10 +26,10 @@ export default class OmitType extends GenericType {
26
26
  ...options, [ParsedKey]: join(options[ParsedKey] ?? "", String(k)),
27
27
  });
28
28
  if (r !== undefined) {
29
- result[k] = r;
29
+ out[k] = r;
30
30
  }
31
31
  }
32
- return result;
32
+ return out;
33
33
  }
34
34
  toJSON() {
35
35
  const properties = {};
@@ -1,6 +1,6 @@
1
1
  import type JSONPayload from "#json/JSONPayload";
2
2
  import type ParseIssue from "#ParseIssue";
3
- import type Serializable from "@rcompat/type/Serializable";
3
+ import type { Serializable } from "@rcompat/type";
4
4
  export default class ParseError extends Error implements Serializable {
5
5
  #private;
6
6
  constructor(issues: ParseIssue[]);
@@ -30,8 +30,8 @@ export default class ParseError extends Error {
30
30
  if (issues.length === 0) {
31
31
  return { message: "Parsing failed", messages: ["Parsing failed"] };
32
32
  }
33
- const isForm = issues.some(i => i.path !== "");
34
- if (!isForm) {
33
+ const is_form = issues.some(i => i.path !== "");
34
+ if (!is_form) {
35
35
  const messages = issues.map(i => i.message);
36
36
  return { message: messages[0], messages };
37
37
  }
@@ -1,6 +1,6 @@
1
1
  import type ParsedKey from "#ParsedKey";
2
2
  import type Validator from "#Validator";
3
- import type JSONPointer from "@rcompat/type/JSONPointer";
3
+ import type { JSONPointer } from "@rcompat/type";
4
4
  export default interface ParseOptions<T = unknown> {
5
5
  coerce?: boolean;
6
6
  [ParsedKey]?: JSONPointer;
@@ -2,7 +2,7 @@ import CoerceKey from "#CoerceKey";
2
2
  import ParsedKey from "#ParsedKey";
3
3
  import type ParseOptions from "#ParseOptions";
4
4
  import type Serialized from "#Serialized";
5
- import type Serializable from "@rcompat/type/Serializable";
5
+ import type { Serializable } from "@rcompat/type";
6
6
  export default abstract class Parsed<StaticType> implements Serializable {
7
7
  get [ParsedKey](): "ParsedKey";
8
8
  get infer(): StaticType;