pema 0.6.1 → 0.8.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 (175) hide show
  1. package/lib/private/ArrayType.d.ts +6 -3
  2. package/lib/private/ArrayType.js +15 -6
  3. package/lib/private/BigIntType.d.ts +1 -1
  4. package/lib/private/BigUintType.d.ts +1 -1
  5. package/lib/private/BooleanType.d.ts +5 -1
  6. package/lib/private/BooleanType.js +6 -0
  7. package/lib/private/BuiltinType.d.ts +1 -1
  8. package/lib/private/BuiltinType.js +6 -2
  9. package/lib/private/ConstructorType.d.ts +1 -1
  10. package/lib/private/ConstructorType.js +3 -1
  11. package/lib/private/DateType.d.ts +4 -1
  12. package/lib/private/DateType.js +6 -0
  13. package/lib/private/DefaultType.d.ts +1 -1
  14. package/lib/private/DefaultType.js +6 -5
  15. package/lib/private/EnumType.d.ts +1 -1
  16. package/lib/private/EnumType.js +5 -2
  17. package/lib/private/FunctionType.d.ts +1 -1
  18. package/lib/private/FunctionType.js +6 -3
  19. package/lib/private/IntType.d.ts +2 -2
  20. package/lib/private/IntType.js +2 -2
  21. package/lib/private/IsType.d.ts +1 -1
  22. package/lib/private/IsType.js +3 -1
  23. package/lib/private/JSONType.d.ts +1 -1
  24. package/lib/private/JSONType.js +3 -1
  25. package/lib/private/LiteralType.d.ts +6 -4
  26. package/lib/private/LiteralType.js +10 -8
  27. package/lib/private/Loose.d.ts +4 -0
  28. package/lib/private/Loose.js +3 -0
  29. package/lib/private/Mode.d.ts +3 -0
  30. package/lib/private/Mode.js +2 -0
  31. package/lib/private/NormalizeSchema.d.ts +3 -3
  32. package/lib/private/NullType.d.ts +1 -1
  33. package/lib/private/NullType.js +5 -2
  34. package/lib/private/NumberType.d.ts +1 -1
  35. package/lib/private/NumericType.d.ts +7 -3
  36. package/lib/private/NumericType.js +5 -2
  37. package/lib/private/ObjectType.d.ts +8 -5
  38. package/lib/private/ObjectType.js +17 -6
  39. package/lib/private/OmitType.d.ts +6 -3
  40. package/lib/private/OmitType.js +7 -2
  41. package/lib/private/OptionalType.d.ts +1 -1
  42. package/lib/private/OptionalType.js +6 -5
  43. package/lib/private/ParseError.d.ts +1 -1
  44. package/lib/private/ParseError.js +9 -8
  45. package/lib/private/ParseOptions.d.ts +2 -1
  46. package/lib/private/Parsed.d.ts +4 -3
  47. package/lib/private/Parsed.js +2 -3
  48. package/lib/private/PartialType.d.ts +6 -3
  49. package/lib/private/PartialType.js +7 -2
  50. package/lib/private/PrimitiveType.d.ts +1 -1
  51. package/lib/private/PrimitiveType.js +7 -4
  52. package/lib/private/PureType.d.ts +1 -1
  53. package/lib/private/PureType.js +2 -2
  54. package/lib/private/RecordType.d.ts +8 -5
  55. package/lib/private/RecordType.js +7 -2
  56. package/lib/private/Serialized.d.ts +13 -13
  57. package/lib/private/Storable.d.ts +1 -0
  58. package/lib/private/Storable.js +4 -0
  59. package/lib/private/StoreType.d.ts +2 -2
  60. package/lib/private/StoreType.js +4 -2
  61. package/lib/private/TupleType.d.ts +6 -3
  62. package/lib/private/TupleType.js +14 -5
  63. package/lib/private/URLType.d.ts +6 -1
  64. package/lib/private/URLType.js +21 -0
  65. package/lib/private/UUIDType.d.ts +1 -1
  66. package/lib/private/UUIDType.js +3 -1
  67. package/lib/private/UUIDV4Type.d.ts +1 -1
  68. package/lib/private/UUIDV4Type.js +3 -1
  69. package/lib/private/UUIDV7Type.d.ts +1 -1
  70. package/lib/private/UUIDV7Type.js +3 -1
  71. package/lib/private/UintType.d.ts +2 -2
  72. package/lib/private/UintType.js +2 -2
  73. package/lib/private/UnionType.d.ts +6 -3
  74. package/lib/private/UnionType.js +10 -5
  75. package/lib/private/VirtualType.js +2 -2
  76. package/lib/private/array.d.ts +6 -4
  77. package/lib/private/array.js +10 -2
  78. package/lib/private/blob.d.ts +6 -2
  79. package/lib/private/blob.js +5 -1
  80. package/lib/private/boolean.d.ts +5 -4
  81. package/lib/private/boolean.js +5 -4
  82. package/lib/private/coerce/date.js +3 -5
  83. package/lib/private/coerce/int.js +1 -1
  84. package/lib/private/constructor.d.ts +6 -5
  85. package/lib/private/constructor.js +11 -1
  86. package/lib/private/date.d.ts +6 -2
  87. package/lib/private/date.js +5 -1
  88. package/lib/private/dict.d.ts +12 -2
  89. package/lib/private/dict.js +10 -2
  90. package/lib/private/enum.d.ts +9 -1
  91. package/lib/private/enum.js +8 -1
  92. package/lib/private/errors.js +5 -6
  93. package/lib/private/f32.d.ts +6 -2
  94. package/lib/private/f32.js +5 -1
  95. package/lib/private/f64.d.ts +6 -2
  96. package/lib/private/f64.js +5 -1
  97. package/lib/private/file.d.ts +6 -2
  98. package/lib/private/file.js +5 -1
  99. package/lib/private/function.d.ts +6 -5
  100. package/lib/private/function.js +5 -1
  101. package/lib/private/i128.d.ts +6 -2
  102. package/lib/private/i128.js +5 -1
  103. package/lib/private/i16.d.ts +6 -2
  104. package/lib/private/i16.js +5 -1
  105. package/lib/private/i32.d.ts +6 -2
  106. package/lib/private/i32.js +5 -1
  107. package/lib/private/i64.d.ts +6 -2
  108. package/lib/private/i64.js +5 -1
  109. package/lib/private/i8.d.ts +6 -2
  110. package/lib/private/i8.js +5 -1
  111. package/lib/private/index.d.ts +257 -41
  112. package/lib/private/index.js +148 -41
  113. package/lib/private/is-parsed.d.ts +3 -0
  114. package/lib/private/is-parsed.js +7 -0
  115. package/lib/private/is.d.ts +6 -2
  116. package/lib/private/is.js +9 -1
  117. package/lib/private/json.d.ts +12 -2
  118. package/lib/private/json.js +9 -1
  119. package/lib/private/literal.d.ts +9 -1
  120. package/lib/private/literal.js +10 -1
  121. package/lib/private/normalize.js +4 -11
  122. package/lib/private/null.d.ts +6 -2
  123. package/lib/private/null.js +5 -1
  124. package/lib/private/object.d.ts +11 -4
  125. package/lib/private/object.js +17 -1
  126. package/lib/private/omit.d.ts +9 -1
  127. package/lib/private/omit.js +9 -1
  128. package/lib/private/partial.d.ts +9 -2
  129. package/lib/private/partial.js +11 -2
  130. package/lib/private/path/next.d.ts +1 -7
  131. package/lib/private/pure.d.ts +9 -1
  132. package/lib/private/pure.js +9 -1
  133. package/lib/private/record.d.ts +6 -5
  134. package/lib/private/record.js +9 -1
  135. package/lib/private/resolve.d.ts +2 -0
  136. package/lib/private/resolve.js +7 -0
  137. package/lib/private/spec/bigint.d.ts +5 -1
  138. package/lib/private/spec/bigint.js +28 -26
  139. package/lib/private/spec/biguint.d.ts +5 -1
  140. package/lib/private/spec/biguint.js +30 -22
  141. package/lib/private/spec/int.d.ts +5 -1
  142. package/lib/private/spec/int.js +25 -23
  143. package/lib/private/spec/uint.d.ts +5 -1
  144. package/lib/private/spec/uint.js +23 -21
  145. package/lib/private/string.d.ts +6 -5
  146. package/lib/private/string.js +5 -4
  147. package/lib/private/symbol.d.ts +6 -2
  148. package/lib/private/symbol.js +5 -1
  149. package/lib/private/test.d.ts +4 -21
  150. package/lib/private/test.js +0 -44
  151. package/lib/private/tuple.d.ts +9 -2
  152. package/lib/private/tuple.js +11 -1
  153. package/lib/private/u128.d.ts +6 -2
  154. package/lib/private/u128.js +5 -1
  155. package/lib/private/u16.d.ts +6 -2
  156. package/lib/private/u16.js +5 -1
  157. package/lib/private/u32.d.ts +6 -5
  158. package/lib/private/u32.js +5 -4
  159. package/lib/private/u64.d.ts +6 -2
  160. package/lib/private/u64.js +5 -1
  161. package/lib/private/u8.d.ts +6 -2
  162. package/lib/private/u8.js +5 -1
  163. package/lib/private/undefined.d.ts +5 -1
  164. package/lib/private/undefined.js +4 -1
  165. package/lib/private/union.d.ts +12 -3
  166. package/lib/private/union.js +9 -1
  167. package/lib/private/unknown.d.ts +5 -4
  168. package/lib/private/unknown.js +4 -1
  169. package/lib/private/url.d.ts +6 -2
  170. package/lib/private/url.js +5 -1
  171. package/lib/private/uuid.d.ts +6 -2
  172. package/lib/private/uuid.js +5 -1
  173. package/package.json +8 -7
  174. package/lib/private/is-parsed-type.d.ts +0 -3
  175. package/lib/private/is-parsed-type.js +0 -6
