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.
- package/lib/private/ArrayType.d.ts +1 -0
- package/lib/private/ArrayType.js +23 -27
- package/lib/private/BuiltinType.d.ts +0 -1
- package/lib/private/BuiltinType.js +2 -5
- package/lib/private/ConstructorType.d.ts +4 -1
- package/lib/private/ConstructorType.js +6 -2
- package/lib/private/DataType.d.ts +5 -0
- package/lib/private/DefaultType.js +3 -6
- package/lib/private/EnumType.d.ts +21 -0
- package/lib/private/EnumType.js +30 -0
- package/lib/private/FunctionType.d.ts +15 -0
- package/lib/private/FunctionType.js +20 -0
- package/lib/private/IsType.d.ts +19 -0
- package/lib/private/IsType.js +29 -0
- package/lib/private/Issue.d.ts +2 -0
- package/lib/private/IssueType.d.ts +3 -0
- package/lib/private/IssueType.js +2 -0
- package/lib/private/JSONType.d.ts +26 -0
- package/lib/private/JSONType.js +41 -0
- package/lib/private/LiteralType.d.ts +4 -0
- package/lib/private/LiteralType.js +10 -2
- package/lib/private/NormalizeSchema.d.ts +1 -1
- package/lib/private/NullType.js +2 -2
- package/lib/private/NumberType.d.ts +1 -1
- package/lib/private/NumberType.js +1 -1
- package/lib/private/ObjectType.d.ts +13 -4
- package/lib/private/ObjectType.js +18 -5
- package/lib/private/OmitType.js +4 -3
- package/lib/private/ParseError.d.ts +5 -1
- package/lib/private/ParseError.js +23 -13
- package/lib/private/Parsed.d.ts +1 -0
- package/lib/private/Parsed.js +3 -0
- package/lib/private/PartialType.js +5 -4
- package/lib/private/PrimitiveType.d.ts +0 -1
- package/lib/private/PrimitiveType.js +6 -14
- package/lib/private/RecordType.js +8 -27
- package/lib/private/Serialized.d.ts +18 -1
- package/lib/private/Storable.d.ts +2 -2
- package/lib/private/StoreType.js +2 -2
- package/lib/private/StringType.d.ts +0 -1
- package/lib/private/StringType.js +5 -11
- package/lib/private/TupleType.js +10 -8
- package/lib/private/UUIDType.d.ts +17 -0
- package/lib/private/UUIDType.js +33 -0
- package/lib/private/UUIDV4Type.d.ts +13 -0
- package/lib/private/UUIDV4Type.js +23 -0
- package/lib/private/UUIDV7Type.d.ts +13 -0
- package/lib/private/UUIDV7Type.js +23 -0
- package/lib/private/UnionType.js +5 -5
- package/lib/private/coerce/bigint.d.ts +1 -1
- package/lib/private/coerce/bigint.js +1 -1
- package/lib/private/coerce/float.d.ts +2 -0
- package/lib/private/coerce/float.js +8 -0
- package/lib/private/coerce/int.d.ts +1 -1
- package/lib/private/coerce/int.js +6 -3
- package/lib/private/enum.d.ts +3 -0
- package/lib/private/enum.js +5 -0
- package/lib/private/errors.d.ts +21 -0
- package/lib/private/errors.js +53 -0
- package/lib/private/function.d.ts +7 -0
- package/lib/private/function.js +6 -0
- package/lib/private/index.d.ts +79 -32
- package/lib/private/index.js +14 -0
- package/lib/private/is.d.ts +4 -0
- package/lib/private/is.js +3 -0
- package/lib/private/json/JSONIssue.d.ts +2 -0
- package/lib/private/json.d.ts +5 -0
- package/lib/private/json.js +5 -0
- package/lib/private/object.d.ts +1 -1
- package/lib/private/object.js +2 -2
- package/lib/private/schema-errors.d.ts +16 -0
- package/lib/private/schema-errors.js +17 -0
- package/lib/private/spec/bigint.d.ts +1 -1
- package/lib/private/spec/bigint.js +30 -41
- package/lib/private/spec/biguint.d.ts +1 -1
- package/lib/private/spec/biguint.js +29 -45
- package/lib/private/spec/int.d.ts +1 -1
- package/lib/private/spec/int.js +27 -38
- package/lib/private/spec/uint.d.ts +1 -1
- package/lib/private/spec/uint.js +31 -43
- package/lib/private/test.d.ts +34 -0
- package/lib/private/test.js +107 -0
- package/lib/private/uuid.d.ts +4 -0
- package/lib/private/uuid.js +3 -0
- package/lib/private/validator/ends-with.d.ts +1 -2
- package/lib/private/validator/ends-with.js +8 -10
- package/lib/private/validator/f32.js +3 -8
- package/lib/private/validator/integer.d.ts +1 -2
- package/lib/private/validator/integer.js +6 -10
- package/lib/private/validator/isotime.js +8 -10
- package/lib/private/validator/length.js +11 -19
- package/lib/private/validator/max.js +14 -15
- package/lib/private/validator/min.js +14 -15
- package/lib/private/validator/port.d.ts +1 -2
- package/lib/private/validator/port.js +6 -9
- package/lib/private/validator/range.js +5 -11
- package/lib/private/validator/regex.d.ts +1 -1
- package/lib/private/validator/regex.js +5 -9
- package/lib/private/validator/starts-with.d.ts +1 -2
- package/lib/private/validator/starts-with.js +8 -10
- package/lib/private/validator/unique-by.d.ts +1 -1
- package/lib/private/validator/unique-by.js +8 -14
- package/lib/private/validator/unique-with.d.ts +1 -1
- package/lib/private/validator/unique-with.js +3 -7
- package/lib/private/validator/unique.js +2 -6
- package/lib/private/validator/values.js +2 -6
- package/lib/public/StoreType.d.ts +1 -1
- package/lib/public/index.d.ts +1 -28
- package/lib/public/index.js +0 -16
- package/package.json +10 -9
- package/lib/private/SchemaError.d.ts +0 -4
- package/lib/private/SchemaError.js +0 -8
- package/lib/private/error/fail.d.ts +0 -3
- package/lib/private/error/fail.js +0 -5
- package/lib/private/error/schemafail.d.ts +0 -3
- package/lib/private/error/schemafail.js +0 -5
- package/lib/private/error.d.ts +0 -4
- package/lib/private/error.js +0 -11
- package/lib/private/expect.d.ts +0 -22
- package/lib/private/expect.js +0 -32
- package/lib/private/expected.d.ts +0 -3
- package/lib/private/expected.js +0 -11
- package/lib/private/fail.d.ts +0 -4
- package/lib/private/fail.js +0 -6
- package/lib/private/test/messages-of.d.ts +0 -3
- package/lib/private/test/messages-of.js +0 -4
- package/lib/private/test/paths-of.d.ts +0 -3
- package/lib/private/test/paths-of.js +0 -4
- package/lib/private/test/throws-issues.d.ts +0 -4
- package/lib/private/test/throws-issues.js +0 -13
- package/lib/private/validator/uuid.d.ts +0 -3
- package/lib/private/validator/uuid.js +0 -4
|
@@ -25,6 +25,7 @@ export default class ArrayType<T extends Parsed<unknown>> extends GenericType<T,
|
|
|
25
25
|
* @returns ArrayType<T>
|
|
26
26
|
*/
|
|
27
27
|
unique(this: Infer<T> extends Primitive ? ArrayType<T> : never): ArrayType<T>;
|
|
28
|
+
uniqueBy<K>(select: (value: Infer<T>) => K): this;
|
|
28
29
|
min(limit: number): this;
|
|
29
30
|
max(limit: number): this;
|
|
30
31
|
length(from: number, to: number): this;
|
package/lib/private/ArrayType.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import DefaultType from "#DefaultType";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import fail from "#fail";
|
|
2
|
+
import EnumType from "#EnumType";
|
|
3
|
+
import E from "#errors";
|
|
5
4
|
import GenericType from "#GenericType";
|
|
6
5
|
import OptionalType from "#OptionalType";
|
|
7
6
|
import ParsedKey from "#ParsedKey";
|
|
@@ -10,12 +9,14 @@ import join from "#path/join";
|
|
|
10
9
|
import next from "#path/next";
|
|
11
10
|
import rebase from "#path/rebase";
|
|
12
11
|
import PrimitiveType from "#PrimitiveType";
|
|
12
|
+
import S from "#schema-errors";
|
|
13
13
|
import length from "#validator/length";
|
|
14
14
|
import max from "#validator/max";
|
|
15
15
|
import min from "#validator/min";
|
|
16
16
|
import unique from "#validator/unique";
|
|
17
|
+
import uniqueBy from "#validator/unique-by";
|
|
17
18
|
function isPrimitive(x) {
|
|
18
|
-
return x instanceof PrimitiveType;
|
|
19
|
+
return x instanceof PrimitiveType || x instanceof EnumType;
|
|
19
20
|
}
|
|
20
21
|
export default class ArrayType extends GenericType {
|
|
21
22
|
#item;
|
|
@@ -46,56 +47,51 @@ export default class ArrayType extends GenericType {
|
|
|
46
47
|
*/
|
|
47
48
|
unique() {
|
|
48
49
|
if (!isPrimitive(this.#item)) {
|
|
49
|
-
throw
|
|
50
|
+
throw S.unique_subtype_not_primitive(this.#item.name);
|
|
50
51
|
}
|
|
51
52
|
return this.derive({ validators: [unique] });
|
|
52
53
|
}
|
|
54
|
+
uniqueBy(select) {
|
|
55
|
+
return this.derive({ validators: [uniqueBy(select)] });
|
|
56
|
+
}
|
|
53
57
|
min(limit) {
|
|
54
|
-
if (limit < 0)
|
|
55
|
-
throw
|
|
56
|
-
}
|
|
58
|
+
if (limit < 0)
|
|
59
|
+
throw S.min_negative(limit);
|
|
57
60
|
return this.derive({ validators: [min(limit)] });
|
|
58
61
|
}
|
|
59
62
|
max(limit) {
|
|
60
|
-
if (limit < 0)
|
|
61
|
-
throw
|
|
62
|
-
}
|
|
63
|
+
if (limit < 0)
|
|
64
|
+
throw S.max_negative(limit);
|
|
63
65
|
return this.derive({ validators: [max(limit)] });
|
|
64
66
|
}
|
|
65
67
|
length(from, to) {
|
|
66
68
|
return this.derive({ validators: [length(from, to)] });
|
|
67
69
|
}
|
|
68
70
|
parse(x, options = {}) {
|
|
71
|
+
const path = options[ParsedKey] ?? "";
|
|
69
72
|
if (!Array.isArray(x))
|
|
70
|
-
throw
|
|
71
|
-
const base = options[ParsedKey] ?? "";
|
|
73
|
+
throw E.invalid_type(x, "array", path);
|
|
72
74
|
const item = this.#item;
|
|
73
75
|
const len = x.length;
|
|
76
|
+
const out = new Array(len);
|
|
74
77
|
for (let i = 0; i < len; i++) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
...error(item.name, undefined, options)[0],
|
|
79
|
-
path: join(base, i),
|
|
80
|
-
}]);
|
|
81
|
-
}
|
|
82
|
-
item.parse(x[i], next(i, options));
|
|
78
|
+
if (!(i in x))
|
|
79
|
+
throw E.invalid_type(undefined, item.name, join(path, i));
|
|
80
|
+
out[i] = item.parse(x[i], next(i, options));
|
|
83
81
|
}
|
|
84
82
|
const validators = this.#validators;
|
|
85
83
|
for (let i = 0; i < validators.length; i++) {
|
|
86
84
|
try {
|
|
87
|
-
validators[i](
|
|
85
|
+
validators[i](out);
|
|
88
86
|
}
|
|
89
87
|
catch (e) {
|
|
90
|
-
if (e
|
|
91
|
-
|
|
92
|
-
.map(issue => ({ ...issue, path: rebase(base, issue.path) }));
|
|
93
|
-
throw new ParseError(rebased);
|
|
88
|
+
if (ParseError.is(e)) {
|
|
89
|
+
throw new ParseError(e.issues.map(issue => ({ ...issue, path: rebase(path, issue.path) })));
|
|
94
90
|
}
|
|
95
91
|
throw e;
|
|
96
92
|
}
|
|
97
93
|
}
|
|
98
|
-
return
|
|
94
|
+
return out;
|
|
99
95
|
}
|
|
100
96
|
toJSON() {
|
|
101
97
|
return { type: this.name, of: this.#item.toJSON() };
|
|
@@ -6,7 +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
|
-
get coerce(): this;
|
|
10
9
|
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
11
10
|
}
|
|
12
11
|
//# sourceMappingURL=BuiltinType.d.ts.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import CoerceKey from "#CoerceKey";
|
|
2
|
-
import
|
|
2
|
+
import E from "#errors";
|
|
3
3
|
import Type from "#Type";
|
|
4
4
|
export default class BuiltinType extends Type {
|
|
5
5
|
#options;
|
|
@@ -11,14 +11,11 @@ export default class BuiltinType extends Type {
|
|
|
11
11
|
const Constructor = this.constructor;
|
|
12
12
|
return new Constructor({ ...this.#options, ...next });
|
|
13
13
|
}
|
|
14
|
-
get coerce() {
|
|
15
|
-
return this.#derive({ coerce: true });
|
|
16
|
-
}
|
|
17
14
|
parse(x, options = {}) {
|
|
18
15
|
const $options = { ...this.#options, ...options };
|
|
19
16
|
const $x = $options.coerce === true ? this[CoerceKey](x) : x;
|
|
20
17
|
if (!($x instanceof this.Type))
|
|
21
|
-
throw
|
|
18
|
+
throw E.invalid_type($x, this.name, $options);
|
|
22
19
|
return $x;
|
|
23
20
|
}
|
|
24
21
|
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import DefaultType from "#DefaultType";
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
3
|
import type Infer from "#Infer";
|
|
4
|
+
import OptionalType from "#OptionalType";
|
|
4
5
|
import type ParseOptions from "#ParseOptions";
|
|
5
6
|
import type DefaultTrait from "#trait/Default";
|
|
7
|
+
import type OptionalTrait from "#trait/Optional";
|
|
6
8
|
import type { AbstractNewable } from "@rcompat/type";
|
|
7
|
-
export default class ConstructorType<C extends AbstractNewable> extends GenericType<C, InstanceType<C>, "InstanceType"> implements DefaultTrait<InstanceType<C>> {
|
|
9
|
+
export default class ConstructorType<C extends AbstractNewable> extends GenericType<C, InstanceType<C>, "InstanceType"> implements OptionalTrait, DefaultTrait<InstanceType<C>> {
|
|
8
10
|
#private;
|
|
9
11
|
constructor(t: C);
|
|
10
12
|
get name(): string;
|
|
13
|
+
optional(): OptionalType<this>;
|
|
11
14
|
default(value: (() => InstanceType<C>) | InstanceType<C>): DefaultType<this, InstanceType<C>>;
|
|
12
15
|
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
13
16
|
toJSON(): {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import DefaultType from "#DefaultType";
|
|
2
|
-
import
|
|
2
|
+
import E from "#errors";
|
|
3
3
|
import GenericType from "#GenericType";
|
|
4
|
+
import OptionalType from "#OptionalType";
|
|
4
5
|
export default class ConstructorType extends GenericType {
|
|
5
6
|
#type;
|
|
6
7
|
constructor(t) {
|
|
@@ -10,12 +11,15 @@ export default class ConstructorType extends GenericType {
|
|
|
10
11
|
get name() {
|
|
11
12
|
return "constructor";
|
|
12
13
|
}
|
|
14
|
+
optional() {
|
|
15
|
+
return new OptionalType(this);
|
|
16
|
+
}
|
|
13
17
|
default(value) {
|
|
14
18
|
return new DefaultType(this, value);
|
|
15
19
|
}
|
|
16
20
|
parse(x, options = {}) {
|
|
17
21
|
if (!(x instanceof this.#type))
|
|
18
|
-
throw
|
|
22
|
+
throw E.invalid_type(x, this.name, options);
|
|
19
23
|
return x;
|
|
20
24
|
}
|
|
21
25
|
toJSON() {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { JSONValue } from "@rcompat/type";
|
|
1
2
|
type DataType = {
|
|
2
3
|
blob: Blob;
|
|
3
4
|
boolean: boolean;
|
|
@@ -9,6 +10,7 @@ type DataType = {
|
|
|
9
10
|
i32: number;
|
|
10
11
|
i64: bigint;
|
|
11
12
|
i8: number;
|
|
13
|
+
json: JSONValue;
|
|
12
14
|
string: string;
|
|
13
15
|
time: string;
|
|
14
16
|
u128: bigint;
|
|
@@ -17,6 +19,9 @@ type DataType = {
|
|
|
17
19
|
u64: bigint;
|
|
18
20
|
u8: number;
|
|
19
21
|
url: URL;
|
|
22
|
+
uuid: string;
|
|
23
|
+
uuid_v4: string;
|
|
24
|
+
uuid_v7: string;
|
|
20
25
|
};
|
|
21
26
|
export type { DataType as default };
|
|
22
27
|
//# sourceMappingURL=DataType.d.ts.map
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import VirtualType from "#VirtualType";
|
|
2
|
-
|
|
3
|
-
return typeof x === "function";
|
|
4
|
-
}
|
|
5
|
-
;
|
|
2
|
+
import is from "@rcompat/is";
|
|
6
3
|
export default class DefaultType extends VirtualType {
|
|
7
4
|
#schema;
|
|
8
5
|
#default;
|
|
@@ -23,8 +20,8 @@ export default class DefaultType extends VirtualType {
|
|
|
23
20
|
parse(x, options = {}) {
|
|
24
21
|
let $x = x;
|
|
25
22
|
// default fallback
|
|
26
|
-
if ($x
|
|
27
|
-
$x =
|
|
23
|
+
if (is.undefined($x)) {
|
|
24
|
+
$x = is.function(this.#default) ? this.#default() : this.#default;
|
|
28
25
|
}
|
|
29
26
|
return this.#schema.parse($x, options);
|
|
30
27
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import GenericType from "#GenericType";
|
|
3
|
+
import type Infer from "#Infer";
|
|
4
|
+
import OptionalType from "#OptionalType";
|
|
5
|
+
import type ParseOptions from "#ParseOptions";
|
|
6
|
+
import type OptionalTrait from "#trait/Optional";
|
|
7
|
+
type Literal = string;
|
|
8
|
+
export default class EnumType<T extends readonly Literal[]> extends GenericType<T, T[number], "EnumType"> implements OptionalTrait {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(values: T);
|
|
11
|
+
get name(): string;
|
|
12
|
+
optional(): OptionalType<this>;
|
|
13
|
+
default<D extends T[number]>(value: (() => D) | D): DefaultType<EnumType<T>, D>;
|
|
14
|
+
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
15
|
+
toJSON(): {
|
|
16
|
+
type: "enum";
|
|
17
|
+
values: T[number][];
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=EnumType.d.ts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import E from "#errors";
|
|
3
|
+
import GenericType from "#GenericType";
|
|
4
|
+
import OptionalType from "#OptionalType";
|
|
5
|
+
export default class EnumType extends GenericType {
|
|
6
|
+
#values;
|
|
7
|
+
constructor(values) {
|
|
8
|
+
super();
|
|
9
|
+
this.#values = values;
|
|
10
|
+
}
|
|
11
|
+
get name() {
|
|
12
|
+
return this.#values.map(v => `"${v}"`).join(" | ");
|
|
13
|
+
}
|
|
14
|
+
optional() {
|
|
15
|
+
return new OptionalType(this);
|
|
16
|
+
}
|
|
17
|
+
default(value) {
|
|
18
|
+
return new DefaultType(this, value);
|
|
19
|
+
}
|
|
20
|
+
parse(x, options = {}) {
|
|
21
|
+
if (typeof x !== "string" || !this.#values.includes(x)) {
|
|
22
|
+
throw E.invalid_type(x, this.name, options);
|
|
23
|
+
}
|
|
24
|
+
return x;
|
|
25
|
+
}
|
|
26
|
+
toJSON() {
|
|
27
|
+
return { type: "enum", values: [...this.#values] };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=EnumType.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
import type ParseOptions from "#ParseOptions";
|
|
5
|
+
import type OptionalTrait from "#trait/Optional";
|
|
6
|
+
import type { UnknownFunction } from "@rcompat/type";
|
|
7
|
+
export default class FunctionType extends GenericType<UnknownFunction, UnknownFunction, "FunctionType"> implements OptionalTrait {
|
|
8
|
+
get name(): "function";
|
|
9
|
+
optional(): OptionalType<this>;
|
|
10
|
+
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
11
|
+
toJSON(): {
|
|
12
|
+
type: "function";
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=FunctionType.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import OptionalType from "#OptionalType";
|
|
3
|
+
import E from "#errors";
|
|
4
|
+
export default class FunctionType extends GenericType {
|
|
5
|
+
get name() {
|
|
6
|
+
return "function";
|
|
7
|
+
}
|
|
8
|
+
optional() {
|
|
9
|
+
return new OptionalType(this);
|
|
10
|
+
}
|
|
11
|
+
parse(x, options = {}) {
|
|
12
|
+
if (typeof x !== "function")
|
|
13
|
+
throw E.invalid_type(x, this.name, options);
|
|
14
|
+
return x;
|
|
15
|
+
}
|
|
16
|
+
toJSON() {
|
|
17
|
+
return { type: this.name };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=FunctionType.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
import Parsed from "#Parsed";
|
|
5
|
+
import type ParseOptions from "#ParseOptions";
|
|
6
|
+
import type DefaultTrait from "#trait/Default";
|
|
7
|
+
import type OptionalTrait from "#trait/Optional";
|
|
8
|
+
export default class IsType<T> extends Parsed<T> implements OptionalTrait, DefaultTrait<T> {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(predicate: (x: unknown) => x is T);
|
|
11
|
+
get name(): "is";
|
|
12
|
+
optional(): OptionalType<this>;
|
|
13
|
+
default(value: (() => T) | T): DefaultType<this, T>;
|
|
14
|
+
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
15
|
+
toJSON(): {
|
|
16
|
+
type: "is";
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=IsType.d.ts.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import E from "#errors";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
import Parsed from "#Parsed";
|
|
5
|
+
export default class IsType extends Parsed {
|
|
6
|
+
#predicate;
|
|
7
|
+
constructor(predicate) {
|
|
8
|
+
super();
|
|
9
|
+
this.#predicate = predicate;
|
|
10
|
+
}
|
|
11
|
+
get name() {
|
|
12
|
+
return "is";
|
|
13
|
+
}
|
|
14
|
+
optional() {
|
|
15
|
+
return new OptionalType(this);
|
|
16
|
+
}
|
|
17
|
+
default(value) {
|
|
18
|
+
return new DefaultType(this, value);
|
|
19
|
+
}
|
|
20
|
+
parse(x, options = {}) {
|
|
21
|
+
if (!this.#predicate(x))
|
|
22
|
+
throw E.invalid_type(x, this.name, options);
|
|
23
|
+
return x;
|
|
24
|
+
}
|
|
25
|
+
toJSON() {
|
|
26
|
+
return { type: this.name };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=IsType.js.map
|
package/lib/private/Issue.d.ts
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
import type Parsed from "#Parsed";
|
|
5
|
+
import type ParseOptions from "#ParseOptions";
|
|
6
|
+
import Storable from "#Storable";
|
|
7
|
+
import type { JSONValue } from "@rcompat/type";
|
|
8
|
+
export type ParsedJSON = Parsed<JSONValue>;
|
|
9
|
+
type JSONInput = ParsedJSON | undefined;
|
|
10
|
+
type JSONInfer<S extends JSONInput> = S extends ParsedJSON ? Infer<S> : JSONValue;
|
|
11
|
+
export default class JSONType<S extends JSONInput = undefined> extends Storable<"json", JSONInfer<S>> {
|
|
12
|
+
#private;
|
|
13
|
+
constructor(inner?: S);
|
|
14
|
+
get name(): "json";
|
|
15
|
+
get datatype(): "json";
|
|
16
|
+
optional(): OptionalType<this>;
|
|
17
|
+
default(value: (() => Infer<this>) | Infer<this>): DefaultType<this, Infer<this>>;
|
|
18
|
+
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
19
|
+
toJSON(): {
|
|
20
|
+
of?: import("./Serialized.js").default | undefined;
|
|
21
|
+
type: "json";
|
|
22
|
+
datatype: "json";
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=JSONType.d.ts.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import E from "#errors";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
import Storable from "#Storable";
|
|
5
|
+
import is from "@rcompat/is";
|
|
6
|
+
export default class JSONType extends Storable {
|
|
7
|
+
#inner;
|
|
8
|
+
constructor(inner) {
|
|
9
|
+
super();
|
|
10
|
+
this.#inner = inner;
|
|
11
|
+
}
|
|
12
|
+
get name() {
|
|
13
|
+
return "json";
|
|
14
|
+
}
|
|
15
|
+
get datatype() {
|
|
16
|
+
return "json";
|
|
17
|
+
}
|
|
18
|
+
optional() {
|
|
19
|
+
return new OptionalType(this);
|
|
20
|
+
}
|
|
21
|
+
default(value) {
|
|
22
|
+
return new DefaultType(this, value);
|
|
23
|
+
}
|
|
24
|
+
parse(x, options = {}) {
|
|
25
|
+
if (this.#inner !== undefined) {
|
|
26
|
+
// delegate to inner schema for typed validation
|
|
27
|
+
return this.#inner.parse(x, options);
|
|
28
|
+
}
|
|
29
|
+
if (!is.json(x))
|
|
30
|
+
throw E.invalid_type(x, "json", options);
|
|
31
|
+
return JSON.parse(JSON.stringify(x));
|
|
32
|
+
}
|
|
33
|
+
toJSON() {
|
|
34
|
+
return {
|
|
35
|
+
type: "json",
|
|
36
|
+
datatype: "json",
|
|
37
|
+
...(this.#inner !== undefined && { of: this.#inner.toJSON() }),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=JSONType.js.map
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
1
2
|
import GenericType from "#GenericType";
|
|
2
3
|
import type Infer from "#Infer";
|
|
4
|
+
import OptionalType from "#OptionalType";
|
|
3
5
|
import type ParseOptions from "#ParseOptions";
|
|
4
6
|
type Literal = string | boolean | number;
|
|
5
7
|
export default class LiteralType<T extends Literal> extends GenericType<T, T, "LiteralType"> {
|
|
@@ -8,6 +10,8 @@ export default class LiteralType<T extends Literal> extends GenericType<T, T, "L
|
|
|
8
10
|
static new<T extends Literal>(literal: T): LiteralType<T>;
|
|
9
11
|
static get Literal(): Literal;
|
|
10
12
|
get name(): string;
|
|
13
|
+
optional(): OptionalType<this>;
|
|
14
|
+
default(value: (() => T) | T): DefaultType<this, T>;
|
|
11
15
|
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
12
16
|
toJSON(): {
|
|
13
17
|
type: "literal";
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
import E from "#errors";
|
|
3
5
|
export default class LiteralType extends GenericType {
|
|
4
6
|
#literal;
|
|
5
7
|
constructor(literal) {
|
|
@@ -15,9 +17,15 @@ export default class LiteralType extends GenericType {
|
|
|
15
17
|
get name() {
|
|
16
18
|
return JSON.stringify(this.#literal);
|
|
17
19
|
}
|
|
20
|
+
optional() {
|
|
21
|
+
return new OptionalType(this);
|
|
22
|
+
}
|
|
23
|
+
default(value) {
|
|
24
|
+
return new DefaultType(this, value);
|
|
25
|
+
}
|
|
18
26
|
parse(x, options = {}) {
|
|
19
27
|
if (x !== this.#literal)
|
|
20
|
-
throw
|
|
28
|
+
throw E.invalid_type(x, this.name, options);
|
|
21
29
|
return x;
|
|
22
30
|
}
|
|
23
31
|
toJSON() {
|
|
@@ -11,7 +11,7 @@ import type { AbstractNewable } from "@rcompat/type";
|
|
|
11
11
|
type NormalizeSchemaArray<T extends Schema[]> = {
|
|
12
12
|
[K in keyof T]: NormalizeSchema<T[K]>;
|
|
13
13
|
};
|
|
14
|
-
type NormalizeSchemaObject<T extends Record<string, unknown>> = {
|
|
14
|
+
export type NormalizeSchemaObject<T extends Record<string, unknown>> = {
|
|
15
15
|
-readonly [K in keyof T]: NormalizeSchema<T[K]>;
|
|
16
16
|
};
|
|
17
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;
|
package/lib/private/NullType.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import fail from "#fail";
|
|
2
1
|
import PrimitiveType from "#PrimitiveType";
|
|
2
|
+
import E from "#errors";
|
|
3
3
|
export default class NullType extends PrimitiveType {
|
|
4
4
|
get name() {
|
|
5
5
|
return "null";
|
|
6
6
|
}
|
|
7
7
|
parse(x, options = {}) {
|
|
8
8
|
if (x !== null)
|
|
9
|
-
throw
|
|
9
|
+
throw E.invalid_type(x, this.name, options);
|
|
10
10
|
return x;
|
|
11
11
|
}
|
|
12
12
|
toJSON() {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import CoerceKey from "#CoerceKey";
|
|
2
2
|
import type FloatDataType from "#FloatDataType";
|
|
3
3
|
import NumericType from "#NumericType";
|
|
4
|
-
import coerce from "#coerce/
|
|
4
|
+
import coerce from "#coerce/float";
|
|
5
5
|
export default class NumberType<T extends FloatDataType = "f64"> extends NumericType<T, number, "NumberType"> {
|
|
6
6
|
[CoerceKey]: typeof coerce;
|
|
7
7
|
get name(): "number";
|
|
@@ -1,23 +1,32 @@
|
|
|
1
1
|
import GenericType from "#GenericType";
|
|
2
2
|
import type Infer from "#Infer";
|
|
3
3
|
import type InferInputSchema from "#InferInputSchema";
|
|
4
|
+
import OptionalType from "#OptionalType";
|
|
4
5
|
import type Parsed from "#Parsed";
|
|
5
6
|
import type ParseOptions from "#ParseOptions";
|
|
6
7
|
import type Serialized from "#Serialized";
|
|
7
|
-
import type
|
|
8
|
-
|
|
8
|
+
import type OptionalTrait from "#trait/Optional";
|
|
9
|
+
import type { Dict, Unpack } from "@rcompat/type";
|
|
10
|
+
type ObjectInfer<P extends Dict<Parsed<unknown>>> = {
|
|
9
11
|
[K in keyof P]: P[K]["infer"];
|
|
10
|
-
}
|
|
12
|
+
};
|
|
13
|
+
export default class ObjectType<P extends Dict<Parsed<unknown>>, I = ObjectInfer<P>> extends GenericType<P, I, "ObjectType"> implements OptionalTrait {
|
|
11
14
|
#private;
|
|
15
|
+
readonly Complement: ObjectType<Record<Exclude<string, keyof P>, Parsed<unknown>>>;
|
|
12
16
|
constructor(properties: P, options?: ParseOptions);
|
|
13
17
|
get name(): string;
|
|
14
18
|
get properties(): P;
|
|
15
19
|
get input(): InferInputSchema<P>;
|
|
16
|
-
|
|
20
|
+
optional(): OptionalType<this>;
|
|
21
|
+
shape<T>(): ObjectType<P, T>;
|
|
22
|
+
extend<E extends {
|
|
23
|
+
[K in keyof E]: K extends keyof P ? never : Parsed<unknown>;
|
|
24
|
+
}>(extra: E | ObjectType<E>): ObjectType<Unpack<P & E>>;
|
|
17
25
|
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
18
26
|
toJSON(): {
|
|
19
27
|
type: "object";
|
|
20
28
|
properties: Dict<Serialized>;
|
|
21
29
|
};
|
|
22
30
|
}
|
|
31
|
+
export {};
|
|
23
32
|
//# sourceMappingURL=ObjectType.d.ts.map
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import E from "#errors";
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
3
4
|
import next from "#path/next";
|
|
5
|
+
import SE from "#schema-errors";
|
|
4
6
|
import is from "@rcompat/is";
|
|
5
7
|
export default class ObjectType extends GenericType {
|
|
6
8
|
#properties;
|
|
@@ -26,13 +28,24 @@ export default class ObjectType extends GenericType {
|
|
|
26
28
|
const Constructor = this.constructor;
|
|
27
29
|
return new Constructor(this.#properties, { ...this.#options, ...options });
|
|
28
30
|
}
|
|
29
|
-
|
|
30
|
-
return this
|
|
31
|
+
optional() {
|
|
32
|
+
return new OptionalType(this);
|
|
33
|
+
}
|
|
34
|
+
shape() {
|
|
35
|
+
return new ObjectType(this.#properties, this.#options);
|
|
36
|
+
}
|
|
37
|
+
extend(extra) {
|
|
38
|
+
const properties = extra instanceof ObjectType ? extra.properties : extra;
|
|
39
|
+
for (const key of Object.keys(properties)) {
|
|
40
|
+
if (key in this.#properties)
|
|
41
|
+
throw SE.extend_key_collision(key);
|
|
42
|
+
}
|
|
43
|
+
return new ObjectType({ ...this.#properties, ...properties }, this.#options);
|
|
31
44
|
}
|
|
32
45
|
parse(x, options = {}) {
|
|
33
46
|
const $options = { ...this.#options, ...options };
|
|
34
|
-
if (x
|
|
35
|
-
throw
|
|
47
|
+
if (is.defined(x) && !is.dict(x))
|
|
48
|
+
throw E.invalid_type(x, "object", $options);
|
|
36
49
|
const input = x ?? {};
|
|
37
50
|
const out = {};
|
|
38
51
|
for (const k in this.#properties) {
|
package/lib/private/OmitType.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import E from "#errors";
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
3
|
import ParsedKey from "#ParsedKey";
|
|
4
4
|
import join from "#path/join";
|
|
5
|
+
import is from "@rcompat/is";
|
|
5
6
|
export default class OmitType extends GenericType {
|
|
6
7
|
#properties;
|
|
7
8
|
constructor(type, keys) {
|
|
@@ -16,8 +17,8 @@ export default class OmitType extends GenericType {
|
|
|
16
17
|
return "omit";
|
|
17
18
|
}
|
|
18
19
|
parse(x, options = {}) {
|
|
19
|
-
if (
|
|
20
|
-
throw
|
|
20
|
+
if (!is.dict(x))
|
|
21
|
+
throw E.invalid_type(x, "object", options);
|
|
21
22
|
const out = {};
|
|
22
23
|
const props = this.#properties;
|
|
23
24
|
for (const k in props) {
|