@pinesw/rttg 0.1.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/LICENSE +201 -0
- package/README.md +5 -0
- package/index.d.ts +83 -0
- package/index.d.ts.map +1 -0
- package/index.js +150 -0
- package/index.js.map +1 -0
- package/index.ts +199 -0
- package/package.json +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines a schema for validating and asserting types in TypeScript.
|
|
3
|
+
* At compile time, it provides type safety, and at runtime, it ensures that values conform to the expected types.
|
|
4
|
+
*/
|
|
5
|
+
export interface Schema<T> {
|
|
6
|
+
/**
|
|
7
|
+
* Validates if the given value conforms to the schema.
|
|
8
|
+
*/
|
|
9
|
+
validate(value: unknown): value is T;
|
|
10
|
+
/**
|
|
11
|
+
* Asserts that the given value conforms to the schema.
|
|
12
|
+
* Throws an error if the value does not match the schema.
|
|
13
|
+
*/
|
|
14
|
+
assertType(value: unknown): asserts value is T;
|
|
15
|
+
/**
|
|
16
|
+
* Return the given value as the type defined by the schema.
|
|
17
|
+
* Throws an error if the value does not match the schema.
|
|
18
|
+
* Useful for type casting with runtime validation.
|
|
19
|
+
*/
|
|
20
|
+
as(value: unknown): T;
|
|
21
|
+
/**
|
|
22
|
+
* A hack to make the schema type parameter `T` bivariant.
|
|
23
|
+
* Ignore this property; its only purpose is to influence TypeScript's type inference.
|
|
24
|
+
*/
|
|
25
|
+
bivarianceHack?: (arg: T) => void;
|
|
26
|
+
}
|
|
27
|
+
type IntersectArrayElements<T extends unknown[]> = T extends [infer F, ...infer R] ? F & IntersectArrayElements<R> : unknown;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a schema that validates if a value is exactly equal to the specified value.
|
|
30
|
+
*/
|
|
31
|
+
export declare function of<T>(value: T): Schema<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a schema that validates if a value is an instance of the specified class or constructor function.
|
|
34
|
+
*/
|
|
35
|
+
export declare function instanceOf<T>(constructor: new (...args: any[]) => T): Schema<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a schema that validates if a value is a number.
|
|
38
|
+
*/
|
|
39
|
+
export declare function number(): Schema<number>;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a schema that validates if a value is a string.
|
|
42
|
+
*/
|
|
43
|
+
export declare function string(): Schema<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a schema that validates if a value is a boolean.
|
|
46
|
+
*/
|
|
47
|
+
export declare function boolean(): Schema<boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a schema that validates if a value is an object matching the keys and schemas defined in the input.
|
|
50
|
+
* Extra keys in the object that are not defined in the schema are ignored.
|
|
51
|
+
*/
|
|
52
|
+
export declare function object<T extends {}>(schema: {
|
|
53
|
+
[K in keyof T]: Schema<T[K]>;
|
|
54
|
+
}): Schema<T>;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a schema that validates if a value is an array where each element conforms to the specified schema.
|
|
57
|
+
*/
|
|
58
|
+
export declare function array<T>(schema: Schema<T>): Schema<T[]>;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a schema that validates if a value is a tuple where each element conforms to the corresponding schema.
|
|
61
|
+
*/
|
|
62
|
+
export declare function tuple<T extends unknown[]>(...schemas: {
|
|
63
|
+
[K in keyof T]: Schema<T[K]>;
|
|
64
|
+
}): Schema<T>;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a schema that validates if a value conforms to at least one of the provided schemas.
|
|
67
|
+
*/
|
|
68
|
+
export declare function union<T extends unknown[]>(...schemas: {
|
|
69
|
+
[K in keyof T]: Schema<T[K]>;
|
|
70
|
+
}): Schema<T[number]>;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a schema that validates if a value conforms to all of the provided schemas.
|
|
73
|
+
*/
|
|
74
|
+
export declare function intersection<T extends unknown[]>(...schemas: {
|
|
75
|
+
[K in keyof T]: Schema<T[K]>;
|
|
76
|
+
}): Schema<IntersectArrayElements<T>>;
|
|
77
|
+
/**
|
|
78
|
+
* Asserts that a value conforms to the specified schema.
|
|
79
|
+
* Throws an error if the value does not match the schema.
|
|
80
|
+
*/
|
|
81
|
+
export declare function assertType<T>(value: unknown, schema: Schema<T>): asserts value is T;
|
|
82
|
+
export {};
|
|
83
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,MAAM,CAAC,CAAC;IACrB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,CAAC,CAAC;IAErC;;;OAGG;IACH,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC;IAE/C;;;;OAIG;IACH,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC;IAEtB;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;CACrC;AAED,KAAK,sBAAsB,CAAC,CAAC,SAAS,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,GAC5E,CAAC,GAAG,sBAAsB,CAAC,CAAC,CAAC,GAC7B,OAAO,CAAC;AAwBd;;GAEG;AACH,wBA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,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAInF"}
|
package/index.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
function fromOptions(options) {
|
|
2
|
+
return {
|
|
3
|
+
validate: options.validationFn,
|
|
4
|
+
assertType(value) {
|
|
5
|
+
if (!this.validate(value)) {
|
|
6
|
+
throw new Error(options.errorMessage);
|
|
7
|
+
}
|
|
8
|
+
},
|
|
9
|
+
as(value) {
|
|
10
|
+
if (!this.validate(value)) {
|
|
11
|
+
throw new Error(options.errorMessage);
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a schema that validates if a value is exactly equal to the specified value.
|
|
19
|
+
*/
|
|
20
|
+
export function of(value) {
|
|
21
|
+
return fromOptions({
|
|
22
|
+
validationFn: (v) => v === value,
|
|
23
|
+
errorMessage: `Expected ${value}`,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates a schema that validates if a value is an instance of the specified class or constructor function.
|
|
28
|
+
*/
|
|
29
|
+
export function instanceOf(constructor) {
|
|
30
|
+
return fromOptions({
|
|
31
|
+
validationFn: (value) => value instanceof constructor,
|
|
32
|
+
errorMessage: `Expected instance of ${constructor.name}`,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates a schema that validates if a value is a number.
|
|
37
|
+
*/
|
|
38
|
+
export function number() {
|
|
39
|
+
return fromOptions({
|
|
40
|
+
validationFn(value) {
|
|
41
|
+
return typeof value === "number";
|
|
42
|
+
},
|
|
43
|
+
errorMessage: "Expected number",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates a schema that validates if a value is a string.
|
|
48
|
+
*/
|
|
49
|
+
export function string() {
|
|
50
|
+
return fromOptions({
|
|
51
|
+
validationFn(value) {
|
|
52
|
+
return typeof value === "string";
|
|
53
|
+
},
|
|
54
|
+
errorMessage: "Expected string",
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Creates a schema that validates if a value is a boolean.
|
|
59
|
+
*/
|
|
60
|
+
export function boolean() {
|
|
61
|
+
return fromOptions({
|
|
62
|
+
validationFn(value) {
|
|
63
|
+
return typeof value === "boolean";
|
|
64
|
+
},
|
|
65
|
+
errorMessage: "Expected boolean",
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Creates a schema that validates if a value is an object matching the keys and schemas defined in the input.
|
|
70
|
+
* Extra keys in the object that are not defined in the schema are ignored.
|
|
71
|
+
*/
|
|
72
|
+
export function object(schema) {
|
|
73
|
+
return fromOptions({
|
|
74
|
+
validationFn(value) {
|
|
75
|
+
if (typeof value !== "object" || value === null) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
for (const key in schema) {
|
|
79
|
+
if (!(key in value) || !schema[key].validate(value[key])) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
},
|
|
85
|
+
errorMessage: "Object validation failed",
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Creates a schema that validates if a value is an array where each element conforms to the specified schema.
|
|
90
|
+
*/
|
|
91
|
+
export function array(schema) {
|
|
92
|
+
return fromOptions({
|
|
93
|
+
validationFn(value) {
|
|
94
|
+
if (!Array.isArray(value)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return value.every((item) => schema.validate(item));
|
|
98
|
+
},
|
|
99
|
+
errorMessage: "Array validation failed",
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Creates a schema that validates if a value is a tuple where each element conforms to the corresponding schema.
|
|
104
|
+
*/
|
|
105
|
+
export function tuple(...schemas) {
|
|
106
|
+
return fromOptions({
|
|
107
|
+
validationFn(value) {
|
|
108
|
+
if (!Array.isArray(value)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (value.length !== schemas.length) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
return value.every((item, index) => schemas[index]?.validate(item));
|
|
115
|
+
},
|
|
116
|
+
errorMessage: "Tuple validation failed",
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Creates a schema that validates if a value conforms to at least one of the provided schemas.
|
|
121
|
+
*/
|
|
122
|
+
export function union(...schemas) {
|
|
123
|
+
return fromOptions({
|
|
124
|
+
validationFn(value) {
|
|
125
|
+
return schemas.some((schema) => schema.validate(value));
|
|
126
|
+
},
|
|
127
|
+
errorMessage: "Union validation failed",
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Creates a schema that validates if a value conforms to all of the provided schemas.
|
|
132
|
+
*/
|
|
133
|
+
export function intersection(...schemas) {
|
|
134
|
+
return fromOptions({
|
|
135
|
+
validationFn(value) {
|
|
136
|
+
return schemas.every((schema) => schema.validate(value));
|
|
137
|
+
},
|
|
138
|
+
errorMessage: "Intersection validation failed",
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Asserts that a value conforms to the specified schema.
|
|
143
|
+
* Throws an error if the value does not match the schema.
|
|
144
|
+
*/
|
|
145
|
+
export function assertType(value, schema) {
|
|
146
|
+
if (!schema.validate(value)) {
|
|
147
|
+
throw new Error("Type assertion failed");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAuCA,SAAS,WAAW,CAAI,OAAyB;IAC7C,OAAO;QACH,QAAQ,EAAE,OAAO,CAAC,YAAY;QAC9B,UAAU,CAAC,KAAK;Y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it provides type safety, and at runtime, it ensures that values conform to the expected types.
|
|
4
|
+
*/
|
|
5
|
+
export interface Schema<T> {
|
|
6
|
+
/**
|
|
7
|
+
* Validates if the given value conforms to the schema.
|
|
8
|
+
*/
|
|
9
|
+
validate(value: unknown): value is T;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Asserts that the given value conforms to the schema.
|
|
13
|
+
* Throws an error if the value does not match the schema.
|
|
14
|
+
*/
|
|
15
|
+
assertType(value: unknown): asserts value is T;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Return the given value as the type defined by the schema.
|
|
19
|
+
* Throws an error if the value does not match the schema.
|
|
20
|
+
* Useful for type casting with runtime validation.
|
|
21
|
+
*/
|
|
22
|
+
as(value: unknown): T;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A hack to make the schema type parameter `T` bivariant.
|
|
26
|
+
* Ignore this property; its only purpose is to influence TypeScript's type inference.
|
|
27
|
+
*/
|
|
28
|
+
bivarianceHack?: (arg: T) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type IntersectArrayElements<T extends unknown[]> = T extends [infer F, ...infer R]
|
|
32
|
+
? F & IntersectArrayElements<R>
|
|
33
|
+
: unknown;
|
|
34
|
+
|
|
35
|
+
interface CreateOptions<T> {
|
|
36
|
+
validationFn: (value: unknown) => value is T;
|
|
37
|
+
errorMessage: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function fromOptions<T>(options: CreateOptions<T>): Schema<T> {
|
|
41
|
+
return {
|
|
42
|
+
validate: options.validationFn,
|
|
43
|
+
assertType(value): asserts value is T {
|
|
44
|
+
if (!this.validate(value)) {
|
|
45
|
+
throw new Error(options.errorMessage);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
as(value: unknown): T {
|
|
49
|
+
if (!this.validate(value)) {
|
|
50
|
+
throw new Error(options.errorMessage);
|
|
51
|
+
}
|
|
52
|
+
return value as T;
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Creates a schema that validates if a value is exactly equal to the specified value.
|
|
59
|
+
*/
|
|
60
|
+
export function of<T>(value: T): Schema<T> {
|
|
61
|
+
return fromOptions({
|
|
62
|
+
validationFn: (v): v is T => v === value,
|
|
63
|
+
errorMessage: `Expected ${value}`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Creates a schema that validates if a value is an instance of the specified class or constructor function.
|
|
69
|
+
*/
|
|
70
|
+
export function instanceOf<T>(constructor: new (...args: any[]) => T): Schema<T> {
|
|
71
|
+
return fromOptions({
|
|
72
|
+
validationFn: (value): value is T => value instanceof constructor,
|
|
73
|
+
errorMessage: `Expected instance of ${constructor.name}`,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Creates a schema that validates if a value is a number.
|
|
79
|
+
*/
|
|
80
|
+
export function number() {
|
|
81
|
+
return fromOptions({
|
|
82
|
+
validationFn(value): value is number {
|
|
83
|
+
return typeof value === "number";
|
|
84
|
+
},
|
|
85
|
+
errorMessage: "Expected number",
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a schema that validates if a value is a string.
|
|
91
|
+
*/
|
|
92
|
+
export function string() {
|
|
93
|
+
return fromOptions({
|
|
94
|
+
validationFn(value): value is string {
|
|
95
|
+
return typeof value === "string";
|
|
96
|
+
},
|
|
97
|
+
errorMessage: "Expected string",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Creates a schema that validates if a value is a boolean.
|
|
103
|
+
*/
|
|
104
|
+
export function boolean() {
|
|
105
|
+
return fromOptions({
|
|
106
|
+
validationFn(value): value is boolean {
|
|
107
|
+
return typeof value === "boolean";
|
|
108
|
+
},
|
|
109
|
+
errorMessage: "Expected boolean",
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Creates a schema that validates if a value is an object matching the keys and schemas defined in the input.
|
|
115
|
+
* Extra keys in the object that are not defined in the schema are ignored.
|
|
116
|
+
*/
|
|
117
|
+
export function object<T extends {}>(schema: { [K in keyof T]: Schema<T[K]> }) {
|
|
118
|
+
return fromOptions({
|
|
119
|
+
validationFn(value): value is T {
|
|
120
|
+
if (typeof value !== "object" || value === null) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
for (const key in schema) {
|
|
124
|
+
if (!(key in value) || !schema[key].validate((value as Record<string, unknown>)[key])) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
},
|
|
130
|
+
errorMessage: "Object validation failed",
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Creates a schema that validates if a value is an array where each element conforms to the specified schema.
|
|
136
|
+
*/
|
|
137
|
+
export function array<T>(schema: Schema<T>) {
|
|
138
|
+
return fromOptions({
|
|
139
|
+
validationFn(value): value is T[] {
|
|
140
|
+
if (!Array.isArray(value)) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return value.every((item) => schema.validate(item));
|
|
144
|
+
},
|
|
145
|
+
errorMessage: "Array validation failed",
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Creates a schema that validates if a value is a tuple where each element conforms to the corresponding schema.
|
|
151
|
+
*/
|
|
152
|
+
export function tuple<T extends unknown[]>(...schemas: { [K in keyof T]: Schema<T[K]> }) {
|
|
153
|
+
return fromOptions({
|
|
154
|
+
validationFn(value): value is T {
|
|
155
|
+
if (!Array.isArray(value)) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
if (value.length !== schemas.length) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return value.every((item, index) => schemas[index]?.validate(item));
|
|
162
|
+
},
|
|
163
|
+
errorMessage: "Tuple validation failed",
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Creates a schema that validates if a value conforms to at least one of the provided schemas.
|
|
169
|
+
*/
|
|
170
|
+
export function union<T extends unknown[]>(...schemas: { [K in keyof T]: Schema<T[K]> }) {
|
|
171
|
+
return fromOptions({
|
|
172
|
+
validationFn(value): value is T[number] {
|
|
173
|
+
return schemas.some((schema) => schema.validate(value));
|
|
174
|
+
},
|
|
175
|
+
errorMessage: "Union validation failed",
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Creates a schema that validates if a value conforms to all of the provided schemas.
|
|
181
|
+
*/
|
|
182
|
+
export function intersection<T extends unknown[]>(...schemas: { [K in keyof T]: Schema<T[K]> }) {
|
|
183
|
+
return fromOptions({
|
|
184
|
+
validationFn(value): value is IntersectArrayElements<T> {
|
|
185
|
+
return schemas.every((schema) => schema.validate(value));
|
|
186
|
+
},
|
|
187
|
+
errorMessage: "Intersection validation failed",
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Asserts that a value conforms to the specified schema.
|
|
193
|
+
* Throws an error if the value does not match the schema.
|
|
194
|
+
*/
|
|
195
|
+
export function assertType<T>(value: unknown, schema: Schema<T>): asserts value is T {
|
|
196
|
+
if (!schema.validate(value)) {
|
|
197
|
+
throw new Error("Type assertion failed");
|
|
198
|
+
}
|
|
199
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pinesw/rttg",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Run-Time Type Guards",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"index.ts",
|
|
13
|
+
"index.js",
|
|
14
|
+
"index.js.map",
|
|
15
|
+
"index.d.ts",
|
|
16
|
+
"index.d.ts.map",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"author": "PineSW",
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^24.3.0",
|
|
24
|
+
"typescript": "^5.9.2"
|
|
25
|
+
}
|
|
26
|
+
}
|