pema 0.4.0 → 0.6.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 (264) hide show
  1. package/lib/private/ArrayType.d.ts +2 -1
  2. package/lib/private/ArrayType.js +29 -41
  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 -2
  10. package/lib/private/BuiltinType.js +3 -8
  11. package/lib/private/ConstructorType.d.ts +5 -2
  12. package/lib/private/ConstructorType.js +7 -5
  13. package/lib/private/DataType.d.ts +5 -1
  14. package/lib/private/DateType.d.ts +2 -2
  15. package/lib/private/DateType.js +2 -2
  16. package/lib/private/DefaultType.js +3 -6
  17. package/lib/private/EnumType.d.ts +21 -0
  18. package/lib/private/EnumType.js +30 -0
  19. package/lib/private/FileType.d.ts +2 -2
  20. package/lib/private/FileType.js +2 -2
  21. package/lib/private/FunctionType.d.ts +15 -0
  22. package/lib/private/FunctionType.js +20 -0
  23. package/lib/private/GenericType.d.ts +1 -1
  24. package/lib/private/InferInputSchema.d.ts +7 -17
  25. package/lib/private/InferStore.d.ts +1 -1
  26. package/lib/private/IntType.d.ts +2 -2
  27. package/lib/private/IsType.d.ts +19 -0
  28. package/lib/private/IsType.js +29 -0
  29. package/lib/private/Issue.d.ts +3 -1
  30. package/lib/private/IssueType.d.ts +3 -0
  31. package/lib/private/IssueType.js +2 -0
  32. package/lib/private/JSONType.d.ts +26 -0
  33. package/lib/private/JSONType.js +41 -0
  34. package/lib/private/LiteralType.d.ts +5 -2
  35. package/lib/private/LiteralType.js +11 -5
  36. package/lib/private/NormalizeSchema.d.ts +4 -6
  37. package/lib/private/NullType.js +3 -5
  38. package/lib/private/NumberType.d.ts +1 -1
  39. package/lib/private/NumberType.js +1 -1
  40. package/lib/private/NumericType.d.ts +2 -2
  41. package/lib/private/ObjectType.d.ts +13 -4
  42. package/lib/private/ObjectType.js +24 -11
  43. package/lib/private/OmitType.d.ts +1 -1
  44. package/lib/private/OmitType.js +7 -6
  45. package/lib/private/ParseError.d.ts +6 -2
  46. package/lib/private/ParseError.js +25 -15
  47. package/lib/private/ParseOptions.d.ts +1 -1
  48. package/lib/private/Parsed.d.ts +2 -1
  49. package/lib/private/Parsed.js +3 -0
  50. package/lib/private/PartialType.js +8 -10
  51. package/lib/private/Partialable.d.ts +1 -1
  52. package/lib/private/PrimitiveType.d.ts +0 -1
  53. package/lib/private/PrimitiveType.js +23 -22
  54. package/lib/private/PureType.d.ts +1 -1
  55. package/lib/private/RecordType.js +14 -39
  56. package/lib/private/Schema.d.ts +1 -1
  57. package/lib/private/Serialized.d.ts +18 -4
  58. package/lib/private/{Storeable.d.ts → Storable.d.ts} +3 -3
  59. package/lib/private/{Storeable.js → Storable.js} +2 -2
  60. package/lib/private/StoreSchema.d.ts +3 -6
  61. package/lib/private/StoreType.d.ts +5 -1
  62. package/lib/private/StoreType.js +22 -2
  63. package/lib/private/StringType.d.ts +2 -3
  64. package/lib/private/StringType.js +7 -13
  65. package/lib/private/TupleType.d.ts +2 -2
  66. package/lib/private/TupleType.js +19 -14
  67. package/lib/private/Type.d.ts +1 -1
  68. package/lib/private/URLType.d.ts +2 -2
  69. package/lib/private/URLType.js +2 -2
  70. package/lib/private/UUIDType.d.ts +17 -0
  71. package/lib/private/UUIDType.js +33 -0
  72. package/lib/private/UUIDV4Type.d.ts +13 -0
  73. package/lib/private/UUIDV4Type.js +23 -0
  74. package/lib/private/UUIDV7Type.d.ts +13 -0
  75. package/lib/private/UUIDV7Type.js +23 -0
  76. package/lib/private/UintType.d.ts +2 -2
  77. package/lib/private/UnionType.d.ts +3 -3
  78. package/lib/private/UnionType.js +11 -13
  79. package/lib/private/VirtualType.js +2 -3
  80. package/lib/private/coerce/bigint.d.ts +1 -1
  81. package/lib/private/coerce/bigint.js +4 -6
  82. package/lib/private/coerce/float.d.ts +2 -0
  83. package/lib/private/coerce/float.js +8 -0
  84. package/lib/private/coerce/int.d.ts +1 -1
  85. package/lib/private/coerce/int.js +7 -5
  86. package/lib/private/constructor.d.ts +1 -1
  87. package/lib/private/dict.d.ts +11 -0
  88. package/lib/private/dict.js +6 -0
  89. package/lib/private/enum.d.ts +3 -0
  90. package/lib/private/enum.js +5 -0
  91. package/lib/private/errors.d.ts +21 -0
  92. package/lib/private/errors.js +53 -0
  93. package/lib/private/function.d.ts +7 -0
  94. package/lib/private/function.js +6 -0
  95. package/lib/private/index.d.ts +79 -30
  96. package/lib/private/index.js +20 -2
  97. package/lib/private/is.d.ts +4 -0
  98. package/lib/private/is.js +3 -0
  99. package/lib/private/json/JSONIssue.d.ts +2 -0
  100. package/lib/private/json/JSONPayload.d.ts +1 -1
  101. package/lib/private/json.d.ts +5 -0
  102. package/lib/private/json.js +5 -0
  103. package/lib/private/normalize.js +2 -3
  104. package/lib/private/object.d.ts +3 -4
  105. package/lib/private/object.js +2 -2
  106. package/lib/private/omit.d.ts +1 -1
  107. package/lib/private/path/join.d.ts +1 -1
  108. package/lib/private/path/next.d.ts +1 -1
  109. package/lib/private/path/rebase.d.ts +1 -1
  110. package/lib/private/schema-errors.d.ts +16 -0
  111. package/lib/private/schema-errors.js +17 -0
  112. package/lib/private/spec/bigint.d.ts +1 -1
  113. package/lib/private/spec/bigint.js +30 -41
  114. package/lib/private/spec/biguint.d.ts +1 -1
  115. package/lib/private/spec/biguint.js +29 -45
  116. package/lib/private/spec/int.d.ts +1 -1
  117. package/lib/private/spec/int.js +27 -38
  118. package/lib/private/spec/uint.d.ts +1 -1
  119. package/lib/private/spec/uint.js +31 -43
  120. package/lib/private/test.d.ts +34 -0
  121. package/lib/private/test.js +107 -0
  122. package/lib/private/uuid.d.ts +4 -0
  123. package/lib/private/uuid.js +3 -0
  124. package/lib/private/validator/ends-with.d.ts +1 -2
  125. package/lib/private/validator/ends-with.js +8 -10
  126. package/lib/private/validator/f32.js +3 -8
  127. package/lib/private/validator/integer.d.ts +1 -2
  128. package/lib/private/validator/integer.js +7 -11
  129. package/lib/private/validator/isotime.js +8 -10
  130. package/lib/private/validator/length.js +12 -20
  131. package/lib/private/validator/max.js +15 -16
  132. package/lib/private/validator/min.js +15 -16
  133. package/lib/private/validator/port.d.ts +1 -2
  134. package/lib/private/validator/port.js +6 -9
  135. package/lib/private/validator/range.js +7 -13
  136. package/lib/private/validator/regex.d.ts +1 -1
  137. package/lib/private/validator/regex.js +5 -9
  138. package/lib/private/validator/starts-with.d.ts +1 -2
  139. package/lib/private/validator/starts-with.js +8 -10
  140. package/lib/private/validator/unique-by.d.ts +1 -1
  141. package/lib/private/validator/unique-by.js +8 -14
  142. package/lib/private/validator/unique-with.d.ts +1 -1
  143. package/lib/private/validator/unique-with.js +3 -7
  144. package/lib/private/validator/unique.js +2 -6
  145. package/lib/private/validator/values.d.ts +1 -1
  146. package/lib/private/validator/values.js +5 -8
  147. package/lib/public/StoreType.d.ts +1 -1
  148. package/lib/public/index.d.ts +1 -9
  149. package/lib/public/index.js +0 -9
  150. package/package.json +18 -15
  151. package/lib/private/DecrementDepth.d.ts +0 -3
  152. package/lib/private/DecrementDepth.js +0 -2
  153. package/lib/private/InferSchema.d.ts +0 -20
  154. package/lib/private/InferSchema.js +0 -2
  155. package/lib/private/PrimaryType.d.ts +0 -14
  156. package/lib/private/PrimaryType.js +0 -26
  157. package/lib/private/SchemaError.d.ts +0 -4
  158. package/lib/private/SchemaError.js +0 -8
  159. package/lib/private/error/fail.d.ts +0 -3
  160. package/lib/private/error/fail.js +0 -5
  161. package/lib/private/error/schemafail.d.ts +0 -3
  162. package/lib/private/error/schemafail.js +0 -5
  163. package/lib/private/error.d.ts +0 -4
  164. package/lib/private/error.js +0 -11
  165. package/lib/private/expect.d.ts +0 -22
  166. package/lib/private/expect.js +0 -32
  167. package/lib/private/expected.d.ts +0 -3
  168. package/lib/private/expected.js +0 -11
  169. package/lib/private/primary.d.ts +0 -4
  170. package/lib/private/primary.js +0 -3
  171. package/lib/private/test/messages-of.d.ts +0 -3
  172. package/lib/private/test/messages-of.js +0 -4
  173. package/lib/private/test/paths-of.d.ts +0 -3
  174. package/lib/private/test/paths-of.js +0 -4
  175. package/lib/private/test/throws-issues.d.ts +0 -4
  176. package/lib/private/test/throws-issues.js +0 -13
  177. package/lib/private/validator/uuid.d.ts +0 -3
  178. package/lib/private/validator/uuid.js +0 -4
  179. package/lib/public/DataType.d.ts +0 -2
  180. package/lib/public/DataType.js +0 -2
  181. package/lib/public/Id.d.ts +0 -2
  182. package/lib/public/Id.js +0 -2
  183. package/lib/public/InferStore.d.ts +0 -2
  184. package/lib/public/InferStore.js +0 -2
  185. package/lib/public/InferStoreOut.d.ts +0 -2
  186. package/lib/public/InferStoreOut.js +0 -2
  187. package/lib/public/Issue.d.ts +0 -2
  188. package/lib/public/Issue.js +0 -2
  189. package/lib/public/JSONPayload.d.ts +0 -2
  190. package/lib/public/JSONPayload.js +0 -2
  191. package/lib/public/OmitType.d.ts +0 -2
  192. package/lib/public/OmitType.js +0 -2
  193. package/lib/public/Schema.d.ts +0 -2
  194. package/lib/public/Schema.js +0 -2
  195. package/lib/public/Serialized.d.ts +0 -2
  196. package/lib/public/Serialized.js +0 -2
  197. package/lib/public/StoreId.d.ts +0 -2
  198. package/lib/public/StoreId.js +0 -2
  199. package/lib/public/StoreSchema.d.ts +0 -2
  200. package/lib/public/StoreSchema.js +0 -2
  201. package/lib/public/array.d.ts +0 -3
  202. package/lib/public/array.js +0 -3
  203. package/lib/public/bigint.d.ts +0 -2
  204. package/lib/public/bigint.js +0 -2
  205. package/lib/public/biguint.d.ts +0 -2
  206. package/lib/public/biguint.js +0 -2
  207. package/lib/public/blob.d.ts +0 -2
  208. package/lib/public/blob.js +0 -2
  209. package/lib/public/boolean.d.ts +0 -3
  210. package/lib/public/boolean.js +0 -3
  211. package/lib/public/constructor.d.ts +0 -3
  212. package/lib/public/constructor.js +0 -3
  213. package/lib/public/date.d.ts +0 -2
  214. package/lib/public/date.js +0 -2
  215. package/lib/public/f32.d.ts +0 -2
  216. package/lib/public/f32.js +0 -2
  217. package/lib/public/f64.d.ts +0 -2
  218. package/lib/public/f64.js +0 -2
  219. package/lib/public/file.d.ts +0 -2
  220. package/lib/public/file.js +0 -2
  221. package/lib/public/i128.d.ts +0 -2
  222. package/lib/public/i128.js +0 -2
  223. package/lib/public/i16.d.ts +0 -2
  224. package/lib/public/i16.js +0 -2
  225. package/lib/public/i32.d.ts +0 -2
  226. package/lib/public/i32.js +0 -2
  227. package/lib/public/i64.d.ts +0 -2
  228. package/lib/public/i64.js +0 -2
  229. package/lib/public/i8.d.ts +0 -2
  230. package/lib/public/i8.js +0 -2
  231. package/lib/public/int.d.ts +0 -2
  232. package/lib/public/int.js +0 -2
  233. package/lib/public/number.d.ts +0 -3
  234. package/lib/public/number.js +0 -3
  235. package/lib/public/object.d.ts +0 -2
  236. package/lib/public/object.js +0 -2
  237. package/lib/public/optional.d.ts +0 -2
  238. package/lib/public/optional.js +0 -2
  239. package/lib/public/primary.d.ts +0 -2
  240. package/lib/public/primary.js +0 -2
  241. package/lib/public/pure.d.ts +0 -2
  242. package/lib/public/pure.js +0 -2
  243. package/lib/public/record.d.ts +0 -3
  244. package/lib/public/record.js +0 -3
  245. package/lib/public/string.d.ts +0 -3
  246. package/lib/public/string.js +0 -3
  247. package/lib/public/symbol.d.ts +0 -2
  248. package/lib/public/symbol.js +0 -2
  249. package/lib/public/u128.d.ts +0 -2
  250. package/lib/public/u128.js +0 -2
  251. package/lib/public/u16.d.ts +0 -2
  252. package/lib/public/u16.js +0 -2
  253. package/lib/public/u32.d.ts +0 -2
  254. package/lib/public/u32.js +0 -2
  255. package/lib/public/u64.d.ts +0 -2
  256. package/lib/public/u64.js +0 -2
  257. package/lib/public/u8.d.ts +0 -2
  258. package/lib/public/u8.js +0 -2
  259. package/lib/public/uint.d.ts +0 -3
  260. package/lib/public/uint.js +0 -3
  261. package/lib/public/union.d.ts +0 -3
  262. package/lib/public/union.js +0 -3
  263. package/lib/public/unknown.d.ts +0 -3
  264. 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
  };