@@ -1,6 +1,8 @@
1
1
  import GenericType from "#GenericType";
2
2
  import type Infer from "#Infer";
3
3
  import type InferInputSchema from "#InferInputSchema";
4
+ import Loose from "#Loose";
5
+ import type Mode from "#Mode";
4
6
  import OptionalType from "#OptionalType";
5
7
  import type Parsed from "#Parsed";
6
8
  import type ParseOptions from "#ParseOptions";
@@ -10,19 +12,20 @@ import type { Dict, Unpack } from "@rcompat/type";
10
12
  type ObjectInfer<P extends Dict<Parsed<unknown>>> = {
11
13
  [K in keyof P]: P[K]["infer"];
12
14
  };
13
- export default class ObjectType<P extends Dict<Parsed<unknown>>, I = ObjectInfer<P>> extends GenericType<P, I, "ObjectType"> implements OptionalTrait {
15
+ export default class ObjectType<P extends Dict<Parsed<unknown>> = Dict<Parsed<unknown>>, M extends Mode = undefined, I = ObjectInfer<P>> extends GenericType<P, I, "ObjectType"> implements OptionalTrait {
14
16
  #private;
17
+ [Loose]: M;
15
18
  readonly Complement: ObjectType<Record<Exclude<string, keyof P>, Parsed<unknown>>>;
16
- constructor(properties: P, options?: ParseOptions);
19
+ constructor(properties: P, mode?: M, options?: ParseOptions);
17
20
  get name(): string;
18
21
  get properties(): P;
19
22
  get input(): InferInputSchema<P>;
20
23
  optional(): OptionalType<this>;
21
- shape<T>(): ObjectType<P, T>;
24
+ shape<T>(): ObjectType<P, M, T>;
22
25
  extend<E extends {
23
26
  [K in keyof E]: K extends keyof P ? never : Parsed<unknown>;
24
- }>(extra: E | ObjectType<E>): ObjectType<Unpack<P & E>>;
25
- parse(x: unknown, options?: ParseOptions): Infer<this>;
27
+ }>(extra: E | ObjectType<E, M>): ObjectType<Unpack<P & E>, M>;
28
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
26
29
  toJSON(): {
27
30
  type: "object";
28
31
  properties: Dict<Serialized>;
@@ -1,15 +1,19 @@
1
1
  import E from "#errors";
2
2
  import GenericType from "#GenericType";
3
+ import Loose from "#Loose";
3
4
  import OptionalType from "#OptionalType";
4
5
  import next from "#path/next";
6
+ import resolve from "#resolve";
5
7
  import SE from "#schema-errors";
6
8
  import is from "@rcompat/is";
7
9
  export default class ObjectType extends GenericType {
8
10
  #properties;
9
11
  #options;
10
- constructor(properties, options = {}) {
12
+ [Loose];
13
+ constructor(properties, mode, options = {}) {
11
14
  super();
12
15
  this.#properties = properties;
16
+ this[Loose] = mode;
13
17
  this.#options = options;
14
18
  }
15
19
  get name() {
@@ -26,13 +30,17 @@ export default class ObjectType extends GenericType {
26
30
  }
27
31
  #derive(options) {
28
32
  const Constructor = this.constructor;
29
- return new Constructor(this.#properties, { ...this.#options, ...options });
33
+ const instance = new Constructor(this.#properties, this[Loose], {
34
+ ...this.#options, ...options,
35
+ });
36
+ instance[Loose] = this[Loose];
37
+ return instance;
30
38
  }
31
39
  optional() {
32
40
  return new OptionalType(this);
33
41
  }
34
42
  shape() {
35
- return new ObjectType(this.#properties, this.#options);
43
+ return new ObjectType(this.#properties, this[Loose], this.#options);
36
44
  }
37
45
  extend(extra) {
38
46
  const properties = extra instanceof ObjectType ? extra.properties : extra;
@@ -40,10 +48,13 @@ export default class ObjectType extends GenericType {
40
48
  if (key in this.#properties)
41
49
  throw SE.extend_key_collision(key);
42
50
  }
43
- return new ObjectType({ ...this.#properties, ...properties }, this.#options);
51
+ return new ObjectType({ ...this.#properties, ...properties }, this[Loose], this.#options);
44
52
  }
45
- parse(x, options = {}) {
46
- const $options = { ...this.#options, ...options };
53
+ parse(u, options = {}) {
54
+ const x = resolve(u);
55
+ const $options = this[Loose] !== undefined
56
+ ? { ...this.#options, ...options, [Loose]: this[Loose] }
57
+ : { ...this.#options, ...options };
47
58
  if (is.defined(x) && !is.dict(x))
48
59
  throw E.invalid_type(x, "object", $options);
49
60
  const input = x ?? {};
@@ -1,17 +1,20 @@
1
1
  import GenericType from "#GenericType";
2
2
  import type Infer from "#Infer";
3
+ import Loose from "#Loose";
4
+ import type Mode from "#Mode";
3
5
  import type ObjectType from "#ObjectType";
4
6
  import type Parsed from "#Parsed";
5
7
  import type ParseOptions from "#ParseOptions";
6
8
  import type Serialized from "#Serialized";
7
9
  import type { Dict } from "@rcompat/type";
8
- export default class OmitType<P extends Dict<Parsed<unknown>>, K extends keyof P> extends GenericType<Omit<P, K>, Omit<{
10
+ export default class OmitType<P extends Dict<Parsed<unknown>>, K extends keyof P, M extends Mode = undefined> extends GenericType<Omit<P, K>, Omit<{
9
11
  [Key in keyof P]: Infer<P[Key]>;
10
12
  }, K>, "OmitType"> {
11
13
  #private;
12
- constructor(type: ObjectType<P>, keys: K[]);
14
+ [Loose]: M;
15
+ constructor(type: ObjectType<P>, keys: K[], mode?: M);
13
16
  get name(): "omit";
14
- parse(x: unknown, options?: ParseOptions): Infer<this>;
17
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
15
18
  toJSON(): {
16
19
  type: "omit";
17
20
  properties: Dict<Serialized>;
@@ -1,22 +1,27 @@
1
1
  import E from "#errors";
2
2
  import GenericType from "#GenericType";
3
+ import Loose from "#Loose";
3
4
  import ParsedKey from "#ParsedKey";
4
5
  import join from "#path/join";
6
+ import resolve from "#resolve";
5
7
  import is from "@rcompat/is";
6
8
  export default class OmitType extends GenericType {
7
9
  #properties;
8
- constructor(type, keys) {
10
+ [Loose];
11
+ constructor(type, keys, mode) {
9
12
  super();
10
13
  const props = { ...type.properties };
11
14
  for (const key of keys) {
12
15
  delete props[key];
13
16
  }
14
17
  this.#properties = props;
18
+ this[Loose] = mode;
15
19
  }
16
20
  get name() {
17
21
  return "omit";
18
22
  }
19
- parse(x, options = {}) {
23
+ parse(u, options = {}) {
24
+ const x = resolve(u);
20
25
  if (!is.dict(x))
21
26
  throw E.invalid_type(x, "object", options);
22
27
  const out = {};
@@ -8,7 +8,7 @@ export default class OptionalType<S extends Parsed<unknown>> extends VirtualType
8
8
  get name(): "optional";
9
9
  get schema(): S;
10
10
  get nullable(): boolean;
11
- parse(x: unknown, options?: ParseOptions): Infer<this>;
11
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
12
12
  toJSON(): {
13
13
  type: "optional";
14
14
  of: import("./Serialized.js").default;
@@ -1,4 +1,6 @@
1
+ import resolve from "#resolve";
1
2
  import VirtualType from "#VirtualType";
3
+ import is from "@rcompat/is";
2
4
  export default class OptionalType extends VirtualType {
3
5
  #schema;
4
6
  constructor(s) {
@@ -14,13 +16,12 @@ export default class OptionalType extends VirtualType {
14
16
  get nullable() {
15
17
  return true;
16
18
  }
17
- parse(x, options = {}) {
18
- const s = this.#schema;
19
+ parse(u, options = {}) {
20
+ const x = resolve(u);
19
21
  // optional
20
- if (x === undefined) {
22
+ if (is.undefined(x))
21
23
  return undefined;
22
- }
23
- return s.parse(x, options);
24
+ return this.#schema.parse(x, options);
24
25
  }
25
26
  toJSON() {
26
27
  return {
@@ -5,7 +5,7 @@ declare const brand: unique symbol;
5
5
  export default class ParseError extends Error implements Serializable {
6
6
  #private;
7
7
  [brand]: boolean;
8
- static is(error: unknown): error is ParseError;
8
+ static is(x: unknown): x is ParseError;
9
9
  constructor(issues: ParseIssue[]);
10
10
  get issues(): ParseIssue[];
11
11
  toJSON(): JSONPayload;
@@ -1,4 +1,5 @@
1
1
  import assert from "@rcompat/assert";
2
+ import is from "@rcompat/is";
2
3
  function humanize(path) {
3
4
  return path === ""
4
5
  ? ""
@@ -16,12 +17,12 @@ function stringify(issue) {
16
17
  ? issue.message
17
18
  : `${humanize(issue.path)}: ${issue.message}`;
18
19
  }
19
- const brand = Symbol.for("pema/error/parse/v0");
20
+ const brand = Symbol.for("pema/ParseError/v0");
20
21
  export default class ParseError extends Error {
21
22
  [brand] = true;
22
23
  #issues = [];
23
- static is(error) {
24
- return typeof error === "object" && error !== null && brand in error;
24
+ static is(x) {
25
+ return is.branded(x, brand);
25
26
  }
26
27
  constructor(issues) {
27
28
  super(stringify(issues[0]));
@@ -43,19 +44,19 @@ export default class ParseError extends Error {
43
44
  messages,
44
45
  };
45
46
  }
46
- const dict = {};
47
+ const payload = {};
47
48
  for (const i of issues) {
48
49
  const key = i.path;
49
- if (!(key in dict))
50
- dict[key] = {
50
+ if (!(key in payload))
51
+ payload[key] = {
51
52
  type: i.type,
52
53
  message: i.message,
53
54
  messages: [],
54
55
  };
55
- const entry = dict[key];
56
+ const entry = payload[key];
56
57
  entry.messages.push(i.message);
57
58
  }
58
- return dict;
59
+ return payload;
59
60
  }
60
61
  }
61
62
  //# sourceMappingURL=ParseError.js.map
@@ -1,9 +1,10 @@
1
+ import type Loose from "#Loose";
1
2
  import type ParsedKey from "#ParsedKey";
2
3
  import type Validator from "#Validator";
3
4
  import type { JSONPointer } from "@rcompat/type";
4
5
  export default interface ParseOptions<T = unknown> {
5
- coerce?: boolean;
6
6
  [ParsedKey]?: JSONPointer;
7
+ [Loose]?: boolean;
7
8
  partial?: boolean;
8
9
  validators?: Validator<T>[];
9
10
  }
@@ -1,4 +1,5 @@
1
1
  import CoerceKey from "#CoerceKey";
2
+ import Loose from "#Loose";
2
3
  import ParsedKey from "#ParsedKey";
3
4
  import type ParseOptions from "#ParseOptions";
4
5
  import type Serialized from "#Serialized";
@@ -7,19 +8,19 @@ export default abstract class Parsed<StaticType> implements Serializable {
7
8
  get [ParsedKey](): "ParsedKey";
8
9
  get infer(): StaticType;
9
10
  get nullable(): boolean;
11
+ [Loose]: boolean | undefined;
10
12
  [CoerceKey](x: unknown): unknown;
11
13
  abstract get name(): string;
12
14
  /**
13
15
  * Parse the given value.
14
16
  *
15
- * @param x Value to parse.
17
+ * @param u Value to parse.
16
18
  *
17
19
  * @throws `ParseError` if the value could not be parsed.
18
20
  *
19
21
  * @returns The parsed value, if successfully parsed.
20
22
  */
21
- abstract parse(x: unknown, options?: ParseOptions): StaticType;
22
- coerce(x: unknown, options?: ParseOptions): StaticType;
23
+ abstract parse(u: unknown, options?: ParseOptions): StaticType;
23
24
  abstract toJSON(): Serialized;
24
25
  }
25
26
  //# sourceMappingURL=Parsed.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import CoerceKey from "#CoerceKey";
2
+ import Loose from "#Loose";
2
3
  import ParsedKey from "#ParsedKey";
3
4
  export default class Parsed {
4
5
  get [ParsedKey]() {
@@ -10,11 +11,9 @@ export default class Parsed {
10
11
  get nullable() {
11
12
  return false;
12
13
  }
14
+ [Loose] = undefined;
13
15
  [CoerceKey](x) {
14
16
  return x;
15
17
  }
16
- coerce(x, options = {}) {
17
- return this.parse(x, { ...options, coerce: true });
18
- }
19
18
  }
20
19
  //# sourceMappingURL=Parsed.js.map
@@ -1,5 +1,7 @@
1
1
  import DefaultType from "#DefaultType";
2
2
  import type Infer from "#Infer";
3
+ import Loose from "#Loose";
4
+ import type Mode from "#Mode";
3
5
  import type ParseOptions from "#ParseOptions";
4
6
  import type Partialable from "#Partialable";
5
7
  import type DefaultTrait from "#trait/Default";
@@ -7,13 +9,14 @@ import VirtualType from "#VirtualType";
7
9
  type InferPartial<D extends Partialable> = {
8
10
  -readonly [K in keyof D]?: NonNullable<Infer<D[K]>>;
9
11
  };
10
- export default class PartialType<D extends Partialable> extends VirtualType<D, InferPartial<D>, "PartialType"> implements DefaultTrait<InferPartial<D>> {
12
+ export default class PartialType<D extends Partialable, M extends Mode = undefined> extends VirtualType<D, InferPartial<D>, "PartialType"> implements DefaultTrait<InferPartial<D>> {
11
13
  #private;
12
- constructor(spec: D);
14
+ [Loose]: M;
15
+ constructor(spec: D, mode?: M);
13
16
  get name(): "partial";
14
17
  get schema(): D;
15
18
  default(value: (() => InferPartial<D>) | InferPartial<D>): DefaultType<this, InferPartial<D>>;
16
- parse(x: unknown, options?: ParseOptions): InferPartial<D>;
19
+ parse(u: unknown, options?: ParseOptions): InferPartial<D>;
17
20
  toJSON(): {
18
21
  type: "partial";
19
22
  of: {
@@ -1,16 +1,20 @@
1
1
  import DefaultType from "#DefaultType";
2
2
  import E from "#errors";
3
+ import Loose from "#Loose";
3
4
  import ParsedKey from "#ParsedKey";
4
5
  import ParseError from "#ParseError";
5
6
  import join from "#path/join";
6
7
  import next from "#path/next";
8
+ import resolve from "#resolve";
7
9
  import VirtualType from "#VirtualType";
8
10
  import is from "@rcompat/is";
9
11
  export default class PartialType extends VirtualType {
10
12
  #spec;
11
- constructor(spec) {
13
+ [Loose];
14
+ constructor(spec, mode) {
12
15
  super();
13
16
  this.#spec = spec;
17
+ this[Loose] = mode;
14
18
  }
15
19
  get name() {
16
20
  return "partial";
@@ -21,7 +25,8 @@ export default class PartialType extends VirtualType {
21
25
  default(value) {
22
26
  return new DefaultType(this, value);
23
27
  }
24
- parse(x, options = {}) {
28
+ parse(u, options = {}) {
29
+ const x = resolve(u);
25
30
  if (!is.dict(x))
26
31
  throw E.invalid_type(x, "object", options);
27
32
  const input = x;
@@ -12,7 +12,7 @@ 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
- parse(x: unknown, options?: ParseOptions<StaticType>): Infer<this>;
15
+ parse(u: unknown, options?: ParseOptions<StaticType>): Infer<this>;
16
16
  }
17
17
  export {};
18
18
  //# sourceMappingURL=PrimitiveType.d.ts.map
@@ -1,7 +1,9 @@
1
1
  import CoerceKey from "#CoerceKey";
2
2
  import E from "#errors";
3
+ import Loose from "#Loose";
3
4
  import ParsedKey from "#ParsedKey";
4
5
  import ParseError from "#ParseError";
6
+ import resolve from "#resolve";
5
7
  import Type from "#Type";
6
8
  export default class PrimitiveType extends Type {
7
9
  #validators;
@@ -21,10 +23,10 @@ export default class PrimitiveType extends Type {
21
23
  const Constructor = this.constructor;
22
24
  return new Constructor([...this.#validators, ...next.validators ?? []], { ...this.#options, ...next.options ?? {} });
23
25
  }
24
- parse(x, options = {}) {
26
+ parse(u, options = {}) {
27
+ const x = resolve(u);
25
28
  // hotpath: avoid object spread when possible
26
- const has_instance_options = this.#options.coerce !== undefined
27
- || this.#options[ParsedKey] !== undefined;
29
+ const has_instance_options = this.#options[ParsedKey] !== undefined;
28
30
  const $options = has_instance_options
29
31
  ? { ...this.#options, ...options }
30
32
  : options;
@@ -33,7 +35,8 @@ export default class PrimitiveType extends Type {
33
35
  const validators = option_validators && option_validators.length > 0
34
36
  ? option_validators.concat(this.#validators)
35
37
  : this.#validators;
36
- const $x = $options.coerce === true ? this[CoerceKey](x) : x;
38
+ const loose = this[Loose] ?? $options[Loose] ?? false;
39
+ const $x = loose ? this[CoerceKey](x) : x;
37
40
  if (typeof $x !== this.name)
38
41
  throw E.invalid_type($x, this.name, $options);
39
42
  const base = $options[ParsedKey] ?? "";
@@ -7,7 +7,7 @@ export default class PureType<Type, Name extends string = "PureType"> extends Pa
7
7
  get name(): string;
8
8
  get Name(): Name;
9
9
  optional(): OptionalType<this>;
10
- parse(x: unknown): Infer<this>;
10
+ parse(u: unknown): Infer<this>;
11
11
  toJSON(): {
12
12
  type: "pure";
13
13
  };
@@ -10,9 +10,9 @@ export default class PureType extends Parsed {
10
10
  optional() {
11
11
  return new OptionalType(this);
12
12
  }
13
- parse(x) {
13
+ parse(u) {
14
14
  // no parsing of static types
15
- return x;
15
+ return u;
16
16
  }
17
17
  toJSON() {
18
18
  return { type: "pure" };
@@ -5,22 +5,25 @@ import type Parsed from "#Parsed";
5
5
  import type ParseOptions from "#ParseOptions";
6
6
  import type RecordTypeKey from "#RecordTypeKey";
7
7
  import type OptionalTrait from "#trait/Optional";
8
- export default class RecordType<Key extends RecordTypeKey, Value extends Parsed<unknown>> extends GenericType<Value, Record<Infer<Key>, Infer<Value>>, "RecordType"> implements OptionalTrait {
8
+ import type Mode from "#Mode";
9
+ import Loose from "#Loose";
10
+ export default class RecordType<Key extends RecordTypeKey, Value extends Parsed<unknown>, M extends Mode = undefined> extends GenericType<Value, Record<Infer<Key>, Infer<Value>>, "RecordType"> implements OptionalTrait {
9
11
  #private;
10
- constructor(k: Key, v: Value);
12
+ [Loose]: M;
13
+ constructor(k: Key, v: Value, mode?: M);
11
14
  optional(): OptionalType<this>;
12
15
  get name(): "record";
13
- parse(x: unknown, options?: ParseOptions): Infer<this>;
16
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
14
17
  toJSON(): {
15
18
  type: "record";
16
19
  key: {
17
20
  type: "string";
18
21
  datatype: "string";
19
- } | {
20
- type: "symbol";
21
22
  } | {
22
23
  type: "number" | "bigint";
23
24
  datatype: "f64";
25
+ } | {
26
+ type: "symbol";
24
27
  };
25
28
  value: import("./Serialized.js").default;
26
29
  };
@@ -4,14 +4,18 @@ import OptionalType from "#OptionalType";
4
4
  import ParsedKey from "#ParsedKey";
5
5
  import join from "#path/join";
6
6
  import next from "#path/next";
7
+ import resolve from "#resolve";
7
8
  import is from "@rcompat/is";
9
+ import Loose from "#Loose";
8
10
  export default class RecordType extends GenericType {
9
11
  #key;
10
12
  #value;
11
- constructor(k, v) {
13
+ [Loose];
14
+ constructor(k, v, mode) {
12
15
  super();
13
16
  this.#key = k;
14
17
  this.#value = v;
18
+ this[Loose] = mode;
15
19
  }
16
20
  optional() {
17
21
  return new OptionalType(this);
@@ -19,7 +23,8 @@ export default class RecordType extends GenericType {
19
23
  get name() {
20
24
  return "record";
21
25
  }
22
- parse(x, options = {}) {
26
+ parse(u, options = {}) {
27
+ const x = resolve(u);
23
28
  const path = options[ParsedKey] ?? "";
24
29
  if (!is.dict(x))
25
30
  throw E.invalid_type(x, "object", path);
@@ -2,31 +2,31 @@ import type { Dict, JSONValue } from "@rcompat/type";
2
2
  type DT<D extends string = string> = {
3
3
  datatype?: D;
4
4
  };
5
- type PrimitiveSerialized = {
5
+ type PrimitiveSerialized = ({
6
6
  type: "string";
7
- } & DT | {
7
+ } & DT) | ({
8
8
  type: "boolean";
9
- } & DT | {
9
+ } & DT) | ({
10
10
  type: "symbol";
11
- } & DT | {
11
+ } & DT) | ({
12
12
  type: "null";
13
- } & DT | {
13
+ } & DT) | ({
14
14
  type: "undefined";
15
- } & DT | {
15
+ } & DT) | ({
16
16
  type: "date";
17
- } & DT | {
17
+ } & DT) | ({
18
18
  type: "blob";
19
- } & DT | {
19
+ } & DT) | ({
20
20
  type: "file";
21
- } & DT | {
21
+ } & DT) | ({
22
22
  type: "url";
23
- } & DT | {
23
+ } & DT) | ({
24
24
  type: "uuid";
25
- } & DT | {
25
+ } & DT) | ({
26
26
  type: "uuid_v4";
27
- } & DT | {
27
+ } & DT) | ({
28
28
  type: "uuid_v7";
29
- } & DT | {
29
+ } & DT) | {
30
30
  type: "function";
31
31
  };
32
32
  type NumberSerialized = {
@@ -11,5 +11,6 @@ export default abstract class Storable<TKey extends K = K, TValue = unknown> ext
11
11
  type: N;
12
12
  datatype: T;
13
13
  };
14
+ static is(x: unknown): x is Storable;
14
15
  }
15
16
  //# sourceMappingURL=Storable.d.ts.map
@@ -1,7 +1,11 @@
1
1
  import Parsed from "#Parsed";
2
+ import is from "@rcompat/is";
2
3
  export default class Storable extends Parsed {
3
4
  static serialize(s) {
4
5
  return { type: s.name, datatype: s.datatype };
5
6
  }
7
+ static is(x) {
8
+ return is.object(x) && "datatype" in x;
9
+ }
6
10
  }
7
11
  //# sourceMappingURL=Storable.js.map
@@ -7,7 +7,7 @@ export default class StoreType<S extends StoreSchema> extends ObjectType<S> {
7
7
  #private;
8
8
  constructor(properties: S, pk?: string | null);
9
9
  get name(): string;
10
- parse(x: unknown, options?: ParseOptions): Infer<this>;
11
- partial(): PartialType<any>;
10
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
11
+ partial(): PartialType<any, undefined>;
12
12
  }
13
13
  //# sourceMappingURL=StoreType.d.ts.map
@@ -2,6 +2,7 @@ import ObjectType from "#ObjectType";
2
2
  import PartialType from "#PartialType";
3
3
  import E from "#errors";
4
4
  import next from "#path/next";
5
+ import resolve from "#resolve";
5
6
  import is from "@rcompat/is";
6
7
  export default class StoreType extends ObjectType {
7
8
  #pk;
@@ -12,9 +13,10 @@ export default class StoreType extends ObjectType {
12
13
  get name() {
13
14
  return "store";
14
15
  }
15
- parse(x, options = {}) {
16
+ parse(u, options = {}) {
17
+ const x = resolve(u);
16
18
  const $options = { ...options };
17
- if (x !== undefined && !is.dict(x))
19
+ if (is.defined(x) && !is.dict(x))
18
20
  throw E.invalid_type(x, "object", $options);
19
21
  const input = x ?? {};
20
22
  const out = {};
@@ -1,5 +1,7 @@
1
1
  import GenericType from "#GenericType";
2
2
  import type Infer from "#Infer";
3
+ import Loose from "#Loose";
4
+ import type Mode from "#Mode";
3
5
  import type NormalizeSchema from "#NormalizeSchema";
4
6
  import OptionalType from "#OptionalType";
5
7
  import type Parsed from "#Parsed";
@@ -9,12 +11,13 @@ import type OptionalTrait from "#trait/Optional";
9
11
  type InferTuple<T extends Schema[]> = {
10
12
  [K in keyof T]: T[K] extends Schema ? NormalizeSchema<T[K]>["infer"] : "tuple-never";
11
13
  };
12
- export default class TupleType<T extends Parsed<unknown>[]> extends GenericType<T, InferTuple<T>, "TupleType"> implements OptionalTrait {
14
+ export default class TupleType<T extends Parsed<unknown>[], M extends Mode = undefined> extends GenericType<T, InferTuple<T>, "TupleType"> implements OptionalTrait {
13
15
  #private;
14
- constructor(items: T);
16
+ [Loose]: M;
17
+ constructor(items: T, mode?: M);
15
18
  get name(): "tuple";
16
19
  optional(): OptionalType<this>;
17
- parse(x: unknown, options?: ParseOptions): Infer<this>;
20
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
18
21
  toJSON(): {
19
22
  type: "tuple";
20
23
  of: import("./Serialized.js").default[];