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
@@ -1,17 +1,10 @@
1
- import error from "#error";
2
- import expected from "#expected";
1
+ import E from "#errors";
3
2
  import GenericType from "#GenericType";
4
3
  import OptionalType from "#OptionalType";
5
4
  import ParsedKey from "#ParsedKey";
6
- import ParseError from "#ParseError";
7
5
  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);
6
+ import next from "#path/next";
7
+ import is from "@rcompat/is";
15
8
  export default class RecordType extends GenericType {
16
9
  #key;
17
10
  #value;
@@ -27,48 +20,30 @@ export default class RecordType extends GenericType {
27
20
  return "record";
28
21
  }
29
22
  parse(x, options = {}) {
30
- if (typeof x !== "object" || x === null) {
31
- throw new ParseError(error("object", x, options));
32
- }
23
+ const path = options[ParsedKey] ?? "";
24
+ if (!is.dict(x))
25
+ throw E.invalid_type(x, "object", path);
33
26
  const key_name = this.#key.name;
34
27
  const keys = Object.keys(x);
35
28
  const symbols = Object.getOwnPropertySymbols(x);
36
- const base = options[ParsedKey] ?? "";
37
29
  if (key_name === "string" || key_name === "number") {
38
30
  // no key may be a symbol
39
- if (symbols.length > 0) {
40
- throw new ParseError([{
41
- input: x,
42
- message: expected(`${key_name} key`, symbols[0]),
43
- path: base,
44
- }]);
45
- }
31
+ if (symbols.length > 0)
32
+ throw E.invalid_type(x, `${key_name} key`, path);
46
33
  keys.forEach(k => {
47
- if (key_name === "string" && is_numeric(k)) {
48
- throw new ParseError([{
49
- input: x,
50
- message: expected("string key", +k),
51
- path: join(base, k),
52
- }]);
34
+ if (key_name === "string" && is.numeric(k)) {
35
+ throw E.invalid_type(x, "string key", join(path, k));
53
36
  }
54
- if (key_name === "number" && !is_numeric(k)) {
55
- throw new ParseError([{
56
- input: x,
57
- message: expected("number key", k),
58
- path: join(base, k),
59
- }]);
37
+ if (key_name === "number" && !is.numeric(k)) {
38
+ throw E.invalid_type(x, "number key", join(path, k));
60
39
  }
61
- this.#value.parse(x[k], nextOptions(k, options));
40
+ this.#value.parse(x[k], next(k, options));
62
41
  });
63
42
  }
64
43
  if (key_name === "symbol") {
65
44
  // disallow any non-symbol keys
66
45
  if (keys.length > 0) {
67
- throw new ParseError([{
68
- input: x,
69
- message: expected("symbol key", keys[0]),
70
- path: join(base, keys[0]),
71
- }]);
46
+ throw E.invalid_type(keys[0], "symbol key", join(path, keys[0]));
72
47
  }
73
48
  symbols.forEach(k => {
74
49
  this.#value.parse(x[k], options);
@@ -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
  };
@@ -21,7 +20,15 @@ type PrimitiveSerialized = {
21
20
  type: "file";
22
21
  } & DT | {
23
22
  type: "url";
24
- } & DT;
23
+ } & DT | {
24
+ type: "uuid";
25
+ } & DT | {
26
+ type: "uuid_v4";
27
+ } & DT | {
28
+ type: "uuid_v7";
29
+ } & DT | {
30
+ type: "function";
31
+ };
25
32
  type NumberSerialized = {
26
33
  type: "number";
27
34
  } & DT;
@@ -62,13 +69,20 @@ type StructuralSerialized = {
62
69
  } | {
63
70
  type: "schema";
64
71
  of: Serialized;
72
+ } | {
73
+ type: "json";
74
+ datatype: "json";
75
+ of?: Serialized;
65
76
  } | {
66
77
  type: "omit";
67
78
  properties: Dict<Serialized>;
79
+ } | {
80
+ type: "enum";
81
+ values: string[];
68
82
  } | {
69
83
  type: "pure";
70
84
  } | {
71
- type: "primary";
85
+ type: "is";
72
86
  };
73
87
  type Serialized = PrimitiveSerialized | NumberSerialized | BigIntSerialized | StructuralSerialized;
74
88
  export type { Serialized as default };
@@ -1,8 +1,8 @@
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> {
5
- abstract get datatype(): T;
4
+ export default abstract class Storable<TKey extends K = K, TValue = unknown> extends Parsed<TValue> {
5
+ abstract get datatype(): TKey;
6
6
  abstract toJSON(): Serialized;
7
7
  static serialize<N extends string, T extends K>(s: {
8
8
  name: N;
@@ -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,9 +1,13 @@
1
+ import type Infer from "#Infer";
1
2
  import ObjectType from "#ObjectType";
3
+ import type ParseOptions from "#ParseOptions";
2
4
  import PartialType from "#PartialType";
3
5
  import type StoreSchema from "#StoreSchema";
4
6
  export default class StoreType<S extends StoreSchema> extends ObjectType<S> {
5
- constructor(spec: S);
7
+ #private;
8
+ constructor(properties: S, pk?: string | null);
6
9
  get name(): string;
10
+ parse(x: unknown, options?: ParseOptions): Infer<this>;
7
11
  partial(): PartialType<any>;
8
12
  }
9
13
  //# sourceMappingURL=StoreType.d.ts.map
@@ -1,12 +1,32 @@
1
1
  import ObjectType from "#ObjectType";
2
2
  import PartialType from "#PartialType";
3
+ import E from "#errors";
4
+ import next from "#path/next";
5
+ import is from "@rcompat/is";
3
6
  export default class StoreType extends ObjectType {
4
- constructor(spec) {
5
- super(spec);
7
+ #pk;
8
+ constructor(properties, pk = null) {
9
+ super(properties);
10
+ this.#pk = pk;
6
11
  }
7
12
  get name() {
8
13
  return "store";
9
14
  }
15
+ parse(x, options = {}) {
16
+ const $options = { ...options };
17
+ if (x !== undefined && !is.dict(x))
18
+ throw E.invalid_type(x, "object", $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;
27
+ }
28
+ return out;
29
+ }
10
30
  partial() {
11
31
  return new PartialType(this.properties);
12
32
  }
@@ -1,12 +1,11 @@
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;
7
7
  regex(pattern: RegExp): this;
8
8
  email(): this;
9
- uuid(): this;
10
9
  startsWith(prefix: string): this;
11
10
  endsWith(suffix: string): this;
12
11
  min(limit: number): this;
@@ -1,6 +1,6 @@
1
- import schemafail from "#error/schemafail";
2
1
  import PrimitiveType from "#PrimitiveType";
3
- import Storeable from "#Storeable";
2
+ import E from "#schema-errors";
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";
@@ -9,7 +9,6 @@ import max from "#validator/max";
9
9
  import min from "#validator/min";
10
10
  import regex from "#validator/regex";
11
11
  import starts_with from "#validator/starts-with";
12
- import uuid from "#validator/uuid";
13
12
  export default class StringType extends PrimitiveType {
14
13
  get name() {
15
14
  return "string";
@@ -26,9 +25,6 @@ export default class StringType extends PrimitiveType {
26
25
  email() {
27
26
  return this.derive({ validators: [email] });
28
27
  }
29
- uuid() {
30
- return this.derive({ validators: [uuid] });
31
- }
32
28
  startsWith(prefix) {
33
29
  return this.derive({ validators: [starts_with(prefix)] });
34
30
  }
@@ -36,22 +32,20 @@ export default class StringType extends PrimitiveType {
36
32
  return this.derive({ validators: [ends_with(suffix)] });
37
33
  }
38
34
  min(limit) {
39
- if (limit < 0) {
40
- throw schemafail("min: {0} must be positive", limit);
41
- }
35
+ if (limit < 0)
36
+ throw E.min_negative(limit);
42
37
  return this.derive({ validators: [min(limit)] });
43
38
  }
44
39
  max(limit) {
45
- if (limit < 0) {
46
- throw schemafail("max: {0} must be positive", limit);
47
- }
40
+ if (limit < 0)
41
+ throw E.max_negative(limit);
48
42
  return this.derive({ validators: [max(limit)] });
49
43
  }
50
44
  length(from, to) {
51
45
  return this.derive({ validators: [length(from, to)] });
52
46
  }
53
47
  toJSON() {
54
- return Storeable.serialize(this);
48
+ return Storable.serialize(this);
55
49
  }
56
50
  }
57
51
  //# 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 E from "#errors";
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,25 @@ 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));
18
+ if (!Array.isArray(x))
19
+ throw E.invalid_type(x, "array", options);
20
+ const items = this.#items;
21
+ const n = items.length;
22
+ const out = new Array(n);
23
+ // validate each expected item
24
+ for (let i = 0; i < n; i++) {
25
+ out[i] = items[i].parse(x[i], next(i, options));
21
26
  }
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
- });
30
- return x;
27
+ // reject extra items
28
+ if (x.length > n)
29
+ throw E.invalid_type(x[n], "undefined", next(n, options));
30
+ return out;
31
+ }
32
+ toJSON() {
33
+ return {
34
+ type: this.name,
35
+ of: this.#items.map(i => i.toJSON()),
36
+ };
31
37
  }
32
- toJSON() { return { type: this.name, of: this.#items.map(i => i.toJSON()) }; }
33
38
  }
34
39
  //# 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
@@ -0,0 +1,17 @@
1
+ import type Infer from "#Infer";
2
+ import PrimitiveType from "#PrimitiveType";
3
+ import Storable from "#Storable";
4
+ import UUIDV4Type from "#UUIDV4Type";
5
+ import UUIDV7Type from "#UUIDV7Type";
6
+ export default class UUIDType extends PrimitiveType<string, "UUIDType"> implements Storable<"uuid"> {
7
+ get name(): "string";
8
+ get datatype(): "uuid";
9
+ parse(x: unknown): Infer<this>;
10
+ toJSON(): {
11
+ type: "string";
12
+ datatype: "uuid";
13
+ };
14
+ v4(): UUIDV4Type;
15
+ v7(): UUIDV7Type;
16
+ }
17
+ //# sourceMappingURL=UUIDType.d.ts.map
@@ -0,0 +1,33 @@
1
+ import PrimitiveType from "#PrimitiveType";
2
+ import Storable from "#Storable";
3
+ import UUIDV4Type from "#UUIDV4Type";
4
+ import UUIDV7Type from "#UUIDV7Type";
5
+ import E from "#errors";
6
+ import is from "@rcompat/is";
7
+ const re = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i;
8
+ const v4 = new UUIDV4Type();
9
+ const v7 = new UUIDV7Type();
10
+ export default class UUIDType extends PrimitiveType {
11
+ get name() {
12
+ return "string";
13
+ }
14
+ get datatype() {
15
+ return "uuid";
16
+ }
17
+ parse(x) {
18
+ if (!is.string(x) || !re.test(x)) {
19
+ throw E.invalid_format(x, `"${x}" is not a valid UUID`);
20
+ }
21
+ return x;
22
+ }
23
+ toJSON() {
24
+ return Storable.serialize(this);
25
+ }
26
+ v4() {
27
+ return v4;
28
+ }
29
+ v7() {
30
+ return v7;
31
+ }
32
+ }
33
+ //# sourceMappingURL=UUIDType.js.map
@@ -0,0 +1,13 @@
1
+ import type Infer from "#Infer";
2
+ import PrimitiveType from "#PrimitiveType";
3
+ import Storable from "#Storable";
4
+ export default class UUIDV4Type extends PrimitiveType<string, "UUIDV4Type"> implements Storable<"uuid_v4"> {
5
+ get name(): "string";
6
+ get datatype(): "uuid_v4";
7
+ parse(x: unknown): Infer<this>;
8
+ toJSON(): {
9
+ type: "string";
10
+ datatype: "uuid_v4";
11
+ };
12
+ }
13
+ //# sourceMappingURL=UUIDV4Type.d.ts.map
@@ -0,0 +1,23 @@
1
+ import PrimitiveType from "#PrimitiveType";
2
+ import Storable from "#Storable";
3
+ import E from "#errors";
4
+ import is from "@rcompat/is";
5
+ const re = /^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i;
6
+ export default class UUIDV4Type extends PrimitiveType {
7
+ get name() {
8
+ return "string";
9
+ }
10
+ get datatype() {
11
+ return "uuid_v4";
12
+ }
13
+ parse(x) {
14
+ if (!is.string(x) || !re.test(x)) {
15
+ throw E.invalid_format(x, `"${x}" is not a valid UUID v4`);
16
+ }
17
+ return x;
18
+ }
19
+ toJSON() {
20
+ return Storable.serialize(this);
21
+ }
22
+ }
23
+ //# sourceMappingURL=UUIDV4Type.js.map
@@ -0,0 +1,13 @@
1
+ import type Infer from "#Infer";
2
+ import PrimitiveType from "#PrimitiveType";
3
+ import Storable from "#Storable";
4
+ export default class UUIDV7Type extends PrimitiveType<string, "UUIDV7Type"> implements Storable<"uuid_v7"> {
5
+ get name(): "string";
6
+ get datatype(): "uuid_v7";
7
+ parse(x: unknown): Infer<this>;
8
+ toJSON(): {
9
+ type: "string";
10
+ datatype: "uuid_v7";
11
+ };
12
+ }
13
+ //# sourceMappingURL=UUIDV7Type.d.ts.map
@@ -0,0 +1,23 @@
1
+ import PrimitiveType from "#PrimitiveType";
2
+ import Storable from "#Storable";
3
+ import E from "#errors";
4
+ import is from "@rcompat/is";
5
+ const re = /^[\da-f]{8}-[\da-f]{4}-7[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i;
6
+ export default class UUIDV7Type extends PrimitiveType {
7
+ get name() {
8
+ return "string";
9
+ }
10
+ get datatype() {
11
+ return "uuid_v7";
12
+ }
13
+ parse(x) {
14
+ if (!is.string(x) || !re.test(x)) {
15
+ throw E.invalid_format(x, `"${x}" is not a valid UUID v7`);
16
+ }
17
+ return x;
18
+ }
19
+ toJSON() {
20
+ return Storable.serialize(this);
21
+ }
22
+ }
23
+ //# sourceMappingURL=UUIDV7Type.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,10 +1,10 @@
1
1
  import DefaultType from "#DefaultType";
2
- import error from "#error";
2
+ import E from "#errors";
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";
7
+ import S from "#schema-errors";
8
8
  import assert from "@rcompat/assert";
9
9
  const print = (type) => {
10
10
  const parsed = isParsedType(type);
@@ -29,7 +29,7 @@ const union_error = (types) => `\`${types.map(t => isParsedType(t) ? t.name : pr
29
29
  export default class UnionType extends GenericType {
30
30
  #of;
31
31
  constructor(of) {
32
- assert(of.length > 1, "union type must have at least two members");
32
+ assert.true(of.length > 1, S.union_at_least_two_members());
33
33
  super();
34
34
  this.#of = of;
35
35
  }
@@ -46,21 +46,19 @@ export default class UnionType extends GenericType {
46
46
  return new DefaultType(this, value);
47
47
  }
48
48
  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);
49
+ for (const type of this.#of) {
52
50
  try {
53
- validator.parse(x, options);
54
- return true;
51
+ type.parse(x, options);
52
+ return x;
55
53
  }
56
54
  catch (e) {
57
- return e;
55
+ if (!ParseError.is(e))
56
+ throw e;
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 E.invalid_type(x, union_error(this.#of), 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,2 +1,2 @@
1
- export default function coerceBigInt(x: unknown): unknown;
1
+ export default function coerce_bigint(x: unknown): unknown;
2
2
  //# sourceMappingURL=bigint.d.ts.map
@@ -1,12 +1,10 @@
1
- import integer from "@rcompat/is/integer";
2
- import numeric from "@rcompat/is/numeric";
3
- export default function coerceBigInt(x) {
1
+ import is from "@rcompat/is";
2
+ export default function coerce_bigint(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
  }