@@ -25,6 +25,7 @@ export default class ArrayType<T extends Parsed<unknown>> extends GenericType<T,
25
25
  * @returns ArrayType<T>
26
26
  */
27
27
  unique(this: Infer<T> extends Primitive ? ArrayType<T> : never): ArrayType<T>;
28
+ uniqueBy<K>(select: (value: Infer<T>) => K): this;
28
29
  min(limit: number): this;
29
30
  max(limit: number): this;
30
31
  length(from: number, to: number): this;
@@ -1,6 +1,6 @@
1
1
  import DefaultType from "#DefaultType";
2
- import error from "#error";
3
- import schemafail from "#error/schemafail";
2
+ import EnumType from "#EnumType";
3
+ import E from "#errors";
4
4
  import GenericType from "#GenericType";
5
5
  import OptionalType from "#OptionalType";
6
6
  import ParsedKey from "#ParsedKey";
@@ -9,14 +9,15 @@ import join from "#path/join";
9
9
  import next from "#path/next";
10
10
  import rebase from "#path/rebase";
11
11
  import PrimitiveType from "#PrimitiveType";
12
+ import S from "#schema-errors";
12
13
  import length from "#validator/length";
13
14
  import max from "#validator/max";
14
15
  import min from "#validator/min";
15
16
  import unique from "#validator/unique";
