typebad 1.0.1 → 1.0.3
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/LICENSE +21 -0
- package/dist/cjs/Type.d.ts +2 -4
- package/dist/cjs/Type.js +18 -23
- package/dist/esm/Type.d.ts +2 -4
- package/dist/esm/Type.js +18 -23
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 iconshot
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/cjs/Type.d.ts
CHANGED
|
@@ -22,17 +22,15 @@ export interface ParseOptions {
|
|
|
22
22
|
export declare class Type<T> {
|
|
23
23
|
private matcher;
|
|
24
24
|
private parser;
|
|
25
|
-
private onParseCallback;
|
|
26
25
|
private allowMissingProperty;
|
|
27
|
-
onParse(callback: (value: OutputValue<T>) => void): Type<T>;
|
|
28
26
|
private parse;
|
|
29
|
-
private isFinalType;
|
|
30
27
|
extend(matcher: (value: ExtendedValue<T>) => boolean): Type<T>;
|
|
31
28
|
nullable(): Type<T | null>;
|
|
32
29
|
optional(): Type<T | null | undefined>;
|
|
33
30
|
default(defaultValue: T | (() => T)): Type<T | undefined>;
|
|
34
31
|
static match<T>(matcher: (value: any) => boolean): Type<T>;
|
|
35
|
-
static enum<const V extends readonly (string | number)[]>(values: V): Type<V[number]>;
|
|
32
|
+
static enum<const V extends readonly (string | number | boolean)[]>(values: V): Type<V[number]>;
|
|
33
|
+
static value<const V extends string | number | boolean>(value: V): Type<V | undefined>;
|
|
36
34
|
static array<T extends Type<any>>(elementType: T): Type<InputType<T>[]>;
|
|
37
35
|
static object<S extends TypeSchema>(schema: S): Type<InputSchema<S>>;
|
|
38
36
|
static union<U extends Type<any>[]>(types: U): Type<InputType<U[number]>>;
|
package/dist/cjs/Type.js
CHANGED
|
@@ -4,28 +4,16 @@ exports.Type = void 0;
|
|
|
4
4
|
class Type {
|
|
5
5
|
matcher = () => true;
|
|
6
6
|
parser = (value) => value;
|
|
7
|
-
onParseCallback = null;
|
|
8
7
|
allowMissingProperty = false;
|
|
9
|
-
onParse(callback) {
|
|
10
|
-
this.onParseCallback = callback;
|
|
11
|
-
return this;
|
|
12
|
-
}
|
|
13
8
|
parse(value, propertyName, options) {
|
|
14
9
|
const isMatching = this.matcher(value);
|
|
15
10
|
if (!isMatching) {
|
|
16
11
|
throw new Error(Type.getValueError(propertyName));
|
|
17
12
|
}
|
|
18
13
|
const parsedValue = this.parser(value, propertyName, options) ?? null;
|
|
19
|
-
this.onParseCallback?.(parsedValue);
|
|
20
14
|
return parsedValue;
|
|
21
15
|
}
|
|
22
|
-
isFinalType() {
|
|
23
|
-
return this.onParseCallback !== null;
|
|
24
|
-
}
|
|
25
16
|
extend(matcher) {
|
|
26
|
-
if (this.isFinalType()) {
|
|
27
|
-
throw new Error("Cannot extend a Type that has onParse callback.");
|
|
28
|
-
}
|
|
29
17
|
const type = new Type();
|
|
30
18
|
type.matcher = (value) => {
|
|
31
19
|
return this.matcher(value) && matcher(value);
|
|
@@ -35,9 +23,6 @@ class Type {
|
|
|
35
23
|
return type;
|
|
36
24
|
}
|
|
37
25
|
nullable() {
|
|
38
|
-
if (this.isFinalType()) {
|
|
39
|
-
throw new Error("Cannot extend a Type that has onParse callback.");
|
|
40
|
-
}
|
|
41
26
|
const type = new Type();
|
|
42
27
|
type.matcher = (value) => {
|
|
43
28
|
if (value === null) {
|
|
@@ -55,9 +40,6 @@ class Type {
|
|
|
55
40
|
return type;
|
|
56
41
|
}
|
|
57
42
|
optional() {
|
|
58
|
-
if (this.isFinalType()) {
|
|
59
|
-
throw new Error("Cannot extend a Type that has onParse callback.");
|
|
60
|
-
}
|
|
61
43
|
const type = new Type();
|
|
62
44
|
type.matcher = (value) => {
|
|
63
45
|
if (value === null || value === undefined) {
|
|
@@ -72,13 +54,9 @@ class Type {
|
|
|
72
54
|
return this.parser(value, propertyName, options);
|
|
73
55
|
};
|
|
74
56
|
type.allowMissingProperty = true;
|
|
75
|
-
type.onParseCallback = this.onParseCallback;
|
|
76
57
|
return type;
|
|
77
58
|
}
|
|
78
59
|
default(defaultValue) {
|
|
79
|
-
if (this.isFinalType()) {
|
|
80
|
-
throw new Error("Cannot extend a Type that has onParse callback.");
|
|
81
|
-
}
|
|
82
60
|
const type = new Type();
|
|
83
61
|
type.matcher = (value) => {
|
|
84
62
|
if (value === undefined) {
|
|
@@ -91,7 +69,7 @@ class Type {
|
|
|
91
69
|
const tmpValue = typeof defaultValue === "function"
|
|
92
70
|
? defaultValue()
|
|
93
71
|
: defaultValue;
|
|
94
|
-
return this.
|
|
72
|
+
return this.parse(tmpValue, propertyName, options);
|
|
95
73
|
}
|
|
96
74
|
return this.parser(value, propertyName, options);
|
|
97
75
|
};
|
|
@@ -110,6 +88,23 @@ class Type {
|
|
|
110
88
|
};
|
|
111
89
|
return type;
|
|
112
90
|
}
|
|
91
|
+
static value(value) {
|
|
92
|
+
const type = new Type();
|
|
93
|
+
type.matcher = (tmpValue) => {
|
|
94
|
+
if (tmpValue === undefined) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
return tmpValue === value;
|
|
98
|
+
};
|
|
99
|
+
type.parser = (tmpValue, propertyName, options) => {
|
|
100
|
+
if (tmpValue === undefined) {
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
return tmpValue;
|
|
104
|
+
};
|
|
105
|
+
type.allowMissingProperty = true;
|
|
106
|
+
return type;
|
|
107
|
+
}
|
|
113
108
|
static array(elementType) {
|
|
114
109
|
const type = new Type();
|
|
115
110
|
type.matcher = (value) => {
|
package/dist/esm/Type.d.ts
CHANGED
|
@@ -22,17 +22,15 @@ export interface ParseOptions {
|
|
|
22
22
|
export declare class Type<T> {
|
|
23
23
|
private matcher;
|
|
24
24
|
private parser;
|
|
25
|
-
private onParseCallback;
|
|
26
25
|
private allowMissingProperty;
|
|
27
|
-
onParse(callback: (value: OutputValue<T>) => void): Type<T>;
|
|
28
26
|
private parse;
|
|
29
|
-
private isFinalType;
|
|
30
27
|
extend(matcher: (value: ExtendedValue<T>) => boolean): Type<T>;
|
|
31
28
|
nullable(): Type<T | null>;
|
|
32
29
|
optional(): Type<T | null | undefined>;
|
|
33
30
|
default(defaultValue: T | (() => T)): Type<T | undefined>;
|
|
34
31
|
static match<T>(matcher: (value: any) => boolean): Type<T>;
|
|
35
|
-
static enum<const V extends readonly (string | number)[]>(values: V): Type<V[number]>;
|
|
32
|
+
static enum<const V extends readonly (string | number | boolean)[]>(values: V): Type<V[number]>;
|
|
33
|
+
static value<const V extends string | number | boolean>(value: V): Type<V | undefined>;
|
|
36
34
|
static array<T extends Type<any>>(elementType: T): Type<InputType<T>[]>;
|
|
37
35
|
static object<S extends TypeSchema>(schema: S): Type<InputSchema<S>>;
|
|
38
36
|
static union<U extends Type<any>[]>(types: U): Type<InputType<U[number]>>;
|
package/dist/esm/Type.js
CHANGED
|
@@ -1,28 +1,16 @@
|
|
|
1
1
|
export class Type {
|
|
2
2
|
matcher = () => true;
|
|
3
3
|
parser = (value) => value;
|
|
4
|
-
onParseCallback = null;
|
|
5
4
|
allowMissingProperty = false;
|
|
6
|
-
onParse(callback) {
|
|
7
|
-
this.onParseCallback = callback;
|
|
8
|
-
return this;
|
|
9
|
-
}
|
|
10
5
|
parse(value, propertyName, options) {
|
|
11
6
|
const isMatching = this.matcher(value);
|
|
12
7
|
if (!isMatching) {
|
|
13
8
|
throw new Error(Type.getValueError(propertyName));
|
|
14
9
|
}
|
|
15
10
|
const parsedValue = this.parser(value, propertyName, options) ?? null;
|
|
16
|
-
this.onParseCallback?.(parsedValue);
|
|
17
11
|
return parsedValue;
|
|
18
12
|
}
|
|
19
|
-
isFinalType() {
|
|
20
|
-
return this.onParseCallback !== null;
|
|
21
|
-
}
|
|
22
13
|
extend(matcher) {
|
|
23
|
-
if (this.isFinalType()) {
|
|
24
|
-
throw new Error("Cannot extend a Type that has onParse callback.");
|
|
25
|
-
}
|
|
26
14
|
const type = new Type();
|
|
27
15
|
type.matcher = (value) => {
|
|
28
16
|
return this.matcher(value) && matcher(value);
|
|
@@ -32,9 +20,6 @@ export class Type {
|
|
|
32
20
|
return type;
|
|
33
21
|
}
|
|
34
22
|
nullable() {
|
|
35
|
-
if (this.isFinalType()) {
|
|
36
|
-
throw new Error("Cannot extend a Type that has onParse callback.");
|
|
37
|
-
}
|
|
38
23
|
const type = new Type();
|
|
39
24
|
type.matcher = (value) => {
|
|
40
25
|
if (value === null) {
|
|
@@ -52,9 +37,6 @@ export class Type {
|
|
|
52
37
|
return type;
|
|
53
38
|
}
|
|
54
39
|
optional() {
|
|
55
|
-
if (this.isFinalType()) {
|
|
56
|
-
throw new Error("Cannot extend a Type that has onParse callback.");
|
|
57
|
-
}
|
|
58
40
|
const type = new Type();
|
|
59
41
|
type.matcher = (value) => {
|
|
60
42
|
if (value === null || value === undefined) {
|
|
@@ -69,13 +51,9 @@ export class Type {
|
|
|
69
51
|
return this.parser(value, propertyName, options);
|
|
70
52
|
};
|
|
71
53
|
type.allowMissingProperty = true;
|
|
72
|
-
type.onParseCallback = this.onParseCallback;
|
|
73
54
|
return type;
|
|
74
55
|
}
|
|
75
56
|
default(defaultValue) {
|
|
76
|
-
if (this.isFinalType()) {
|
|
77
|
-
throw new Error("Cannot extend a Type that has onParse callback.");
|
|
78
|
-
}
|
|
79
57
|
const type = new Type();
|
|
80
58
|
type.matcher = (value) => {
|
|
81
59
|
if (value === undefined) {
|
|
@@ -88,7 +66,7 @@ export class Type {
|
|
|
88
66
|
const tmpValue = typeof defaultValue === "function"
|
|
89
67
|
? defaultValue()
|
|
90
68
|
: defaultValue;
|
|
91
|
-
return this.
|
|
69
|
+
return this.parse(tmpValue, propertyName, options);
|
|
92
70
|
}
|
|
93
71
|
return this.parser(value, propertyName, options);
|
|
94
72
|
};
|
|
@@ -107,6 +85,23 @@ export class Type {
|
|
|
107
85
|
};
|
|
108
86
|
return type;
|
|
109
87
|
}
|
|
88
|
+
static value(value) {
|
|
89
|
+
const type = new Type();
|
|
90
|
+
type.matcher = (tmpValue) => {
|
|
91
|
+
if (tmpValue === undefined) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return tmpValue === value;
|
|
95
|
+
};
|
|
96
|
+
type.parser = (tmpValue, propertyName, options) => {
|
|
97
|
+
if (tmpValue === undefined) {
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
return tmpValue;
|
|
101
|
+
};
|
|
102
|
+
type.allowMissingProperty = true;
|
|
103
|
+
return type;
|
|
104
|
+
}
|
|
110
105
|
static array(elementType) {
|
|
111
106
|
const type = new Type();
|
|
112
107
|
type.matcher = (value) => {
|