pema 0.5.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 (132) hide show
  1. package/lib/private/ArrayType.d.ts +1 -0
  2. package/lib/private/ArrayType.js +23 -27
  3. package/lib/private/BuiltinType.d.ts +0 -1
  4. package/lib/private/BuiltinType.js +2 -5
  5. package/lib/private/ConstructorType.d.ts +4 -1
  6. package/lib/private/ConstructorType.js +6 -2
  7. package/lib/private/DataType.d.ts +5 -0
  8. package/lib/private/DefaultType.js +3 -6
  9. package/lib/private/EnumType.d.ts +21 -0
  10. package/lib/private/EnumType.js +30 -0
  11. package/lib/private/FunctionType.d.ts +15 -0
  12. package/lib/private/FunctionType.js +20 -0
  13. package/lib/private/IsType.d.ts +19 -0
  14. package/lib/private/IsType.js +29 -0
  15. package/lib/private/Issue.d.ts +2 -0
  16. package/lib/private/IssueType.d.ts +3 -0
  17. package/lib/private/IssueType.js +2 -0
  18. package/lib/private/JSONType.d.ts +26 -0
  19. package/lib/private/JSONType.js +41 -0
  20. package/lib/private/LiteralType.d.ts +4 -0
  21. package/lib/private/LiteralType.js +10 -2
  22. package/lib/private/NormalizeSchema.d.ts +1 -1
  23. package/lib/private/NullType.js +2 -2
  24. package/lib/private/NumberType.d.ts +1 -1
  25. package/lib/private/NumberType.js +1 -1
  26. package/lib/private/ObjectType.d.ts +13 -4
  27. package/lib/private/ObjectType.js +18 -5
  28. package/lib/private/OmitType.js +4 -3
  29. package/lib/private/ParseError.d.ts +5 -1
  30. package/lib/private/ParseError.js +23 -13
  31. package/lib/private/Parsed.d.ts +1 -0
  32. package/lib/private/Parsed.js +3 -0
  33. package/lib/private/PartialType.js +5 -4
  34. package/lib/private/PrimitiveType.d.ts +0 -1
  35. package/lib/private/PrimitiveType.js +6 -14
  36. package/lib/private/RecordType.js +8 -27
  37. package/lib/private/Serialized.d.ts +18 -1
  38. package/lib/private/Storable.d.ts +2 -2
  39. package/lib/private/StoreType.js +2 -2
  40. package/lib/private/StringType.d.ts +0 -1
  41. package/lib/private/StringType.js +5 -11
  42. package/lib/private/TupleType.js +10 -8
  43. package/lib/private/UUIDType.d.ts +17 -0
  44. package/lib/private/UUIDType.js +33 -0
  45. package/lib/private/UUIDV4Type.d.ts +13 -0
  46. package/lib/private/UUIDV4Type.js +23 -0
  47. package/lib/private/UUIDV7Type.d.ts +13 -0
  48. package/lib/private/UUIDV7Type.js +23 -0
  49. package/lib/private/UnionType.js +5 -5
  50. package/lib/private/coerce/bigint.d.ts +1 -1
  51. package/lib/private/coerce/bigint.js +1 -1
  52. package/lib/private/coerce/float.d.ts +2 -0
  53. package/lib/private/coerce/float.js +8 -0
  54. package/lib/private/coerce/int.d.ts +1 -1
  55. package/lib/private/coerce/int.js +6 -3
  56. package/lib/private/enum.d.ts +3 -0
  57. package/lib/private/enum.js +5 -0
  58. package/lib/private/errors.d.ts +21 -0
  59. package/lib/private/errors.js +53 -0
  60. package/lib/private/function.d.ts +7 -0
  61. package/lib/private/function.js +6 -0
  62. package/lib/private/index.d.ts +79 -32
  63. package/lib/private/index.js +14 -0
  64. package/lib/private/is.d.ts +4 -0
  65. package/lib/private/is.js +3 -0
  66. package/lib/private/json/JSONIssue.d.ts +2 -0
  67. package/lib/private/json.d.ts +5 -0
  68. package/lib/private/json.js +5 -0
  69. package/lib/private/object.d.ts +1 -1
  70. package/lib/private/object.js +2 -2
  71. package/lib/private/schema-errors.d.ts +16 -0
  72. package/lib/private/schema-errors.js +17 -0
  73. package/lib/private/spec/bigint.d.ts +1 -1
  74. package/lib/private/spec/bigint.js +30 -41
  75. package/lib/private/spec/biguint.d.ts +1 -1
  76. package/lib/private/spec/biguint.js +29 -45
  77. package/lib/private/spec/int.d.ts +1 -1
  78. package/lib/private/spec/int.js +27 -38
  79. package/lib/private/spec/uint.d.ts +1 -1
  80. package/lib/private/spec/uint.js +31 -43
  81. package/lib/private/test.d.ts +34 -0
  82. package/lib/private/test.js +107 -0
  83. package/lib/private/uuid.d.ts +4 -0
  84. package/lib/private/uuid.js +3 -0
  85. package/lib/private/validator/ends-with.d.ts +1 -2
  86. package/lib/private/validator/ends-with.js +8 -10
  87. package/lib/private/validator/f32.js +3 -8
  88. package/lib/private/validator/integer.d.ts +1 -2
  89. package/lib/private/validator/integer.js +6 -10
  90. package/lib/private/validator/isotime.js +8 -10
  91. package/lib/private/validator/length.js +11 -19
  92. package/lib/private/validator/max.js +14 -15
  93. package/lib/private/validator/min.js +14 -15
  94. package/lib/private/validator/port.d.ts +1 -2
  95. package/lib/private/validator/port.js +6 -9
  96. package/lib/private/validator/range.js +5 -11
  97. package/lib/private/validator/regex.d.ts +1 -1
  98. package/lib/private/validator/regex.js +5 -9
  99. package/lib/private/validator/starts-with.d.ts +1 -2
  100. package/lib/private/validator/starts-with.js +8 -10
  101. package/lib/private/validator/unique-by.d.ts +1 -1
  102. package/lib/private/validator/unique-by.js +8 -14
  103. package/lib/private/validator/unique-with.d.ts +1 -1
  104. package/lib/private/validator/unique-with.js +3 -7
  105. package/lib/private/validator/unique.js +2 -6
  106. package/lib/private/validator/values.js +2 -6
  107. package/lib/public/StoreType.d.ts +1 -1
  108. package/lib/public/index.d.ts +1 -28
  109. package/lib/public/index.js +0 -16
  110. package/package.json +10 -9
  111. package/lib/private/SchemaError.d.ts +0 -4
  112. package/lib/private/SchemaError.js +0 -8
  113. package/lib/private/error/fail.d.ts +0 -3
  114. package/lib/private/error/fail.js +0 -5
  115. package/lib/private/error/schemafail.d.ts +0 -3
  116. package/lib/private/error/schemafail.js +0 -5
  117. package/lib/private/error.d.ts +0 -4
  118. package/lib/private/error.js +0 -11
  119. package/lib/private/expect.d.ts +0 -22
  120. package/lib/private/expect.js +0 -32
  121. package/lib/private/expected.d.ts +0 -3
  122. package/lib/private/expected.js +0 -11
  123. package/lib/private/fail.d.ts +0 -4
  124. package/lib/private/fail.js +0 -6
  125. package/lib/private/test/messages-of.d.ts +0 -3
  126. package/lib/private/test/messages-of.js +0 -4
  127. package/lib/private/test/paths-of.d.ts +0 -3
  128. package/lib/private/test/paths-of.js +0 -4
  129. package/lib/private/test/throws-issues.d.ts +0 -4
  130. package/lib/private/test/throws-issues.js +0 -13
  131. package/lib/private/validator/uuid.d.ts +0 -3
  132. package/lib/private/validator/uuid.js +0 -4