17
+ import uniqueBy from "#validator/unique-by";
16
18
  function isPrimitive(x) {
17
- return x instanceof PrimitiveType;
19
+ return x instanceof PrimitiveType || x instanceof EnumType;
18
20
  }
19
- const is = (x, validator) => validator(x);
20
21
  export default class ArrayType extends GenericType {
21
22
  #item;
22
23
  #validators;
@@ -46,64 +47,51 @@ export default class ArrayType extends GenericType {
46
47
  */
47
48
  unique() {
48
49
  if (!isPrimitive(this.#item)) {
49
- throw schemafail("unique: subtype {0} must be primitive", this.#item.name);
50
+ throw S.unique_subtype_not_primitive(this.#item.name);
50
51
  }
51
52
  return this.derive({ validators: [unique] });
52
53
  }
54
+ uniqueBy(select) {
55
+ return this.derive({ validators: [uniqueBy(select)] });
56
+ }
53
57
  min(limit) {
54
- if (limit < 0) {
55
- throw schemafail("min: {0} must be positive", limit);
56
- }
58
+ if (limit < 0)
59
+ throw S.min_negative(limit);
57
60
  return this.derive({ validators: [min(limit)] });
58
61
  }
59
62
  max(limit) {
60
- if (limit < 0) {
61
- throw schemafail("max: {0} must be positive", limit);
62
- }
63
+ if (limit < 0)
64
+ throw S.max_negative(limit);
63
65
  return this.derive({ validators: [max(limit)] });
64
66
  }
65
67
  length(from, to) {
66
68
  return this.derive({ validators: [length(from, to)] });
67
69
  }
68
70
  parse(x, options = {}) {
69
- if (!is(x, _ => !!x && Array.isArray(x))) {
70
- throw new ParseError(error("array", x, options));
71
- }
72
- const base = options[ParsedKey] ?? "";
73
- let last = 0;
74
- x.forEach((v, i) => {
75
- // sparse array check
76
- if (i > last) {
77
- throw new ParseError([{
78
- ...error(this.#item.name, undefined, options)[0],
79
- path: join(base, last),
80
- }]);
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
- }]);
71
+ const path = options[ParsedKey] ?? "";
72
+ if (!Array.isArray(x))
73
+ throw E.invalid_type(x, "array", path);
74
+ const item = this.#item;
75
+ const len = x.length;
76
+ const out = new Array(len);
77
+ for (let i = 0; i < len; i++) {
78
+ if (!(i in x))
79
+ throw E.invalid_type(undefined, item.name, join(path, i));
80
+ out[i] = item.parse(x[i], next(i, options));
92
81
  }
93
- for (const v of this.#validators) {
82
+ const validators = this.#validators;
83
+ for (let i = 0; i < validators.length; i++) {
94
84
  try {
95
- v(x);
85
+ validators[i](out);
96
86
  }
97
87
  catch (e) {
98
- if (e instanceof ParseError) {
99
- const rebased = (e.issues ?? [])
100
- .map(i => ({ ...i, path: rebase(base, i.path) }));
101
- throw new ParseError(rebased);
88
+ if (ParseError.is(e)) {
89
+ throw new ParseError(e.issues.map(issue => ({ ...issue, path: rebase(path, issue.path) })));
102
90
  }
103
91
  throw e;
104
92
  }
105
93
  }
106
- return x;
94
+ return out;
107
95
  }
108
96
  toJSON() {
109
97
  return { type: this.name, of: this.#item.toJSON() };
@@ -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,12 +1,11 @@
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;
8
8
  constructor(options?: ParseOptions);
9
- get coerce(): this;
10
9
  parse(x: unknown, options?: ParseOptions): Infer<this>;
11
10
  }
12
11
  //# sourceMappingURL=BuiltinType.d.ts.map
@@ -1,6 +1,5 @@
1
1
  import CoerceKey from "#CoerceKey";
2
- import error from "#error";
3
- import ParseError from "#ParseError";
2
+ import E from "#errors";
4
3
  import Type from "#Type";
5
4
  export default class BuiltinType extends Type {
6
5
  #options;
@@ -12,15 +11,11 @@ export default class BuiltinType extends Type {
12
11
  const Constructor = this.constructor;
13
12
  return new Constructor({ ...this.#options, ...next });
14
13
  }
15
- get coerce() {
16
- return this.#derive({ coerce: true });
17
- }
18
14
  parse(x, options = {}) {
19
15
  const $options = { ...this.#options, ...options };
20
16
  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
- }
17
+ if (!($x instanceof this.Type))
18
+ throw E.invalid_type($x, this.name, $options);
24
19
  return $x;
25
20
  }
26
21
  }
@@ -1,13 +1,16 @@
1
1
  import DefaultType from "#DefaultType";
2
2
  import GenericType from "#GenericType";
3
3
  import type Infer from "#Infer";
4
+ import OptionalType from "#OptionalType";
4
5
  import type ParseOptions from "#ParseOptions";
5
6
  import type DefaultTrait from "#trait/Default";
6
- import type AbstractNewable from "@rcompat/type/AbstractNewable";
7
- export default class ConstructorType<C extends AbstractNewable> extends GenericType<C, InstanceType<C>, "InstanceType"> implements DefaultTrait<InstanceType<C>> {
7
+ import type OptionalTrait from "#trait/Optional";
8
+ import type { AbstractNewable } from "@rcompat/type";
9
+ export default class ConstructorType<C extends AbstractNewable> extends GenericType<C, InstanceType<C>, "InstanceType"> implements OptionalTrait, DefaultTrait<InstanceType<C>> {
8
10
  #private;
9
11
  constructor(t: C);
10
12
  get name(): string;
13
+ optional(): OptionalType<this>;
11
14
  default(value: (() => InstanceType<C>) | InstanceType<C>): DefaultType<this, InstanceType<C>>;
12
15
  parse(x: unknown, options?: ParseOptions): Infer<this>;
13
16
  toJSON(): {
@@ -1,7 +1,7 @@
1
1
  import DefaultType from "#DefaultType";
2
- import error from "#error";
2
+ import E from "#errors";
3
3
  import GenericType from "#GenericType";
4
- import ParseError from "#ParseError";
4
+ import OptionalType from "#OptionalType";
5
5
  export default class ConstructorType extends GenericType {
6
6
  #type;
7
7
  constructor(t) {
@@ -11,13 +11,15 @@ export default class ConstructorType extends GenericType {
11
11
  get name() {
12
12
  return "constructor";
13
13
  }
14
+ optional() {
15
+ return new OptionalType(this);
16
+ }
14
17
  default(value) {
15
18
  return new DefaultType(this, value);
16
19
  }
17
20
  parse(x, options = {}) {
18
- if (!(x instanceof this.#type)) {
19
- throw new ParseError(error(this.name, x, options));
20
- }
21
+ if (!(x instanceof this.#type))
22
+ throw E.invalid_type(x, this.name, options);
21
23
  return x;
22
24
  }
23
25
  toJSON() {
@@ -1,3 +1,4 @@
1
+ import type { JSONValue } from "@rcompat/type";
1
2
  type DataType = {
2
3
  blob: Blob;
3
4
  boolean: boolean;
@@ -9,7 +10,7 @@ type DataType = {
9
10
  i32: number;
10
11
  i64: bigint;
11
12
  i8: number;
12
- primary: string;
13
+ json: JSONValue;
13
14
  string: string;
14
15
  time: string;
15
16
  u128: bigint;
@@ -18,6 +19,9 @@ type DataType = {
18
19
  u64: bigint;
19
20
  u8: number;
20
21
  url: URL;
22
+ uuid: string;
23
+ uuid_v4: string;
24
+ uuid_v7: string;
21
25
  };
22
26
  export type { DataType as default };
23
27
  //# sourceMappingURL=DataType.d.ts.map
@@ -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,8 +1,5 @@
1
1
  import VirtualType from "#VirtualType";
2
- function isDefaultFunction(x) {
3
- return typeof x === "function";
4
- }
5
- ;
2
+ import is from "@rcompat/is";
6
3
  export default class DefaultType extends VirtualType {
7
4
  #schema;
8
5
  #default;
@@ -23,8 +20,8 @@ export default class DefaultType extends VirtualType {
23
20
  parse(x, options = {}) {
24
21
  let $x = x;
25
22
  // default fallback
26
- if ($x === undefined) {
27
- $x = isDefaultFunction(this.#default) ? this.#default() : this.#default;
23
+ if (is.undefined($x)) {
24
+ $x = is.function(this.#default) ? this.#default() : this.#default;
28
25
  }
29
26
  return this.#schema.parse($x, options);
30
27
  }
@@ -0,0 +1,21 @@
1
+ import DefaultType from "#DefaultType";
2
+ import GenericType from "#GenericType";
3
+ import type Infer from "#Infer";
4
+ import OptionalType from "#OptionalType";
5
+ import type ParseOptions from "#ParseOptions";
6
+ import type OptionalTrait from "#trait/Optional";
7
+ type Literal = string;
8
+ export default class EnumType<T extends readonly Literal[]> extends GenericType<T, T[number], "EnumType"> implements OptionalTrait {
9
+ #private;
10
+ constructor(values: T);
11
+ get name(): string;
12
+ optional(): OptionalType<this>;
13
+ default<D extends T[number]>(value: (() => D) | D): DefaultType<EnumType<T>, D>;
14
+ parse(x: unknown, options?: ParseOptions): Infer<this>;
15
+ toJSON(): {
16
+ type: "enum";
17
+ values: T[number][];
18
+ };
19
+ }
20
+ export {};
21
+ //# sourceMappingURL=EnumType.d.ts.map
@@ -0,0 +1,30 @@
1
+ import DefaultType from "#DefaultType";
2
+ import E from "#errors";
3
+ import GenericType from "#GenericType";
4
+ import OptionalType from "#OptionalType";
5
+ export default class EnumType extends GenericType {
6
+ #values;
7
+ constructor(values) {
8
+ super();
9
+ this.#values = values;
10
+ }
11
+ get name() {
12
+ return this.#values.map(v => `"${v}"`).join(" | ");
13
+ }
14
+ optional() {
15
+ return new OptionalType(this);
16
+ }
17
+ default(value) {
18
+ return new DefaultType(this, value);
19
+ }
20
+ parse(x, options = {}) {
21
+ if (typeof x !== "string" || !this.#values.includes(x)) {
22
+ throw E.invalid_type(x, this.name, options);
23
+ }
24
+ return x;
25
+ }
26
+ toJSON() {
27
+ return { type: "enum", values: [...this.#values] };
28
+ }
29
+ }
30
+ //# sourceMappingURL=EnumType.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
@@ -0,0 +1,15 @@
1
+ import GenericType from "#GenericType";
2
+ import type Infer from "#Infer";
3
+ import OptionalType from "#OptionalType";
4
+ import type ParseOptions from "#ParseOptions";
5
+ import type OptionalTrait from "#trait/Optional";
6
+ import type { UnknownFunction } from "@rcompat/type";
7
+ export default class FunctionType extends GenericType<UnknownFunction, UnknownFunction, "FunctionType"> implements OptionalTrait {
8
+ get name(): "function";
9
+ optional(): OptionalType<this>;
10
+ parse(x: unknown, options?: ParseOptions): Infer<this>;
11
+ toJSON(): {
12
+ type: "function";
13
+ };
14
+ }
15
+ //# sourceMappingURL=FunctionType.d.ts.map
@@ -0,0 +1,20 @@
1
+ import GenericType from "#GenericType";
2
+ import OptionalType from "#OptionalType";
3
+ import E from "#errors";
4
+ export default class FunctionType extends GenericType {
5
+ get name() {
6
+ return "function";
7
+ }
8
+ optional() {
9
+ return new OptionalType(this);
10
+ }
11
+ parse(x, options = {}) {
12
+ if (typeof x !== "function")
13
+ throw E.invalid_type(x, this.name, options);
14
+ return x;
15
+ }
16
+ toJSON() {
17
+ return { type: this.name };
18
+ }
19
+ }
20
+ //# sourceMappingURL=FunctionType.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>;
@@ -0,0 +1,19 @@
1
+ import DefaultType from "#DefaultType";
2
+ import type Infer from "#Infer";
3
+ import OptionalType from "#OptionalType";
4
+ import Parsed from "#Parsed";
5
+ import type ParseOptions from "#ParseOptions";
6
+ import type DefaultTrait from "#trait/Default";
7
+ import type OptionalTrait from "#trait/Optional";
8
+ export default class IsType<T> extends Parsed<T> implements OptionalTrait, DefaultTrait<T> {
9
+ #private;
10
+ constructor(predicate: (x: unknown) => x is T);
11
+ get name(): "is";
12
+ optional(): OptionalType<this>;
13
+ default(value: (() => T) | T): DefaultType<this, T>;
14
+ parse(x: unknown, options?: ParseOptions): Infer<this>;
15
+ toJSON(): {
16
+ type: "is";
17
+ };
18
+ }
19
+ //# sourceMappingURL=IsType.d.ts.map
@@ -0,0 +1,29 @@
1
+ import DefaultType from "#DefaultType";
2
+ import E from "#errors";
3
+ import OptionalType from "#OptionalType";
4
+ import Parsed from "#Parsed";
5
+ export default class IsType extends Parsed {
6
+ #predicate;
7
+ constructor(predicate) {
8
+ super();
9
+ this.#predicate = predicate;
10
+ }
11
+ get name() {
12
+ return "is";
13
+ }
14
+ optional() {
15
+ return new OptionalType(this);
16
+ }
17
+ default(value) {
18
+ return new DefaultType(this, value);
19
+ }
20
+ parse(x, options = {}) {
21
+ if (!this.#predicate(x))
22
+ throw E.invalid_type(x, this.name, options);
23
+ return x;
24
+ }
25
+ toJSON() {
26
+ return { type: this.name };
27
+ }
28
+ }
29
+ //# sourceMappingURL=IsType.js.map
@@ -1,5 +1,7 @@
1
- import type JSONPointer from "@rcompat/type/JSONPointer";
1
+ import type IssueType from "#IssueType";
2
+ import type { JSONPointer } from "@rcompat/type";
2
3
  export default interface Issue {
4
+ readonly type: IssueType;
3
5
  readonly message: string;
4
6
  readonly path: JSONPointer;
5
7
  }
@@ -0,0 +1,3 @@
1
+ type IssueType = "invalid_type" | "invalid_format" | "too_small" | "too_large" | "out_of_range" | "not_in_set" | "duplicate" | "network_error";
2
+ export type { IssueType as default };
3
+ //# sourceMappingURL=IssueType.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=IssueType.js.map