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,3 +1,2 @@
1
- declare const _default: (x: bigint | number) => void;
2
- export default _default;
1
+ export default function integer(x: bigint | number): void;
3
2
  //# sourceMappingURL=integer.d.ts.map
@@ -1,12 +1,8 @@
1
- import ParseError from "#ParseError";
2
- import integer from "@rcompat/is/integer";
3
- export default (x) => {
4
- if (!integer(x)) {
5
- throw new ParseError([{
6
- input: x,
7
- message: `${x} is not an integer`,
8
- path: "",
9
- }]);
10
- }
11
- };
1
+ import E from "#errors";
2
+ import is from "@rcompat/is";
3
+ export default function integer(x) {
4
+ if (!is.int(x))
5
+ throw E.invalid_type(x, `${x} is not an integer`);
6
+ }
7
+ ;
12
8
  //# sourceMappingURL=integer.js.map
@@ -1,4 +1,4 @@
1
- import ParseError from "#ParseError";
1
+ import E from "#errors";
2
2
  import regex from "#validator/regex";
3
3
  const ISOTIME = /^T?(?<hour>\d{2}):?(?<minute>\d{2}):?(?<second>\d{2})$/u;
4
4
  const RANGE = {
@@ -6,25 +6,23 @@ const RANGE = {
6
6
  minute: { max: 59, min: 0 },
7
7
  second: { max: 60, min: 0 },
8
8
  };
9
+ function validator(s) {
10
+ return `"${s}" is not a valid ISO time`;
11
+ }
9
12
  export default function isotime(x) {
10
13
  // check format
11
- regex(ISOTIME, y => `"${y}" is not a valid ISO time`)(x);
14
+ regex(ISOTIME, validator)(x);
12
15
  // check range
13
16
  const match = ISOTIME.exec(x);
14
17
  const g = match.groups;
15
18
  const h = Number(g.hour);
16
19
  const m = Number(g.minute);
17
20
  const s = Number(g.second);
18
- const inRange = h >= RANGE.hour.min && h <= RANGE.hour.max &&
21
+ const in_range = h >= RANGE.hour.min && h <= RANGE.hour.max &&
19
22
  m >= RANGE.minute.min && m <= RANGE.minute.max &&
20
23
  s >= RANGE.second.min && s <= RANGE.second.max;
21
- if (!inRange) {
22
- throw new ParseError([{
23
- input: x,
24
- message: `"${x}" is not a valid ISO time`,
25
- path: "",
26
- }]);
27
- }
24
+ if (!in_range)
25
+ throw E.out_of_range(x, validator(x));
28
26
  }
29
27
  ;
30
28
  //# sourceMappingURL=isotime.js.map
@@ -1,27 +1,19 @@
1
- import fail from "#error/fail";
2
- import schemafail from "#error/schemafail";
3
- import ParseError from "#ParseError";
4
- import isFinite from "@rcompat/is/finite";
1
+ import E from "#errors";
2
+ import S from "#schema-errors";
3
+ import is from "@rcompat/is";
5
4
  export default function length(from, to) {
6
- if (!isFinite(from) || !isFinite(to)) {
7
- throw schemafail("length: {0} and {1} must be finite numbers", from, to);
8
- }
9
- if (from < 0 || to < 0) {
10
- throw schemafail("length: {0} and {1} must be positive", from, to);
11
- }
12
- if (from > to) {
13
- throw schemafail("length: {0} must be lower than {1}", from, to);
14
- }
5
+ if (!is.finite(from) || !is.finite(to))
6
+ throw S.length_not_finite(from, to);
7
+ if (from < 0 || to < 0)
8
+ throw S.length_not_positive(from, to);
9
+ if (from > to)
10
+ throw S.length_from_exceeds_to(from, to);
15
11
  return (x) => {
16
- if (typeof x !== "string" && !Array.isArray(x)) {
17
- throw fail(x, "invalid type");
12
+ if (!is.string(x) && !is.array(x)) {
13
+ throw E.invalid_type(x, "string or array");
18
14
  }
19
15
  if (x.length < from || x.length > to) {
20
- throw new ParseError([{
21
- input: x,
22
- message: "length out of range",
23
- path: "",
24
- }]);
16
+ throw E.out_of_range(x, "length out of range");
25
17
  }
26
18
  };
27
19
  }
@@ -1,36 +1,35 @@
1
- import fail from "#error/fail";
2
- import schemafail from "#error/schemafail";
3
- import isFinite from "@rcompat/is/finite";
1
+ import E from "#errors";
2
+ import S from "#schema-errors";
3
+ import is from "@rcompat/is";
4
4
  export default function max(limit) {
5
5
  // validate limit once
6
- if (typeof limit === "number") {
7
- if (!isFinite(limit)) {
8
- throw schemafail("max: limit {0} must be a finite number", limit);
9
- }
6
+ if (is.number(limit)) {
7
+ if (!is.finite(limit))
8
+ throw S.max_limit_not_finite(limit);
10
9
  return (x) => {
11
- if (typeof x === "number") {
10
+ if (is.number(x)) {
12
11
  if (x > limit)
13
- throw fail(x, `${x} is greater than max (${limit})`);
12
+ throw E.too_large(x, `${x} is greater than ${limit}`);
14
13
  }
15
- else if (typeof x === "string" || Array.isArray(x)) {
14
+ else if (is.string(x) || is.array(x)) {
16
15
  if (x.length > limit) {
17
- const unit = typeof x === "string" ? "characters" : "items";
18
- throw fail(x, `max ${limit} ${unit}`);
16
+ const unit = is.string(x) ? "characters" : "items";
17
+ throw E.too_large(x, `max ${limit} ${unit}`);
19
18
  }
20
19
  }
21
20
  else {
22
- throw fail(x, "invalid type");
21
+ throw E.invalid_type(x, "number");
23
22
  }
24
23
  };
25
24
  }
26
25
  // bigint
27
26
  return (x) => {
28
- if (typeof x === "bigint") {
27
+ if (is.bigint(x)) {
29
28
  if (x > limit)
30
- throw fail(x, `${x} is greater than max (${limit})`);
29
+ throw E.too_large(x, `${x} is greater than ${limit}`);
31
30
  }
32
31
  else {
33
- throw fail(x, "invalid type");
32
+ throw E.invalid_type(x, "bigint");
34
33
  }
35
34
  };
36
35
  }
@@ -1,36 +1,35 @@
1
- import fail from "#error/fail";
2
- import schemafail from "#error/schemafail";
3
- import isFinite from "@rcompat/is/finite";
1
+ import E from "#errors";
2
+ import S from "#schema-errors";
3
+ import is from "@rcompat/is";
4
4
  export default function min(limit) {
5
5
  // validate limit once
6
- if (typeof limit === "number") {
7
- if (!isFinite(limit)) {
8
- throw schemafail("max: limit {0} must be a finite number", limit);
9
- }
6
+ if (is.number(limit)) {
7
+ if (!is.finite(limit))
8
+ throw S.min_limit_not_finite(limit);
10
9
  return (x) => {
11
- if (typeof x === "number") {
10
+ if (is.number(x)) {
12
11
  if (x < limit)
13
- throw fail(x, `${x} is lower than min (${limit})`);
12
+ throw E.too_small(x, `${x} is lower than ${limit}`);
14
13
  }
15
- else if (typeof x === "string" || Array.isArray(x)) {
14
+ else if (is.string(x) || is.array(x)) {
16
15
  if (x.length < limit) {
17
- const unit = typeof x === "string" ? "characters" : "items";
18
- throw fail(x, `min ${limit} ${unit}`);
16
+ const unit = is.string(x) ? "characters" : "items";
17
+ throw E.too_small(x, `min ${limit} ${unit}`);
19
18
  }
20
19
  }
21
20
  else {
22
- throw fail(x, "invalid type");
21
+ throw E.invalid_type(x, "number");
23
22
  }
24
23
  };
25
24
  }
26
25
  // bigint
27
26
  return (x) => {
28
- if (typeof x === "bigint") {
27
+ if (is.bigint(x)) {
29
28
  if (x < limit)
30
- throw fail(x, `${x} is lower than min (${limit})`);
29
+ throw E.too_small(x, `${x} is lower than ${limit}`);
31
30
  }
32
31
  else {
33
- throw fail(x, "invalid type");
32
+ throw E.invalid_type(x, "bigint");
34
33
  }
35
34
  };
36
35
  }
@@ -1,3 +1,2 @@
1
- declare const _default: (port: number) => void;
2
- export default _default;
1
+ export default function port(given: number): void;
3
2
  //# sourceMappingURL=port.d.ts.map
@@ -1,15 +1,12 @@
1
- import ParseError from "#ParseError";
1
+ import E from "#errors";
2
2
  // 1000
3
3
  const min = 2 ** 10;
4
4
  // 65535
5
5
  const max = 2 ** 16 - 1;
6
- export default (port) => {
7
- if (port < min || port > max) {
8
- throw new ParseError([{
9
- input: port,
10
- message: `${port} out of port range (${min} - ${max})`,
11
- path: "",
12
- }]);
6
+ export default function port(given) {
7
+ if (given < min || given > max) {
8
+ throw E.out_of_range(given, `${given} out of port range (${min} - ${max})`);
13
9
  }
14
- };
10
+ }
11
+ ;
15
12
  //# sourceMappingURL=port.js.map
@@ -1,21 +1,15 @@
1
- import ParseError from "#ParseError";
2
- import isFinite from "@rcompat/is/finite";
3
- const fail = (input, msg) => new ParseError([{ input, message: msg, path: "" }]);
1
+ import E from "#errors";
2
+ import is from "@rcompat/is";
4
3
  export default function range(from, to) {
5
- if (!isFinite(from) || !isFinite(to)) {
4
+ if (!is.finite(from) || !is.finite(to)) {
6
5
  throw new TypeError("range(): from and to must be finite numbers");
7
6
  }
8
7
  return (x) => {
9
- if (typeof x !== "number" && typeof x !== "bigint") {
10
- throw fail(x, "invalid type");
11
- }
12
- if (x < from || x > to) {
13
- throw new ParseError([{
14
- input: x,
15
- message: `${x} is out of range`,
16
- path: "",
17
- }]);
8
+ if (!is.number(x) && !is.bigint(x)) {
9
+ throw E.invalid_type(x, "number or bigint");
18
10
  }
11
+ if (x < from || x > to)
12
+ throw E.out_of_range(x, `${x} is out of range`);
19
13
  };
20
14
  }
21
15
  ;
@@ -1,5 +1,5 @@
1
1
  import type Validator from "#Validator";
2
2
  type ErrorFunction = (x: string) => string;
3
- export default function validateRegex(regex: RegExp, error?: ErrorFunction): Validator<string>;
3
+ export default function regex(format: RegExp, error?: ErrorFunction): Validator<string>;
4
4
  export {};
5
5
  //# sourceMappingURL=regex.d.ts.map
@@ -1,13 +1,9 @@
1
- import ParseError from "#ParseError";
2
- export default function validateRegex(regex, error) {
1
+ import E from "#errors";
2
+ export default function regex(format, error) {
3
3
  return (x) => {
4
- if (!regex.test(x)) {
5
- const message = (error ?? ((y) => `"${y}" is not a valid ${String(regex)}`))(x);
6
- throw new ParseError([{
7
- input: x,
8
- message,
9
- path: "", // root; the calling type should rebase if needed
10
- }]);
4
+ if (!format.test(x)) {
5
+ const message = (error ?? ((y) => `"${y}" is not a valid ${String(format)}`))(x);
6
+ throw E.invalid_format(x, message);
11
7
  }
12
8
  };
13
9
  }
@@ -1,4 +1,3 @@
1
1
  import type Validator from "#Validator";
2
- declare const _default: (prefix: string) => Validator<string>;
3
- export default _default;
2
+ export default function starts_with(prefix: string): Validator<string>;
4
3
  //# sourceMappingURL=starts-with.d.ts.map
@@ -1,11 +1,9 @@
1
- import ParseError from "#ParseError";
2
- export default (prefix) => (x) => {
3
- if (!x.startsWith(prefix)) {
4
- throw new ParseError([{
5
- input: x,
6
- message: `"${x}" does not start with "${prefix}"`,
7
- path: "",
8
- }]);
9
- }
10
- };
1
+ import E from "#errors";
2
+ export default function starts_with(prefix) {
3
+ return (x) => {
4
+ if (!x.startsWith(prefix)) {
5
+ throw E.invalid_format(x, `"${x}" does not start with "${prefix}"`);
6
+ }
7
+ };
8
+ }
11
9
  //# sourceMappingURL=starts-with.js.map
@@ -1,2 +1,2 @@
1
- export default function uniqueBy<T, K extends PropertyKey>(selector: (item: T) => K): (array: Array<T>) => void;
1
+ export default function unique_by<T, K>(select: (value: T) => K): (array: T[]) => void;
2
2
  //# sourceMappingURL=unique-by.d.ts.map
@@ -1,22 +1,16 @@
1
- import ParseError from "#ParseError";
1
+ import E from "#errors";
2
2
  import join from "#path/join";
3
- function message(k, i, first) {
4
- return `duplicate key "${String(k)}" at index ${i} (first ${first})`;
5
- }
6
- export default function uniqueBy(selector) {
3
+ export default function unique_by(select) {
7
4
  return (array) => {
8
5
  const seen = new Map();
9
6
  for (let i = 0; i < array.length; i++) {
10
- const k = selector(array[i]);
11
- if (seen.has(k)) {
12
- const first = seen.get(k);
13
- throw new ParseError([{
14
- input: array,
15
- message: message(k, i, first),
16
- path: join("", i),
17
- }]);
7
+ const value = array[i];
8
+ const key = select(value);
9
+ if (seen.has(key)) {
10
+ const first = seen.get(key);
11
+ throw E.duplicate(array, `duplicate value at index ${i} (first seen at ${first})`, join("", i));
18
12
  }
19
- seen.set(k, i);
13
+ seen.set(key, i);
20
14
  }
21
15
  };
22
16
  }
@@ -1,2 +1,2 @@
1
- export default function uniqueWith<T>(equals: (a: T, b: T) => boolean): (array: Array<T>) => void;
1
+ export default function unique<T>(equals: (a: T, b: T) => boolean): (array: Array<T>) => void;
2
2
  //# sourceMappingURL=unique-with.d.ts.map
@@ -1,15 +1,11 @@
1
- import ParseError from "#ParseError";
1
+ import E from "#errors";
2
2
  import join from "#path/join";
3
- export default function uniqueWith(equals) {
3
+ export default function unique(equals) {
4
4
  return (array) => {
5
5
  for (let i = 0; i < array.length; i++) {
6
6
  for (let j = 0; j < i; j++) {
7
7
  if (equals(array[i], array[j])) {
8
- throw new ParseError([{
9
- input: array,
10
- message: `items at ${j} and ${i} considered equal`,
11
- path: join("", i),
12
- }]);
8
+ throw E.duplicate(array, `items at ${j} and ${i} considered equal`, join("", i));
13
9
  }
14
10
  }
15
11
  }
@@ -1,4 +1,4 @@
1
- import ParseError from "#ParseError";
1
+ import E from "#errors";
2
2
  import join from "#path/join";
3
3
  export default function unique(array) {
4
4
  const seen = new Map();
@@ -7,11 +7,7 @@ export default function unique(array) {
7
7
  if (seen.has(v)) {
8
8
  const first = seen.get(v);
9
9
  // key = current duplicate index
10
- throw new ParseError([{
11
- input: array,
12
- message: `duplicate value at index ${i} (first seen at ${first})`,
13
- path: join("", i),
14
- }]);
10
+ throw E.duplicate(array, `duplicate value at index ${i} (first seen at ${first})`, join("", i));
15
11
  }
16
12
  seen.set(v, i);
17
13
  }
@@ -1,4 +1,4 @@
1
1
  import type Validator from "#Validator";
2
- import type Dict from "@rcompat/type/Dict";
2
+ import type { Dict } from "@rcompat/type";
3
3
  export default function values<T>(input: Dict<T>): Validator<T>;
4
4
  //# sourceMappingURL=values.d.ts.map
@@ -1,13 +1,10 @@
1
- import ParseError from "#ParseError";
1
+ import E from "#errors";
2
2
  export default function values(input) {
3
- const allowed = Object.values(input).map(v => String(v)).join(", ");
3
+ const input_values = Object.values(input);
4
+ const allowed = input_values.map(v => String(v)).join(", ");
4
5
  return (x) => {
5
- if (!Object.values(input).includes(x)) {
6
- throw new ParseError([{
7
- input: x,
8
- message: `"${x}" not in given list of values (${allowed})`,
9
- path: "",
10
- }]);
6
+ if (!input_values.includes(x)) {
7
+ throw E.not_in_set(x, `"${x}" not in given list of values (${allowed})`);
11
8
  }
12
9
  };
13
10
  }
@@ -1,3 +1,3 @@
1
- export { default as Serialized } from "#Serialized";
1
+ export type { default as Serialized } from "#Serialized";
2
2
  export { default } from "#StoreType";
3
3
  //# sourceMappingURL=StoreType.d.ts.map
@@ -1,11 +1,3 @@
1
- export { default as ConstructorType } from "#ConstructorType";
2
- export { default as DefaultType } from "#DefaultType";
1
+ export type * from "#index";
3
2
  export { default } from "#index";
4
- export { default as LiteralType } from "#LiteralType";
5
- export { default as ObjectType } from "#ObjectType";
6
- export { default as OptionalType } from "#OptionalType";
7
- export { default as PureType } from "#PureType";
8
- export { default as TupleType } from "#TupleType";
9
- export { default as UndefinedType } from "#UndefinedType";
10
- export { default as ArrayType } from "#ArrayType";
11
3
  //# sourceMappingURL=index.d.ts.map
@@ -1,11 +1,2 @@
1
- export { default as ConstructorType } from "#ConstructorType";
2
- export { default as DefaultType } from "#DefaultType";
3
1
  export { default } from "#index";
4
- export { default as LiteralType } from "#LiteralType";
5
- export { default as ObjectType } from "#ObjectType";
6
- export { default as OptionalType } from "#OptionalType";
7
- export { default as PureType } from "#PureType";
8
- export { default as TupleType } from "#TupleType";
9
- export { default as UndefinedType } from "#UndefinedType";
10
- export { default as ArrayType } from "#ArrayType";
11
2
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,40 +1,43 @@
1
1
  {
2
2
  "name": "pema",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Primate schema validation",
5
5
  "homepage": "https://primate.run/docs/validation",
6
6
  "bugs": "https://github.com/primate-run/primate/issues",
7
+ "type": "module",
7
8
  "license": "MIT",
8
- "files": [
9
- "/lib/**/*.js",
10
- "/lib/**/*.d.ts",
11
- "!/**/*.spec.*"
12
- ],
13
9
  "repository": {
14
10
  "type": "git",
15
11
  "url": "https://github.com/primate-run/primate",
16
12
  "directory": "packages/pema"
17
13
  },
14
+ "files": [
15
+ "/lib/**/*.js",
16
+ "/lib/**/*.d.ts",
17
+ "!/**/*.spec.*"
18
+ ],
18
19
  "dependencies": {
19
- "@rcompat/assert": "^0.3.1",
20
- "@rcompat/cli": "^0.11.3",
21
- "@rcompat/is": "^0.1.4",
22
- "@rcompat/record": "^0.9.1"
20
+ "@rcompat/assert": "^0.8.1",
21
+ "@rcompat/cli": "^0.18.1",
22
+ "@rcompat/error": "^0.3.1",
23
+ "@rcompat/is": "^0.6.1"
24
+ },
25
+ "devDependencies": {
26
+ "@rcompat/type": "^0.11.1"
23
27
  },
24
- "type": "module",
25
28
  "imports": {
26
29
  "#*": {
27
- "apekit": "./src/private/*.ts",
30
+ "@primate/source": "./src/private/*.ts",
28
31
  "default": "./lib/private/*.js"
29
32
  }
30
33
  },
31
34
  "exports": {
32
35
  ".": {
33
- "apekit": "./src/public/index.ts",
36
+ "@primate/source": "./src/public/index.ts",
34
37
  "default": "./lib/public/index.js"
35
38
  },
36
39
  "./*": {
37
- "apekit": "./src/public/*.ts",
40
+ "@primate/source": "./src/public/*.ts",
38
41
  "default": "./lib/public/*.js"
39
42
  }
40
43
  },
@@ -43,6 +46,6 @@
43
46
  "clean": "rm -rf ./lib",
44
47
  "lint": "eslint .",
45
48
  "lint:fix": "eslint . --fix",
46
- "test": "npm run build && npx proby"
49
+ "test": "npx proby"
47
50
  }
48
51
  }
@@ -1,3 +0,0 @@
1
- type DecrementDepth = [never, 0, 1, 2, 3];
2
- export type { DecrementDepth as default };
3
- //# sourceMappingURL=DecrementDepth.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=DecrementDepth.js.map
@@ -1,20 +0,0 @@
1
- import type ArrayType from "#ArrayType";
2
- import type DecrementDepth from "#DecrementDepth";
3
- import type Infer from "#Infer";
4
- import type NullType from "#NullType";
5
- import type Parsed from "#Parsed";
6
- import type Schema from "#Schema";
7
- import type TupleType from "#TupleType";
8
- import type UndefinedType from "#UndefinedType";
9
- import type UndefinedToOptional from "@rcompat/type/UndefinedToOptional";
10
- type InferSchema<S, Depth extends number = 3> = [
11
- Depth
12
- ] extends [never] ? never : 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[] ? InferSchema<TupleType<{
13
- [K in keyof S]: S[K] extends Parsed<unknown> ? InferSchema<S[K], DecrementDepth[Depth]> : never;
14
- }>> : S extends {
15
- [key: string]: Schema;
16
- } ? UndefinedToOptional<{
17
- [K in keyof S]: InferSchema<S[K], DecrementDepth[Depth]>;
18
- }> : never;
19
- export type { InferSchema as default };
20
- //# sourceMappingURL=InferSchema.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=InferSchema.js.map
@@ -1,14 +0,0 @@
1
- import type Infer from "#Infer";
2
- import type ParseOptions from "#ParseOptions";
3
- import PrimitiveType from "#PrimitiveType";
4
- import Storeable from "#Storeable";
5
- export default class PrimaryType extends PrimitiveType<string | undefined, "PrimaryType"> implements Storeable<"primary"> {
6
- get name(): "primary";
7
- get datatype(): "primary";
8
- parse(x: unknown, options?: ParseOptions): Infer<this>;
9
- toJSON(): {
10
- type: "primary";
11
- datatype: "primary";
12
- };
13
- }
14
- //# sourceMappingURL=PrimaryType.d.ts.map
@@ -1,26 +0,0 @@
1
- import error from "#error";
2
- import ParseError from "#ParseError";
3
- import PrimitiveType from "#PrimitiveType";
4
- import Storeable from "#Storeable";
5
- export default class PrimaryType extends PrimitiveType {
6
- get name() {
7
- return "primary";
8
- }
9
- get datatype() {
10
- return "primary";
11
- }
12
- parse(x, options = {}) {
13
- // the primary type is an optional ype
14
- if (x === undefined) {
15
- return x;
16
- }
17
- if (typeof x !== "string") {
18
- throw new ParseError(error(this.name, x, options));
19
- }
20
- return x;
21
- }
22
- toJSON() {
23
- return Storeable.serialize(this);
24
- }
25
- }
26
- //# sourceMappingURL=PrimaryType.js.map
@@ -1,4 +0,0 @@
1
- export default class SchemaError extends Error {
2
- constructor(message: string, ...params: unknown[]);
3
- }
4
- //# sourceMappingURL=SchemaError.d.ts.map
@@ -1,8 +0,0 @@
1
- import mark from "@rcompat/cli/mark";
2
- export default class SchemaError extends Error {
3
- constructor(message, ...params) {
4
- super(mark(message, ...params));
5
- }
6
- }
7
- ;
8
- //# sourceMappingURL=SchemaError.js.map
@@ -1,3 +0,0 @@
1
- import ParseError from "#ParseError";
2
- export default function fail(input: unknown, msg: string): ParseError;
3
- //# sourceMappingURL=fail.d.ts.map