@@ -1,10 +1,14 @@
1
1
  import type JSONPayload from "#json/JSONPayload";
2
2
  import type ParseIssue from "#ParseIssue";
3
3
  import type { Serializable } from "@rcompat/type";
4
+ declare const brand: unique symbol;
4
5
  export default class ParseError extends Error implements Serializable {
5
6
  #private;
7
+ [brand]: boolean;
8
+ static is(error: unknown): error is ParseError;
6
9
  constructor(issues: ParseIssue[]);
7
- get issues(): ParseIssue[] | undefined;
10
+ get issues(): ParseIssue[];
8
11
  toJSON(): JSONPayload;
9
12
  }
13
+ export {};
10
14
  //# sourceMappingURL=ParseError.d.ts.map
@@ -1,3 +1,4 @@
1
+ import assert from "@rcompat/assert";
1
2
  function humanize(path) {
2
3
  return path === ""
3
4
  ? ""
@@ -9,14 +10,19 @@ function humanize(path) {
9
10
  .join("");
10
11
  }
11
12
  function stringify(issue) {
12
- // For root (scalar) errors, keep just the message;
13
+ // for root (scalar) errors, keep just the message;
13
14
  // otherwise prefix with humanized path
14
15
  return issue.path === ""
15
16
  ? issue.message
16
17
  : `${humanize(issue.path)}: ${issue.message}`;
17
18
  }
19
+ const brand = Symbol.for("pema/error/parse/v0");
18
20
  export default class ParseError extends Error {
19
- #issues;
21
+ [brand] = true;
22
+ #issues = [];
23
+ static is(error) {
24
+ return typeof error === "object" && error !== null && brand in error;
25
+ }
20
26
  constructor(issues) {
21
27
  super(stringify(issues[0]));
22
28
  this.name = "ParseError";
@@ -26,24 +32,28 @@ export default class ParseError extends Error {
26
32
  return this.#issues;
27
33
  }
28
34
  toJSON() {
29
- const issues = this.#issues ?? [];
30
- if (issues.length === 0) {
31
- return { message: "Parsing failed", messages: ["Parsing failed"] };
32
- }
35
+ const issues = this.#issues;
36
+ assert.uint(issues.length);
33
37
  const is_form = issues.some(i => i.path !== "");
34
38
  if (!is_form) {
35
39
  const messages = issues.map(i => i.message);
36
- return { message: messages[0], messages };
40
+ return {
41
+ type: issues[0].type,
42
+ message: messages[0],
43
+ messages,
44
+ };
37
45
  }
38
46
  const dict = {};
39
47
  for (const i of issues) {
40
48
  const key = i.path;
41
- if (!dict[key])
42
- dict[key] = { message: i.message, messages: [] };
43
- dict[key].messages.push(i.message);
44
- // Keep the first as the short message
45
- if (dict[key].messages.length === 1)
46
- dict[key].message = i.message;
49
+ if (!(key in dict))
50
+ dict[key] = {
51
+ type: i.type,
52
+ message: i.message,
53
+ messages: [],
54
+ };
55
+ const entry = dict[key];
56
+ entry.messages.push(i.message);
47
57
  }
48
58
  return dict;
49
59
  }
@@ -19,6 +19,7 @@ export default abstract class Parsed<StaticType> implements Serializable {
19
19
  * @returns The parsed value, if successfully parsed.
20
20
  */
21
21
  abstract parse(x: unknown, options?: ParseOptions): StaticType;
22
+ coerce(x: unknown, options?: ParseOptions): StaticType;
22
23
  abstract toJSON(): Serialized;
23
24
  }
24
25
  //# sourceMappingURL=Parsed.d.ts.map
@@ -13,5 +13,8 @@ export default class Parsed {
13
13
  [CoerceKey](x) {
14
14
  return x;
15
15
  }
16
+ coerce(x, options = {}) {
17
+ return this.parse(x, { ...options, coerce: true });
18
+ }
16
19
  }
17
20
  //# sourceMappingURL=Parsed.js.map
@@ -1,5 +1,5 @@
1
1
  import DefaultType from "#DefaultType";
2
- import fail from "#fail";
2
+ import E from "#errors";
3
3
  import ParsedKey from "#ParsedKey";
4
4
  import ParseError from "#ParseError";
5
5
  import join from "#path/join";
@@ -23,7 +23,7 @@ export default class PartialType extends VirtualType {
23
23
  }
24
24
  parse(x, options = {}) {
25
25
  if (!is.dict(x))
26
- throw fail("object", x, options);
26
+ throw E.invalid_type(x, "object", options);
27
27
  const input = x;
28
28
  const out = {};
29
29
  const issues = [];
@@ -37,9 +37,9 @@ export default class PartialType extends VirtualType {
37
37
  out[key] = parsed;
38
38
  }
39
39
  catch (e) {
40
- if (e instanceof ParseError) {
40
+ if (ParseError.is(e)) {
41
41
  // child already rebased to /<key> via nextOptions -> just collect
42
- if (e.issues && e.issues.length)
42
+ if (e.issues.length > 0)
43
43
  issues.push(...e.issues);
44
44
  }
45
45
  else {
@@ -49,6 +49,7 @@ export default class PartialType extends VirtualType {
49
49
  : String(e);
50
50
  issues.push({
51
51
  input: input[key],
52
+ type: "invalid_type",
52
53
  message: message,
53
54
  path: join(options[ParsedKey] ?? "", key),
54
55
  });
@@ -12,7 +12,6 @@ export default abstract class PrimitiveType<StaticType, Name extends string> ext
12
12
  get options(): ParseOptions<unknown>;
13
13
  get validators(): Validator<StaticType>[];
14
14
  derive(next: Next<StaticType>): this;
15
- get coerce(): this;
16
15
  parse(x: unknown, options?: ParseOptions<StaticType>): Infer<this>;
17
16
  }
18
17
  export {};
@@ -1,5 +1,5 @@
1
1
  import CoerceKey from "#CoerceKey";
2
- import error from "#error";
2
+ import E from "#errors";
3
3
  import ParsedKey from "#ParsedKey";
4
4
  import ParseError from "#ParseError";
5
5
  import Type from "#Type";
@@ -21,9 +21,6 @@ export default class PrimitiveType extends Type {
21
21
  const Constructor = this.constructor;
22
22
  return new Constructor([...this.#validators, ...next.validators ?? []], { ...this.#options, ...next.options ?? {} });
23
23
  }
24
- get coerce() {
25
- return this.derive({ options: { coerce: true } });
26
- }
27
24
  parse(x, options = {}) {
28
25
  // hotpath: avoid object spread when possible
29
26
  const has_instance_options = this.#options.coerce !== undefined
@@ -37,18 +34,17 @@ export default class PrimitiveType extends Type {
37
34
  ? option_validators.concat(this.#validators)
38
35
  : this.#validators;
39
36
  const $x = $options.coerce === true ? this[CoerceKey](x) : x;
40
- if (typeof $x !== this.name) {
41
- throw new ParseError(error(this.name, $x, $options));
42
- }
37
+ if (typeof $x !== this.name)
38
+ throw E.invalid_type($x, this.name, $options);
43
39
  const base = $options[ParsedKey] ?? "";
44
40
  for (let i = 0; i < validators.length; i++) {
45
41
  try {
46
42
  validators[i]($x);
47
43
  }
48
44
  catch (e) {
49
- if (e instanceof ParseError) {
45
+ if (ParseError.is(e)) {
50
46
  // rebase each issue path under `base`
51
- const rebased = (e.issues ?? []).map(issue => ({
47
+ const rebased = e.issues.map(issue => ({
52
48
  ...issue,
53
49
  path: issue.path === ""
54
50
  ? base
@@ -60,11 +56,7 @@ export default class PrimitiveType extends Type {
60
56
  const message = e && typeof e.message === "string"
61
57
  ? e.message
62
58
  : String(e);
63
- throw new ParseError([{
64
- input: x,
65
- message,
66
- path: base,
67
- }]);
59
+ throw E.invalid_type(x, message, base);
68
60
  }
69
61
  }
70
62
  return $x;
@@ -1,9 +1,7 @@
1
- import expected from "#expected";
2
- import fail from "#fail";
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
6
  import next from "#path/next";
9
7
  import is from "@rcompat/is";
@@ -22,35 +20,22 @@ export default class RecordType extends GenericType {
22
20
  return "record";
23
21
  }
24
22
  parse(x, options = {}) {
23
+ const path = options[ParsedKey] ?? "";
25
24
  if (!is.dict(x))
26
- throw fail("object", x, options);
25
+ throw E.invalid_type(x, "object", path);
27
26
  const key_name = this.#key.name;
28
27
  const keys = Object.keys(x);
29
28
  const symbols = Object.getOwnPropertySymbols(x);
30
- const base = options[ParsedKey] ?? "";
31
29
  if (key_name === "string" || key_name === "number") {
32
30
  // no key may be a symbol
33
- if (symbols.length > 0) {
34
- throw new ParseError([{
35
- input: x,
36
- message: expected(`${key_name} key`, symbols[0]),
37
- path: base,
38
- }]);
39
- }
31
+ if (symbols.length > 0)
32
+ throw E.invalid_type(x, `${key_name} key`, path);
40
33
  keys.forEach(k => {
41
34
  if (key_name === "string" && is.numeric(k)) {
42
- throw new ParseError([{
43
- input: x,
44
- message: expected("string key", +k),
45
- path: join(base, k),
46
- }]);
35
+ throw E.invalid_type(x, "string key", join(path, k));
47
36
  }
48
37
  if (key_name === "number" && !is.numeric(k)) {
49
- throw new ParseError([{
50
- input: x,
51
- message: expected("number key", k),
52
- path: join(base, k),
53
- }]);
38
+ throw E.invalid_type(x, "number key", join(path, k));
54
39
  }
55
40
  this.#value.parse(x[k], next(k, options));
56
41
  });
@@ -58,11 +43,7 @@ export default class RecordType extends GenericType {
58
43
  if (key_name === "symbol") {
59
44
  // disallow any non-symbol keys
60
45
  if (keys.length > 0) {
61
- throw new ParseError([{
62
- input: x,
63
- message: expected("symbol key", keys[0]),
64
- path: join(base, keys[0]),
65
- }]);
46
+ throw E.invalid_type(keys[0], "symbol key", join(path, keys[0]));
66
47
  }
67
48
  symbols.forEach(k => {
68
49
  this.#value.parse(x[k], options);
@@ -20,7 +20,15 @@ type PrimitiveSerialized = {
20
20
  type: "file";
21
21
  } & DT | {
22
22
  type: "url";
23
- } & DT;
23
+ } & DT | {
24
+ type: "uuid";
25
+ } & DT | {
26
+ type: "uuid_v4";
27
+ } & DT | {
28
+ type: "uuid_v7";
29
+ } & DT | {
30
+ type: "function";
31
+ };
24
32
  type NumberSerialized = {
25
33
  type: "number";
26
34
  } & DT;
@@ -61,11 +69,20 @@ type StructuralSerialized = {
61
69
  } | {
62
70
  type: "schema";
63
71
  of: Serialized;
72
+ } | {
73
+ type: "json";
74
+ datatype: "json";
75
+ of?: Serialized;
64
76
  } | {
65
77
  type: "omit";
66
78
  properties: Dict<Serialized>;
79
+ } | {
80
+ type: "enum";
81
+ values: string[];
67
82
  } | {
68
83
  type: "pure";
84
+ } | {
85
+ type: "is";
69
86
  };
70
87
  type Serialized = PrimitiveSerialized | NumberSerialized | BigIntSerialized | StructuralSerialized;
71
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 Storable<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;
@@ -1,6 +1,6 @@
1
1
  import ObjectType from "#ObjectType";
2
2
  import PartialType from "#PartialType";
3
- import fail from "#fail";
3
+ import E from "#errors";
4
4
  import next from "#path/next";
5
5
  import is from "@rcompat/is";
6
6
  export default class StoreType extends ObjectType {
@@ -15,7 +15,7 @@ export default class StoreType extends ObjectType {
15
15
  parse(x, options = {}) {
16
16
  const $options = { ...options };
17
17
  if (x !== undefined && !is.dict(x))
18
- throw fail("object", x, $options);
18
+ throw E.invalid_type(x, "object", $options);
19
19
  const input = x ?? {};
20
20
  const out = {};
21
21
  for (const k in this.properties) {
@@ -6,7 +6,6 @@ export default class StringType extends PrimitiveType<string, "StringType"> impl
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,5 +1,5 @@
1
- import schemafail from "#error/schemafail";
2
1
  import PrimitiveType from "#PrimitiveType";
2
+ import E from "#schema-errors";
3
3
  import Storable from "#Storable";
4
4
  import email from "#validator/email";
5
5
  import ends_with from "#validator/ends-with";
@@ -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,15 +32,13 @@ 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) {
@@ -1,4 +1,4 @@
1
- import fail from "#fail";
1
+ import E from "#errors";
2
2
  import GenericType from "#GenericType";
3
3
  import OptionalType from "#OptionalType";
4
4
  import next from "#path/next";
@@ -16,16 +16,18 @@ export default class TupleType extends GenericType {
16
16
  }
17
17
  parse(x, options = {}) {
18
18
  if (!Array.isArray(x))
19
- throw fail("array", x, options);
19
+ throw E.invalid_type(x, "array", options);
20
20
  const items = this.#items;
21
- const len = items.length;
21
+ const n = items.length;
22
+ const out = new Array(n);
22
23
  // validate each expected item
23
- for (let i = 0; i < len; i++)
24
- items[i].parse(x[i], next(i, options));
24
+ for (let i = 0; i < n; i++) {
25
+ out[i] = items[i].parse(x[i], next(i, options));
26
+ }
25
27
  // reject extra items
26
- if (x.length > len)
27
- throw fail("undefined", x[len], next(len, options));
28
- return x;
28
+ if (x.length > n)
29
+ throw E.invalid_type(x[n], "undefined", next(n, options));
30
+ return out;
29
31
  }
30
32
  toJSON() {
31
33
  return {
@@ -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
@@ -1,9 +1,10 @@
1
1
  import DefaultType from "#DefaultType";
2
- import fail from "#fail";
2
+ import E from "#errors";
3
3
  import GenericType from "#GenericType";
4
4
  import isParsedType from "#is-parsed-type";
5
5
  import OptionalType from "#OptionalType";
6
6
  import ParseError from "#ParseError";
7
+ import S from "#schema-errors";
7
8
  import assert from "@rcompat/assert";
8
9
  const print = (type) => {
9
10
  const parsed = isParsedType(type);
@@ -28,7 +29,7 @@ const union_error = (types) => `\`${types.map(t => isParsedType(t) ? t.name : pr
28
29
  export default class UnionType extends GenericType {
29
30
  #of;
30
31
  constructor(of) {
31
- assert.true(of.length > 1, "union type must have at least two members");
32
+ assert.true(of.length > 1, S.union_at_least_two_members());
32
33
  super();
33
34
  this.#of = of;
34
35
  }
@@ -51,14 +52,13 @@ export default class UnionType extends GenericType {
51
52
  return x;
52
53
  }
53
54
  catch (e) {
54
- if (!(e instanceof ParseError)) {
55
+ if (!ParseError.is(e))
55
56
  throw e;
56
- }
57
57
  // continue to next
58
58
  }
59
59
  }
60
60
  // all types failed
61
- throw fail(union_error(this.#of), x, options);
61
+ throw E.invalid_type(x, union_error(this.#of), options);
62
62
  }
63
63
  toJSON() {
64
64
  return { type: this.name, of: this.#of.map(t => t.toJSON()) };
@@ -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,5 +1,5 @@
1
1
  import is from "@rcompat/is";
2
- export default function coerceBigInt(x) {
2
+ export default function coerce_bigint(x) {
3
3
  // normalize from string to number; other types unaffected
4
4
  const n = is.numeric(x) ? Number(x) : x;
5
5
  // normalize from number to bigint; other types unaffected
@@ -0,0 +1,2 @@
1
+ export default function coerceFloat(x: unknown): unknown;
2
+ //# sourceMappingURL=float.d.ts.map
@@ -0,0 +1,8 @@
1
+ import is from "@rcompat/is";
2
+ export default function coerceFloat(x) {
3
+ const n = is.numeric(x) ? Number(x) : x;
4
+ if (is.number(n))
5
+ return n;
6
+ return x;
7
+ }
8
+ //# sourceMappingURL=float.js.map
@@ -1,2 +1,2 @@
1
- export default function coerceInt(x: unknown): unknown;
1
+ export default function coerce_int(x: unknown): unknown;
2
2
  //# sourceMappingURL=int.d.ts.map
@@ -1,7 +1,10 @@
1
1
  import is from "@rcompat/is";
2
- export default function coerceInt(x) {
3
- if (is.numeric(x))
4
- return Number(x);
2
+ export default function coerce_int(x) {
3
+ // normalize from string to number; other types unaffected
4
+ const n = is.numeric(x) ? Number(x) : x;
5
+ // if is integer, return it
6
+ if (typeof n === "number" && is.int(n))
7
+ return n;
5
8
  return x;
6
9
  }
7
10
  //# sourceMappingURL=int.js.map
@@ -0,0 +1,3 @@
1
+ import EnumType from "#EnumType";
2
+ export default function _enum<T extends readonly string[]>(values: T): EnumType<T>;
3
+ //# sourceMappingURL=enum.d.ts.map
@@ -0,0 +1,5 @@
1
+ import EnumType from "#EnumType";
2
+ export default function _enum(values) {
3
+ return new EnumType(values);
4
+ }
5
+ //# sourceMappingURL=enum.js.map