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 DefaultType from "#DefaultType";
2
2
  import GenericType from "#GenericType";
3
3
  import type Infer from "#Infer";
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";
@@ -11,9 +13,10 @@ import type { Primitive } from "@rcompat/type";
11
13
  type Next<T> = {
12
14
  validators?: Validator<T>[];
13
15
  };
14
- export default class ArrayType<T extends Parsed<unknown>> extends GenericType<T, Infer<T>[], "ArrayType"> implements OptionalTrait, DefaultTrait<Infer<T>[]> {
16
+ export default class ArrayType<T extends Parsed<unknown>, M extends Mode = undefined> extends GenericType<T, Infer<T>[], "ArrayType"> implements OptionalTrait, DefaultTrait<Infer<T>[]> {
15
17
  #private;
16
- constructor(item: T, validators?: Validator<Array<Infer<T>>>[]);
18
+ [Loose]: M;
19
+ constructor(item: T, mode?: M, validators?: Validator<Array<Infer<T>>>[]);
17
20
  get name(): "array";
18
21
  optional(): OptionalType<this>;
19
22
  default(value: (() => Infer<T>[]) | Infer<T>[]): DefaultType<this, Infer<T>[]>;
@@ -29,7 +32,7 @@ export default class ArrayType<T extends Parsed<unknown>> extends GenericType<T,
29
32
  min(limit: number): this;
30
33
  max(limit: number): this;
31
34
  length(from: number, to: number): this;
32
- parse(x: unknown, options?: ParseOptions): Infer<this>;
35
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
33
36
  toJSON(): {
34
37
  type: "array";
35
38
  of: import("./Serialized.js").default;
@@ -2,6 +2,7 @@ import DefaultType from "#DefaultType";
2
2
  import EnumType from "#EnumType";
3
3
  import E from "#errors";
4
4
  import GenericType from "#GenericType";
5
+ import Loose from "#Loose";
5
6
  import OptionalType from "#OptionalType";
6
7
  import ParsedKey from "#ParsedKey";
7
8
  import ParseError from "#ParseError";
@@ -9,21 +10,25 @@ import join from "#path/join";
9
10
  import next from "#path/next";
10
11
  import rebase from "#path/rebase";
11
12
  import PrimitiveType from "#PrimitiveType";
13
+ import resolve from "#resolve";
12
14
  import S from "#schema-errors";
13
15
  import length from "#validator/length";
14
16
  import max from "#validator/max";
15
17
  import min from "#validator/min";
16
18
  import unique from "#validator/unique";
17
19
  import uniqueBy from "#validator/unique-by";
20
+ import is from "@rcompat/is";
18
21
  function isPrimitive(x) {
19
22
  return x instanceof PrimitiveType || x instanceof EnumType;
20
23
  }
21
24
  export default class ArrayType extends GenericType {
22
25
  #item;
23
26
  #validators;
24
- constructor(item, validators = []) {
27
+ [Loose];
28
+ constructor(item, mode, validators = []) {
25
29
  super();
26
30
  this.#item = item;
31
+ this[Loose] = mode;
27
32
  this.#validators = validators;
28
33
  }
29
34
  get name() {
@@ -37,7 +42,7 @@ export default class ArrayType extends GenericType {
37
42
  }
38
43
  derive(_next) {
39
44
  const Constructor = this.constructor;
40
- return new Constructor(this.#item, [...this.#validators, ..._next.validators ?? []]);
45
+ return new Constructor(this.#item, this[Loose], [...this.#validators, ..._next.validators ?? []]);
41
46
  }
42
47
  /**
43
48
  * Member values are unique — only for primitive subtypes.
@@ -67,9 +72,13 @@ export default class ArrayType extends GenericType {
67
72
  length(from, to) {
68
73
  return this.derive({ validators: [length(from, to)] });
69
74
  }
70
- parse(x, options = {}) {
71
- const path = options[ParsedKey] ?? "";
72
- if (!Array.isArray(x))
75
+ parse(u, options = {}) {
76
+ const x = resolve(u);
77
+ const $options = this[Loose] !== undefined
78
+ ? { ...options, [Loose]: this[Loose] }
79
+ : options;
80
+ const path = $options[ParsedKey] ?? "";
81
+ if (!is.array(x))
73
82
  throw E.invalid_type(x, "array", path);
74
83
  const item = this.#item;
75
84
  const len = x.length;
@@ -77,7 +86,7 @@ export default class ArrayType extends GenericType {
77
86
  for (let i = 0; i < len; i++) {
78
87
  if (!(i in x))
79
88
  throw E.invalid_type(undefined, item.name, join(path, i));
80
- out[i] = item.parse(x[i], next(i, options));
89
+ out[i] = item.parse(x[i], next(i, $options));
81
90
  }
82
91
  const validators = this.#validators;
83
92
  for (let i = 0; i < validators.length; i++) {
@@ -3,7 +3,7 @@ import coerce from "#coerce/bigint";
3
3
  import CoerceKey from "#CoerceKey";
4
4
  import NumericType from "#NumericType";
5
5
  import type Storable from "#Storable";
6
- export default class BigIntType<T extends BigIntDataType = "i64"> extends NumericType<T, bigint, "BigIntType"> implements Storable<T> {
6
+ export default class BigIntType<T extends BigIntDataType = "i64", L extends boolean | undefined = undefined> extends NumericType<T, bigint, "BigIntType", L> implements Storable<T> {
7
7
  [CoerceKey]: typeof coerce;
8
8
  get name(): string;
9
9
  }
@@ -3,7 +3,7 @@ import coerce from "#coerce/bigint";
3
3
  import CoerceKey from "#CoerceKey";
4
4
  import NumericType from "#NumericType";
5
5
  import type Storable from "#Storable";
6
- export default class BigUintType<T extends BigUintDataType = "u64"> extends NumericType<T, bigint, "BigUintType"> implements Storable<T> {
6
+ export default class BigUintType<T extends BigUintDataType = "u64", L extends boolean | undefined = undefined> extends NumericType<T, bigint, "BigUintType", L> implements Storable<T> {
7
7
  [CoerceKey]: typeof coerce;
8
8
  get name(): string;
9
9
  }
@@ -1,7 +1,11 @@
1
1
  import CoerceKey from "#CoerceKey";
2
+ import Loose from "#Loose";
3
+ import type Mode from "#Mode";
2
4
  import PrimitiveType from "#PrimitiveType";
3
5
  import Storable from "#Storable";
4
- export default class BooleanType extends PrimitiveType<boolean, "BooleanType"> implements Storable<"boolean"> {
6
+ export default class BooleanType<M extends Mode = undefined> extends PrimitiveType<boolean, "BooleanType"> implements Storable<"boolean"> {
7
+ [Loose]: M;
8
+ constructor(mode?: M);
5
9
  get name(): "boolean";
6
10
  get datatype(): "boolean";
7
11
  [CoerceKey](x: unknown): unknown;
@@ -1,8 +1,14 @@
1
1
  import CoerceKey from "#CoerceKey";
2
+ import Loose from "#Loose";
2
3
  import PrimitiveType from "#PrimitiveType";
3
4
  import Storable from "#Storable";
4
5
  import is from "@rcompat/is";
5
6
  export default class BooleanType extends PrimitiveType {
7
+ [Loose];
8
+ constructor(mode) {
9
+ super();
10
+ this[Loose] = mode;
11
+ }
6
12
  get name() {
7
13
  return "boolean";
8
14
  }
@@ -6,6 +6,6 @@ export default abstract class BuiltinType<StaticType, Name extends string> exten
6
6
  #private;
7
7
  abstract get Type(): AbstractNewable;
8
8
  constructor(options?: ParseOptions);
9
- parse(x: unknown, options?: ParseOptions): Infer<this>;
9
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
10
10
  }
11
11
  //# sourceMappingURL=BuiltinType.d.ts.map
@@ -1,5 +1,7 @@
1
1
  import CoerceKey from "#CoerceKey";
2
2
  import E from "#errors";
3
+ import Loose from "#Loose";
4
+ import resolve from "#resolve";
3
5
  import Type from "#Type";
4
6
  export default class BuiltinType extends Type {
5
7
  #options;
@@ -11,9 +13,11 @@ export default class BuiltinType extends Type {
11
13
  const Constructor = this.constructor;
12
14
  return new Constructor({ ...this.#options, ...next });
13
15
  }
14
- parse(x, options = {}) {
16
+ parse(u, options = {}) {
17
+ const x = resolve(u);
15
18
  const $options = { ...this.#options, ...options };
16
- const $x = $options.coerce === true ? this[CoerceKey](x) : x;
19
+ const loose = this[Loose] ?? $options[Loose] ?? false;
20
+ const $x = loose ? this[CoerceKey](x) : x;
17
21
  if (!($x instanceof this.Type))
18
22
  throw E.invalid_type($x, this.name, $options);
19
23
  return $x;
@@ -12,7 +12,7 @@ export default class ConstructorType<C extends AbstractNewable> extends GenericT
12
12
  get name(): string;
13
13
  optional(): OptionalType<this>;
14
14
  default(value: (() => InstanceType<C>) | InstanceType<C>): DefaultType<this, InstanceType<C>>;
15
- parse(x: unknown, options?: ParseOptions): Infer<this>;
15
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
16
16
  toJSON(): {
17
17
  type: "newable";
18
18
  of: string;
@@ -2,6 +2,7 @@ import DefaultType from "#DefaultType";
2
2
  import E from "#errors";
3
3
  import GenericType from "#GenericType";
4
4
  import OptionalType from "#OptionalType";
5
+ import resolve from "#resolve";
5
6
  export default class ConstructorType extends GenericType {
6
7
  #type;
7
8
  constructor(t) {
@@ -17,7 +18,8 @@ export default class ConstructorType extends GenericType {
17
18
  default(value) {
18
19
  return new DefaultType(this, value);
19
20
  }
20
- parse(x, options = {}) {
21
+ parse(u, options = {}) {
22
+ const x = resolve(u);
21
23
  if (!(x instanceof this.#type))
22
24
  throw E.invalid_type(x, this.name, options);
23
25
  return x;
@@ -1,9 +1,12 @@
1
1
  import BuiltinType from "#BuiltinType";
2
2
  import coerce from "#coerce/date";
3
3
  import CoerceKey from "#CoerceKey";
4
+ import Loose from "#Loose";
4
5
  import Storable from "#Storable";
5
- export default class DateType extends BuiltinType<Date, "DateType"> implements Storable<"datetime"> {
6
+ export default class DateType<M extends boolean | undefined = undefined> extends BuiltinType<Date, "DateType"> implements Storable<"datetime"> {
6
7
  [CoerceKey]: typeof coerce;
8
+ [Loose]: M;
9
+ constructor(mode?: M);
7
10
  get Type(): DateConstructor;
8
11
  get name(): "date";
9
12
  get datatype(): "datetime";
@@ -1,9 +1,15 @@
1
1
  import BuiltinType from "#BuiltinType";
2
2
  import coerce from "#coerce/date";
3
3
  import CoerceKey from "#CoerceKey";
4
+ import Loose from "#Loose";
4
5
  import Storable from "#Storable";
5
6
  export default class DateType extends BuiltinType {
6
7
  [CoerceKey] = coerce;
8
+ [Loose];
9
+ constructor(mode) {
10
+ super();
11
+ this[Loose] = mode;
12
+ }
7
13
  get Type() {
8
14
  return Date;
9
15
  }
@@ -8,7 +8,7 @@ export default class DefaultType<S extends Parsed<unknown>, D extends Infer<S>>
8
8
  get name(): "default";
9
9
  get schema(): S;
10
10
  get input(): Infer<S> | undefined;
11
- parse(x: unknown, options?: ParseOptions): Infer<this>;
11
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
12
12
  toJSON(): {
13
13
  type: "default";
14
14
  of: import("./Serialized.js").default;
@@ -1,3 +1,4 @@
1
+ import resolve from "#resolve";
1
2
  import VirtualType from "#VirtualType";
2
3
  import is from "@rcompat/is";
3
4
  export default class DefaultType extends VirtualType {
@@ -17,13 +18,13 @@ export default class DefaultType extends VirtualType {
17
18
  get input() {
18
19
  return undefined;
19
20
  }
20
- parse(x, options = {}) {
21
- let $x = x;
21
+ parse(u, options = {}) {
22
+ let x = resolve(u);
22
23
  // default fallback
23
- if (is.undefined($x)) {
24
- $x = is.function(this.#default) ? this.#default() : this.#default;
24
+ if (is.undefined(x)) {
25
+ x = is.function(this.#default) ? this.#default() : this.#default;
25
26
  }
26
- return this.#schema.parse($x, options);
27
+ return this.#schema.parse(x, options);
27
28
  }
28
29
  toJSON() {
29
30
  return { type: this.name, of: this.#schema.toJSON() };
@@ -11,7 +11,7 @@ export default class EnumType<T extends readonly Literal[]> extends GenericType<
11
11
  get name(): string;
12
12
  optional(): OptionalType<this>;
13
13
  default<D extends T[number]>(value: (() => D) | D): DefaultType<EnumType<T>, D>;
14
- parse(x: unknown, options?: ParseOptions): Infer<this>;
14
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
15
15
  toJSON(): {
16
16
  type: "enum";
17
17
  values: T[number][];
@@ -2,6 +2,8 @@ import DefaultType from "#DefaultType";
2
2
  import E from "#errors";
3
3
  import GenericType from "#GenericType";
4
4
  import OptionalType from "#OptionalType";
5
+ import resolve from "#resolve";
6
+ import is from "@rcompat/is";
5
7
  export default class EnumType extends GenericType {
6
8
  #values;
7
9
  constructor(values) {
@@ -17,8 +19,9 @@ export default class EnumType extends GenericType {
17
19
  default(value) {
18
20
  return new DefaultType(this, value);
19
21
  }
20
- parse(x, options = {}) {
21
- if (typeof x !== "string" || !this.#values.includes(x)) {
22
+ parse(u, options = {}) {
23
+ const x = resolve(u);
24
+ if (!is.string(x) || !this.#values.includes(x)) {
22
25
  throw E.invalid_type(x, this.name, options);
23
26
  }
24
27
  return x;
@@ -7,7 +7,7 @@ import type { UnknownFunction } from "@rcompat/type";
7
7
  export default class FunctionType extends GenericType<UnknownFunction, UnknownFunction, "FunctionType"> implements OptionalTrait {
8
8
  get name(): "function";
9
9
  optional(): OptionalType<this>;
10
- parse(x: unknown, options?: ParseOptions): Infer<this>;
10
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
11
11
  toJSON(): {
12
12
  type: "function";
13
13
  };
@@ -1,6 +1,8 @@
1
+ import E from "#errors";
1
2
  import GenericType from "#GenericType";
2
3
  import OptionalType from "#OptionalType";
3
- import E from "#errors";
4
+ import resolve from "#resolve";
5
+ import is from "@rcompat/is";
4
6
  export default class FunctionType extends GenericType {
5
7
  get name() {
6
8
  return "function";
@@ -8,8 +10,9 @@ export default class FunctionType extends GenericType {
8
10
  optional() {
9
11
  return new OptionalType(this);
10
12
  }
11
- parse(x, options = {}) {
12
- if (typeof x !== "function")
13
+ parse(u, options = {}) {
14
+ const x = resolve(u);
15
+ if (!is.function(x))
13
16
  throw E.invalid_type(x, this.name, options);
14
17
  return x;
15
18
  }
@@ -4,9 +4,9 @@ import type IntDataType from "#IntDataType";
4
4
  import NumericType from "#NumericType";
5
5
  import type ParseOptions from "#ParseOptions";
6
6
  import type Storable from "#Storable";
7
- export default class IntType<T extends IntDataType> extends NumericType<T, number, "IntType"> implements Storable<T> {
7
+ export default class IntType<T extends IntDataType, L extends boolean | undefined = undefined> extends NumericType<T, number, "IntType", L> implements Storable<T> {
8
8
  [CoerceKey]: typeof coerce;
9
9
  get name(): string;
10
- parse(x: unknown, options?: ParseOptions<number>): import("./Infer.js").default<this>;
10
+ parse(u: unknown, options?: ParseOptions<number>): import("./Infer.js").default<this>;
11
11
  }
12
12
  //# sourceMappingURL=IntType.d.ts.map
@@ -7,8 +7,8 @@ export default class IntType extends NumericType {
7
7
  get name() {
8
8
  return "number";
9
9
  }
10
- parse(x, options = {}) {
11
- return super.parse(x, {
10
+ parse(u, options = {}) {
11
+ return super.parse(u, {
12
12
  ...options,
13
13
  validators: [integer],
14
14
  });
@@ -11,7 +11,7 @@ export default class IsType<T> extends Parsed<T> implements OptionalTrait, Defau
11
11
  get name(): "is";
12
12
  optional(): OptionalType<this>;
13
13
  default(value: (() => T) | T): DefaultType<this, T>;
14
- parse(x: unknown, options?: ParseOptions): Infer<this>;
14
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
15
15
  toJSON(): {
16
16
  type: "is";
17
17
  };
@@ -2,6 +2,7 @@ import DefaultType from "#DefaultType";
2
2
  import E from "#errors";
3
3
  import OptionalType from "#OptionalType";
4
4
  import Parsed from "#Parsed";
5
+ import resolve from "#resolve";
5
6
  export default class IsType extends Parsed {
6
7
  #predicate;
7
8
  constructor(predicate) {
@@ -17,7 +18,8 @@ export default class IsType extends Parsed {
17
18
  default(value) {
18
19
  return new DefaultType(this, value);
19
20
  }
20
- parse(x, options = {}) {
21
+ parse(u, options = {}) {
22
+ const x = resolve(u);
21
23
  if (!this.#predicate(x))
22
24
  throw E.invalid_type(x, this.name, options);
23
25
  return x;
@@ -16,7 +16,7 @@ export default class JSONType<S extends JSONInput = undefined> extends Storable<
16
16
  get datatype(): "json";
17
17
  optional(): OptionalType<this>;
18
18
  default(value: (() => Infer<this>) | Infer<this>): DefaultType<this, Infer<this>>;
19
- parse(x: unknown, options?: ParseOptions): Infer<this>;
19
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
20
20
  toJSON(): {
21
21
  type: "json";
22
22
  datatype: "json";
@@ -1,6 +1,7 @@
1
1
  import DefaultType from "#DefaultType";
2
2
  import E from "#errors";
3
3
  import OptionalType from "#OptionalType";
4
+ import resolve from "#resolve";
4
5
  import Storable from "#Storable";
5
6
  import is from "@rcompat/is";
6
7
  export default class JSONType extends Storable {
@@ -21,7 +22,8 @@ export default class JSONType extends Storable {
21
22
  default(value) {
22
23
  return new DefaultType(this, value);
23
24
  }
24
- parse(x, options = {}) {
25
+ parse(u, options = {}) {
26
+ const x = resolve(u);
25
27
  if (this.#inner !== undefined) {
26
28
  // delegate to inner schema for typed validation
27
29
  return this.#inner.parse(x, options);
@@ -1,18 +1,20 @@
1
1
  import DefaultType from "#DefaultType";
2
2
  import GenericType from "#GenericType";
3
3
  import type Infer from "#Infer";
4
+ import Loose from "#Loose";
4
5
  import OptionalType from "#OptionalType";
5
6
  import type ParseOptions from "#ParseOptions";
7
+ import type Mode from "#Mode";
6
8
  type Literal = string | boolean | number;
7
- export default class LiteralType<T extends Literal> extends GenericType<T, T, "LiteralType"> {
9
+ export default class LiteralType<T extends Literal, M extends Mode = undefined> extends GenericType<T, T, "LiteralType"> {
8
10
  #private;
9
- constructor(literal: T);
10
- static new<T extends Literal>(literal: T): LiteralType<T>;
11
+ [Loose]: M;
11
12
  static get Literal(): Literal;
13
+ constructor(literal: T, mode?: M);
12
14
  get name(): string;
13
15
  optional(): OptionalType<this>;
14
16
  default(value: (() => T) | T): DefaultType<this, T>;
15
- parse(x: unknown, options?: ParseOptions): Infer<this>;
17
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
16
18
  toJSON(): {
17
19
  type: "literal";
18
20
  value: T;
@@ -1,19 +1,20 @@
1
1
  import DefaultType from "#DefaultType";
2
2
  import GenericType from "#GenericType";
3
+ import Loose from "#Loose";
3
4
  import OptionalType from "#OptionalType";
4
5
  import E from "#errors";
6
+ import resolve from "#resolve";
5
7
  export default class LiteralType extends GenericType {
6
8
  #literal;
7
- constructor(literal) {
8
- super();
9
- this.#literal = literal;
10
- }
11
- static new(literal) {
12
- return new LiteralType(literal);
13
- }
9
+ [Loose];
14
10
  static get Literal() {
15
11
  return undefined;
16
12
  }
13
+ constructor(literal, mode) {
14
+ super();
15
+ this.#literal = literal;
16
+ this[Loose] = mode;
17
+ }
17
18
  get name() {
18
19
  return JSON.stringify(this.#literal);
19
20
  }
@@ -23,7 +24,8 @@ export default class LiteralType extends GenericType {
23
24
  default(value) {
24
25
  return new DefaultType(this, value);
25
26
  }
26
- parse(x, options = {}) {
27
+ parse(u, options = {}) {
28
+ const x = resolve(u);
27
29
  if (x !== this.#literal)
28
30
  throw E.invalid_type(x, this.name, options);
29
31
  return x;
@@ -0,0 +1,4 @@
1
+ declare const Loose: unique symbol;
2
+ type Loose = typeof Loose;
3
+ export { Loose as default };
4
+ //# sourceMappingURL=Loose.d.ts.map
@@ -0,0 +1,3 @@
1
+ const Loose = Symbol("Loose");
2
+ export { Loose as default };
3
+ //# sourceMappingURL=Loose.js.map
@@ -0,0 +1,3 @@
1
+ type Mode = boolean | undefined;
2
+ export type { Mode as default };
3
+ //# sourceMappingURL=Mode.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Mode.js.map
@@ -7,13 +7,13 @@ import type Parsed from "#Parsed";
7
7
  import type Schema from "#Schema";
8
8
  import type TupleType from "#TupleType";
9
9
  import type UndefinedType from "#UndefinedType";
10
- import type { AbstractNewable } from "@rcompat/type";
10
+ import type { AbstractNewable, Dict } from "@rcompat/type";
11
11
  type NormalizeSchemaArray<T extends Schema[]> = {
12
12
  [K in keyof T]: NormalizeSchema<T[K]>;
13
13
  };
14
- export type NormalizeSchemaObject<T extends Record<string, unknown>> = {
14
+ export type NormalizeSchemaObject<T extends Dict> = {
15
15
  -readonly [K in keyof T]: NormalizeSchema<T[K]>;
16
16
  };
17
- type NormalizeSchema<S> = S extends Parsed<unknown> ? S : S extends null ? NullType : S extends undefined ? UndefinedType : S extends string | number | boolean ? LiteralType<S> : S extends AbstractNewable ? ConstructorType<S> : S extends [Schema] ? ArrayType<NormalizeSchema<S[0]>> : S extends Schema[] ? TupleType<NormalizeSchemaArray<S>> : S extends Record<string, unknown> ? ObjectType<NormalizeSchemaObject<S>> : never;
17
+ type NormalizeSchema<S> = S extends Parsed<unknown> ? S : S extends null ? NullType : S extends undefined ? UndefinedType : S extends string | number | boolean ? LiteralType<S> : S extends AbstractNewable ? ConstructorType<S> : S extends [Schema] ? ArrayType<NormalizeSchema<S[0]>> : S extends Schema[] ? TupleType<NormalizeSchemaArray<S>> : S extends Dict ? ObjectType<NormalizeSchemaObject<S>> : never;
18
18
  export type { NormalizeSchema as default };
19
19
  //# sourceMappingURL=NormalizeSchema.d.ts.map
@@ -3,7 +3,7 @@ import type ParseOptions from "#ParseOptions";
3
3
  import PrimitiveType from "#PrimitiveType";
4
4
  export default class NullType extends PrimitiveType<null, "NullType"> {
5
5
  get name(): "null";
6
- parse(x: unknown, options?: ParseOptions): Infer<this>;
6
+ parse(u: unknown, options?: ParseOptions): Infer<this>;
7
7
  toJSON(): {
8
8
  type: "null";
9
9
  };
@@ -1,11 +1,14 @@
1
1
  import PrimitiveType from "#PrimitiveType";
2
2
  import E from "#errors";
3
+ import resolve from "#resolve";
4
+ import is from "@rcompat/is";
3
5
  export default class NullType extends PrimitiveType {
4
6
  get name() {
5
7
  return "null";
6
8
  }
7
- parse(x, options = {}) {
8
- if (x !== null)
9
+ parse(u, options = {}) {
10
+ const x = resolve(u);
11
+ if (!is.null(x))
9
12
  throw E.invalid_type(x, this.name, options);
10
13
  return x;
11
14
  }
@@ -2,7 +2,7 @@ import CoerceKey from "#CoerceKey";
2
2
  import type FloatDataType from "#FloatDataType";
3
3
  import NumericType from "#NumericType";
4
4
  import coerce from "#coerce/float";
5
- export default class NumberType<T extends FloatDataType = "f64"> extends NumericType<T, number, "NumberType"> {
5
+ export default class NumberType<T extends FloatDataType = "f64", L extends boolean | undefined = undefined> extends NumericType<T, number, "NumberType", L> {
6
6
  [CoerceKey]: typeof coerce;
7
7
  get name(): "number";
8
8
  }
@@ -1,17 +1,21 @@
1
1
  import type DataKey from "#DataKey";
2
+ import Loose from "#Loose";
3
+ import type Mode from "#Mode";
2
4
  import type ParseOptions from "#ParseOptions";
3
5
  import PrimitiveType from "#PrimitiveType";
4
6
  import type Storable from "#Storable";
5
7
  import type Validator from "#Validator";
8
+ import type { Dict } from "@rcompat/type";
6
9
  type Next<T> = {
7
10
  options?: ParseOptions;
8
11
  validators?: Validator<T>[];
9
12
  };
10
- export default abstract class NumericType<Key extends DataKey, T extends bigint | number, Name extends string> extends PrimitiveType<T, Name> implements Storable<Key> {
13
+ export default abstract class NumericType<Key extends DataKey, T extends bigint | number, Name extends string, M extends Mode = undefined> extends PrimitiveType<T, Name> implements Storable<Key> {
11
14
  #private;
12
- constructor(datatype: Key, validators?: Validator<T>[], options?: ParseOptions);
15
+ [Loose]: M;
16
+ constructor(datatype: Key, mode?: M, validators?: Validator<T>[], options?: ParseOptions);
13
17
  derive(next: Next<T>): this;
14
- values(anyof: Record<string, T>): this;
18
+ values(anyof: Dict<T>): this;
15
19
  range(from: T, to: T): this;
16
20
  min(from: T): this;
17
21
  max(to: T): this;
@@ -1,3 +1,4 @@
1
+ import Loose from "#Loose";
1
2
  import PrimitiveType from "#PrimitiveType";
2
3
  import max from "#validator/max";
3
4
  import min from "#validator/min";
@@ -5,13 +6,15 @@ import range from "#validator/range";
5
6
  import values from "#validator/values";
6
7
  export default class NumericType extends PrimitiveType {
7
8
  #datatype;
8
- constructor(datatype, validators = [], options = {}) {
9
+ [Loose];
10
+ constructor(datatype, mode = undefined, validators = [], options = {}) {
9
11
  super(validators, options);
10
12
  this.#datatype = datatype;
13
+ this[Loose] = mode;
11
14
  }
12
15
  derive(next) {
13
16
  const Constructor = this.constructor;
14
- return new Constructor(this.#datatype, [...this.validators, ...next.validators ?? []], { ...this.options, ...next.options ?? {} });
17
+ return new Constructor(this.#datatype, this[Loose], [...this.validators, ...next.validators ?? []], { ...this.options, ...next.options ?? {} });
15
18
  }
16
19
  values(anyof) {
17
20
  return this.derive({ validators: [values(anyof)] });