@spudlabs/guardis 0.4.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/README.md +901 -0
- package/esm/mod.d.ts +55 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +43 -0
- package/esm/package.json +3 -0
- package/esm/specs/standard-schema-spec.v1.d.ts +56 -0
- package/esm/specs/standard-schema-spec.v1.d.ts.map +1 -0
- package/esm/specs/standard-schema-spec.v1.js +1 -0
- package/esm/src/batch.d.ts +23 -0
- package/esm/src/batch.d.ts.map +1 -0
- package/esm/src/batch.js +23 -0
- package/esm/src/brand.d.ts +62 -0
- package/esm/src/brand.d.ts.map +1 -0
- package/esm/src/brand.js +9 -0
- package/esm/src/context.d.ts +19 -0
- package/esm/src/context.d.ts.map +1 -0
- package/esm/src/context.js +41 -0
- package/esm/src/extend.d.ts +23 -0
- package/esm/src/extend.d.ts.map +1 -0
- package/esm/src/extend.js +9 -0
- package/esm/src/guard.d.ts +288 -0
- package/esm/src/guard.d.ts.map +1 -0
- package/esm/src/guard.js +631 -0
- package/esm/src/helpers/http.helpers.d.ts +3 -0
- package/esm/src/helpers/http.helpers.d.ts.map +1 -0
- package/esm/src/helpers/http.helpers.js +2 -0
- package/esm/src/helpers/strings.helpers.d.ts +18 -0
- package/esm/src/helpers/strings.helpers.d.ts.map +1 -0
- package/esm/src/helpers/strings.helpers.js +47 -0
- package/esm/src/introspect.d.ts +36 -0
- package/esm/src/introspect.d.ts.map +1 -0
- package/esm/src/introspect.js +25 -0
- package/esm/src/modules/async.d.ts +27 -0
- package/esm/src/modules/async.d.ts.map +1 -0
- package/esm/src/modules/async.js +38 -0
- package/esm/src/modules/http.branded.d.ts +42 -0
- package/esm/src/modules/http.branded.d.ts.map +1 -0
- package/esm/src/modules/http.branded.js +24 -0
- package/esm/src/modules/http.d.ts +46 -0
- package/esm/src/modules/http.d.ts.map +1 -0
- package/esm/src/modules/http.js +59 -0
- package/esm/src/modules/strings.branded.d.ts +185 -0
- package/esm/src/modules/strings.branded.d.ts.map +1 -0
- package/esm/src/modules/strings.branded.js +123 -0
- package/esm/src/modules/strings.d.ts +109 -0
- package/esm/src/modules/strings.d.ts.map +1 -0
- package/esm/src/modules/strings.js +147 -0
- package/esm/src/types.d.ts +318 -0
- package/esm/src/types.d.ts.map +1 -0
- package/esm/src/types.js +1 -0
- package/esm/src/utilities.d.ts +95 -0
- package/esm/src/utilities.d.ts.map +1 -0
- package/esm/src/utilities.js +196 -0
- package/package.json +40 -0
- package/script/mod.d.ts +55 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +83 -0
- package/script/package.json +3 -0
- package/script/specs/standard-schema-spec.v1.d.ts +56 -0
- package/script/specs/standard-schema-spec.v1.d.ts.map +1 -0
- package/script/specs/standard-schema-spec.v1.js +2 -0
- package/script/src/batch.d.ts +23 -0
- package/script/src/batch.d.ts.map +1 -0
- package/script/src/batch.js +26 -0
- package/script/src/brand.d.ts +62 -0
- package/script/src/brand.d.ts.map +1 -0
- package/script/src/brand.js +12 -0
- package/script/src/context.d.ts +19 -0
- package/script/src/context.d.ts.map +1 -0
- package/script/src/context.js +45 -0
- package/script/src/extend.d.ts +23 -0
- package/script/src/extend.d.ts.map +1 -0
- package/script/src/extend.js +12 -0
- package/script/src/guard.d.ts +288 -0
- package/script/src/guard.d.ts.map +1 -0
- package/script/src/guard.js +640 -0
- package/script/src/helpers/http.helpers.d.ts +3 -0
- package/script/src/helpers/http.helpers.d.ts.map +1 -0
- package/script/src/helpers/http.helpers.js +5 -0
- package/script/src/helpers/strings.helpers.d.ts +18 -0
- package/script/src/helpers/strings.helpers.d.ts.map +1 -0
- package/script/src/helpers/strings.helpers.js +52 -0
- package/script/src/introspect.d.ts +36 -0
- package/script/src/introspect.d.ts.map +1 -0
- package/script/src/introspect.js +31 -0
- package/script/src/modules/async.d.ts +27 -0
- package/script/src/modules/async.d.ts.map +1 -0
- package/script/src/modules/async.js +41 -0
- package/script/src/modules/http.branded.d.ts +42 -0
- package/script/src/modules/http.branded.d.ts.map +1 -0
- package/script/src/modules/http.branded.js +30 -0
- package/script/src/modules/http.d.ts +46 -0
- package/script/src/modules/http.d.ts.map +1 -0
- package/script/src/modules/http.js +62 -0
- package/script/src/modules/strings.branded.d.ts +185 -0
- package/script/src/modules/strings.branded.d.ts.map +1 -0
- package/script/src/modules/strings.branded.js +126 -0
- package/script/src/modules/strings.d.ts +109 -0
- package/script/src/modules/strings.d.ts.map +1 -0
- package/script/src/modules/strings.js +150 -0
- package/script/src/types.d.ts +318 -0
- package/script/src/types.d.ts.map +1 -0
- package/script/src/types.js +2 -0
- package/script/src/utilities.d.ts +95 -0
- package/script/src/utilities.d.ts.map +1 -0
- package/script/src/utilities.js +207 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from "../specs/standard-schema-spec.v1.js";
|
|
2
|
+
import type { includes, tupleHas } from "./utilities.js";
|
|
3
|
+
/**
|
|
4
|
+
* Context for tracking validation paths and collecting issues during validation.
|
|
5
|
+
* Only present during `validate()` calls, not during regular type guard checks.
|
|
6
|
+
*/
|
|
7
|
+
export interface Context {
|
|
8
|
+
/** The current path being validated (array of property keys and indices) */
|
|
9
|
+
readonly path: ReadonlyArray<PropertyKey>;
|
|
10
|
+
/** The collected validation issues */
|
|
11
|
+
readonly issues: StandardSchemaV1.Issue[];
|
|
12
|
+
/** Creates a new context with the segment added to the path */
|
|
13
|
+
pushPath(segment: PropertyKey): Context;
|
|
14
|
+
/** Adds an issue at the current path */
|
|
15
|
+
addIssue(message: string): void;
|
|
16
|
+
}
|
|
17
|
+
type Helpers = {
|
|
18
|
+
/** Check for required property with optional custom error message */
|
|
19
|
+
has: <K extends PropertyKey, G = unknown>(t: object, k: K, guard?: (v: unknown) => v is G, errorMessage?: string) => t is {
|
|
20
|
+
[K2 in K]: G;
|
|
21
|
+
};
|
|
22
|
+
/** Check that a property does not exist with optional custom error message */
|
|
23
|
+
hasNot: <K extends PropertyKey>(t: object, k: K, errorMessage?: string) => t is {
|
|
24
|
+
[K2 in K]: never;
|
|
25
|
+
};
|
|
26
|
+
/** Check for optional property with optional custom error message */
|
|
27
|
+
hasOptional: <K extends PropertyKey, G = unknown>(t: object, k: K, guard?: (v: unknown) => v is G, errorMessage?: string) => t is {
|
|
28
|
+
[K2 in K]+?: G;
|
|
29
|
+
};
|
|
30
|
+
tupleHas: typeof tupleHas;
|
|
31
|
+
includes: typeof includes;
|
|
32
|
+
/** Check if a key exists in an object with optional custom error message */
|
|
33
|
+
keyOf: <T extends object>(k: unknown, t: T, errorMessage?: string) => k is keyof T;
|
|
34
|
+
/** Returns null and adds custom error message to context if during validation */
|
|
35
|
+
fail: (message: string) => null;
|
|
36
|
+
};
|
|
37
|
+
/** Helpers extended with internal context access for validation */
|
|
38
|
+
export type HelpersWithContext = Helpers & {
|
|
39
|
+
_ctx?: Context;
|
|
40
|
+
};
|
|
41
|
+
/** A parser is a function that takes an unknown and returns T or null */
|
|
42
|
+
export type Parser<T = unknown> = (val: unknown, helper: Helpers) => T | null;
|
|
43
|
+
export type ExtendedParser<T1, T2 extends T1 = T1> = (val: T1, helper: Helpers) => T2 | null;
|
|
44
|
+
export type Predicate<T> = (val: unknown) => val is T;
|
|
45
|
+
export type StrictTypeGuard<T> = (value: unknown, errorMsg?: string) => value is T;
|
|
46
|
+
/**
|
|
47
|
+
* Represents a type guard function with additional utility methods.
|
|
48
|
+
*
|
|
49
|
+
* A TypeGuard is a function that determines if a value is of type T, providing
|
|
50
|
+
* type narrowing in TypeScript. This type extends the basic type guard with:
|
|
51
|
+
* - strict mode validation
|
|
52
|
+
* - assertion functions that throw errors for invalid values
|
|
53
|
+
* - utilities for handling non-empty and optional values
|
|
54
|
+
*
|
|
55
|
+
* @template T1 The type being guarded
|
|
56
|
+
*/
|
|
57
|
+
export interface TypeGuard<T1> extends StandardSchemaV1<T1> {
|
|
58
|
+
/**
|
|
59
|
+
* A utility to gain access to the type being guarded. Can be used
|
|
60
|
+
* to infer the type in other parts of the code.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* type GuardedType = typeof isString._TYPE; // string
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
_TYPE: T1;
|
|
68
|
+
/**
|
|
69
|
+
* A type guard function that checks if the value is of type T.
|
|
70
|
+
* @param value The value to check
|
|
71
|
+
* @returns true if the value is of type T, otherwise false
|
|
72
|
+
*/
|
|
73
|
+
(value: unknown): value is T1;
|
|
74
|
+
/**
|
|
75
|
+
* A type guard function that checks if the value is of type T or T2.
|
|
76
|
+
* This is useful for creating unions of types.
|
|
77
|
+
* @param guard A type guard for T2
|
|
78
|
+
* @returns A new type guard that checks if the value is of type T or T2
|
|
79
|
+
*/
|
|
80
|
+
or: <T2>(guard: Predicate<T2>) => TypeGuard<T1 | T2>;
|
|
81
|
+
/**
|
|
82
|
+
* A strict type guard that throws an error if the value is not of type T.
|
|
83
|
+
* @param value The value to check
|
|
84
|
+
* @param errorMsg Optional error message to include in the thrown error
|
|
85
|
+
* @returns true if the value is of type T, otherwise throws an error
|
|
86
|
+
*/
|
|
87
|
+
strict: StrictTypeGuard<T1>;
|
|
88
|
+
/**
|
|
89
|
+
* An assertion function that throws an error if the value is not of type T.
|
|
90
|
+
* This is useful for ensuring that a value meets the type requirements at runtime.
|
|
91
|
+
*
|
|
92
|
+
* Unfortunately, TypeScript does not support the inference of assertion functions
|
|
93
|
+
* so the function must be invoked by declaring an intermediate variable and specifying
|
|
94
|
+
* the type.
|
|
95
|
+
*
|
|
96
|
+
* Example:
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const value: unknown = someValue();
|
|
99
|
+
*
|
|
100
|
+
* const assertIsString: typeof isString.assert = isString.assert;
|
|
101
|
+
* assertIsString(value, "Expected a string");
|
|
102
|
+
* // After this line, TypeScript knows that value is a string
|
|
103
|
+
* ```
|
|
104
|
+
* @param value The value to check
|
|
105
|
+
* @param errorMsg Optional error message to include in the thrown error
|
|
106
|
+
* @returns Asserts that the value is of type T
|
|
107
|
+
*/
|
|
108
|
+
assert: (value: unknown, errorMsg?: string) => asserts value is T1;
|
|
109
|
+
/**
|
|
110
|
+
* Validates the value against the schema. If the value is of type T1,
|
|
111
|
+
* it returns a success result with the value, otherwise it returns a failure result with issues.
|
|
112
|
+
*
|
|
113
|
+
* Included as a shortcut to the `validate` method of the StandardSchemaV1 interface.
|
|
114
|
+
* @param value The value to validate
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
validate: (value: unknown) => StandardSchemaV1.Result<T1>;
|
|
118
|
+
/**
|
|
119
|
+
* Extends the current type guard with an additional parser, building upon
|
|
120
|
+
* the existing type guard. The new type guard will first check if the value
|
|
121
|
+
* passes the original type guard, and if it does, it will then apply the
|
|
122
|
+
* additional parser.
|
|
123
|
+
* @param {Function} parse An additional parser to further validate the type.
|
|
124
|
+
* @returns {Function} A new type guard that combines the original and additional parsers.
|
|
125
|
+
*/
|
|
126
|
+
extend: IsExtensible<T1> extends false ? never : T1 extends Record<string, unknown> ? {
|
|
127
|
+
<S extends TypeGuardShape>(shape: S): TypeGuard<T1 & InferShape<S>>;
|
|
128
|
+
<S extends TypeGuardShape>(name: string, shape: S): TypeGuard<T1 & InferShape<S>>;
|
|
129
|
+
<T2 extends T1>(parse: ExtendedParser<T1, T2>): TypeGuard<T2>;
|
|
130
|
+
<T2 extends T1>(name: string, parse: ExtendedParser<T1, T2>): TypeGuard<T2>;
|
|
131
|
+
} : {
|
|
132
|
+
<T2 extends T1>(parse: ExtendedParser<T1, T2>): TypeGuard<T2>;
|
|
133
|
+
<T2 extends T1>(name: string, parse: ExtendedParser<T1, T2>): TypeGuard<T2>;
|
|
134
|
+
};
|
|
135
|
+
optional: {
|
|
136
|
+
/**
|
|
137
|
+
* A type guard that checks if the value is either undefined or of type T.
|
|
138
|
+
* @param value The value to check
|
|
139
|
+
* @returns true if the value is of type T or undefined, otherwise false
|
|
140
|
+
*/
|
|
141
|
+
(value: unknown): value is T1 | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* A strict type guard that throws an error if the value is defined but not of type T.
|
|
144
|
+
* @param value The value to check
|
|
145
|
+
* @param errorMsg Optional error message to include in the thrown error
|
|
146
|
+
* @returns true if the value is of type T, otherwise throws an error
|
|
147
|
+
*/
|
|
148
|
+
strict: (value: unknown, errorMsg?: string) => value is T1 | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* An assertion function that throws an error if the value is defined but not of type T.
|
|
151
|
+
* This is useful for ensuring that a value meets the type requirements at runtime.
|
|
152
|
+
*
|
|
153
|
+
* Unfortunately, TypeScript does not support the inference of assertion functions
|
|
154
|
+
* so the function must be invoked by declaring an intermediate variable and specifying
|
|
155
|
+
* the type.
|
|
156
|
+
*
|
|
157
|
+
* Example:
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const value: unknown = someValue();
|
|
160
|
+
*
|
|
161
|
+
* const assertIsOptionalString: typeof isString.optional.assert = isString.optional.assert;
|
|
162
|
+
* assertIsOptionalString(value, "Expected a string or undefined");
|
|
163
|
+
* // After this line, TypeScript knows that value is a string
|
|
164
|
+
* ```
|
|
165
|
+
* @param value The value to check
|
|
166
|
+
* @param errorMsg Optional error message to include in the thrown error
|
|
167
|
+
* @returns Asserts that the value is of type T
|
|
168
|
+
*/
|
|
169
|
+
assert: (value: unknown, errorMsg?: string) => asserts value is T1 | undefined;
|
|
170
|
+
/**
|
|
171
|
+
* Validates the value against the schema, accepting undefined as valid.
|
|
172
|
+
* @param value The value to validate
|
|
173
|
+
* @returns A success result with the value (including undefined), or a failure result with issues.
|
|
174
|
+
*/
|
|
175
|
+
validate: (value: unknown) => StandardSchemaV1.Result<T1 | undefined>;
|
|
176
|
+
/**
|
|
177
|
+
* A type guard function that checks if the value is of type T1 | undefined | T2.
|
|
178
|
+
* This is useful for creating unions of types.
|
|
179
|
+
* @param guard A type guard for T2
|
|
180
|
+
* @returns A new type guard that checks if the value is of type T1 | undefined | T2
|
|
181
|
+
*/
|
|
182
|
+
or: <T2>(guard: Predicate<T2>) => TypeGuard<T1 | undefined | T2>;
|
|
183
|
+
/**
|
|
184
|
+
* A type guard that checks if the value is not empty and of type T | undefined.
|
|
185
|
+
* An empty value is defined as null, an empty string, an empty array,
|
|
186
|
+
* or an empty object.
|
|
187
|
+
* @param value The value to check
|
|
188
|
+
* @returns true if the value is of type T and not empty, otherwise false
|
|
189
|
+
*
|
|
190
|
+
* @note This method is equivalent to calling `isString.notEmpty.optional`
|
|
191
|
+
* on a type guard named `isString`.
|
|
192
|
+
*/
|
|
193
|
+
notEmpty: CanBeEmpty<T1> extends false ? never : TypeGuard<T1 | undefined>["notEmpty"];
|
|
194
|
+
};
|
|
195
|
+
notEmpty: CanBeEmpty<T1> extends false ? never : {
|
|
196
|
+
/**
|
|
197
|
+
* A type guard that checks if the value is not empty and of type T.
|
|
198
|
+
* An empty value is defined as null, undefined, an empty string, an empty array,
|
|
199
|
+
* or an empty object.
|
|
200
|
+
* @param value The value to check
|
|
201
|
+
* @returns true if the value is of type T and not empty, otherwise false
|
|
202
|
+
*/
|
|
203
|
+
(value: unknown): value is T1;
|
|
204
|
+
/**
|
|
205
|
+
* A strict type guard that throws an error if the value is not of type T
|
|
206
|
+
* or if the value is empty (null, undefined, empty string, empty array, or empty object).
|
|
207
|
+
* @param value The value to check
|
|
208
|
+
* @param errorMsg Optional error message to include in the thrown error
|
|
209
|
+
* @returns true if the value is of type T, otherwise throws an error
|
|
210
|
+
*/
|
|
211
|
+
strict: StrictTypeGuard<T1>;
|
|
212
|
+
/**
|
|
213
|
+
* An assertion function that throws an error if the value is not of type T or if it is
|
|
214
|
+
* empty. An empty value is defined as null, undefined, an empty string,
|
|
215
|
+
* an empty array, or an empty object. This is useful for ensuring that a value meets
|
|
216
|
+
* the type requirements at runtime.
|
|
217
|
+
*
|
|
218
|
+
* Unfortunately, TypeScript does not support the inference of assertion functions
|
|
219
|
+
* so the function must be invoked by declaring an intermediate variable and specifying
|
|
220
|
+
* the type.
|
|
221
|
+
*
|
|
222
|
+
* Example:
|
|
223
|
+
* ```typescript
|
|
224
|
+
* const value: unknown = someValue();
|
|
225
|
+
*
|
|
226
|
+
* const assertIsNotEmptyString: typeof isString.notEmpty.assert = isString.notEmpty.assert;
|
|
227
|
+
* assertIsNotEmptyString(value, "Expected a non-empty string");
|
|
228
|
+
* // After this line, TypeScript knows that value is a string
|
|
229
|
+
* ```
|
|
230
|
+
* @param value The value to check
|
|
231
|
+
* @param errorMsg Optional error message to include in the thrown error
|
|
232
|
+
* @returns Asserts that the value is of type T
|
|
233
|
+
*/
|
|
234
|
+
assert: (value: unknown, errorMsg?: string) => asserts value is T1;
|
|
235
|
+
/**
|
|
236
|
+
* Validates the value against the schema, ensuring it is not empty. If the value is of
|
|
237
|
+
* type T1 and not empty, it returns a success result with the value, otherwise it
|
|
238
|
+
* returns a failure result with issues.
|
|
239
|
+
*
|
|
240
|
+
* Included as a shortcut to the `validate` method of the StandardSchemaV1 interface.
|
|
241
|
+
* @param value The value to validate
|
|
242
|
+
* @returns
|
|
243
|
+
*/
|
|
244
|
+
validate: (value: unknown) => StandardSchemaV1.Result<T1>;
|
|
245
|
+
/**
|
|
246
|
+
* A type guard function that checks if the value is of type T1 or T2.
|
|
247
|
+
* This is useful for creating unions of types.
|
|
248
|
+
* @param guard A type guard for T2
|
|
249
|
+
* @returns A new type guard that checks if the value is of type T1 or T2
|
|
250
|
+
*/
|
|
251
|
+
or: <T2>(guard: Predicate<T2>) => TypeGuard<T1 | T2>;
|
|
252
|
+
/**
|
|
253
|
+
* A type guard that checks if the value is not empty and of type T | undefined.
|
|
254
|
+
* An empty value is defined as null, an empty string, an empty array,
|
|
255
|
+
* or an empty object.
|
|
256
|
+
* @param value The value to check
|
|
257
|
+
* @returns true if the value is of type T and not empty, otherwise false
|
|
258
|
+
*
|
|
259
|
+
* @note This method is equivalent to calling `isString.optional.notEmpty`
|
|
260
|
+
* on a type guard named `isString`.
|
|
261
|
+
*/
|
|
262
|
+
optional: TypeGuard<T1 | undefined>["optional"];
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/** Helper type to extract the guarded type from a TypeGuard */
|
|
266
|
+
export type GuardedType<G> = G extends TypeGuard<infer T> ? T : never;
|
|
267
|
+
/** A named parser entry with an explicit name for error messages. */
|
|
268
|
+
export type NamedParser = {
|
|
269
|
+
parse: Parser;
|
|
270
|
+
name: string;
|
|
271
|
+
};
|
|
272
|
+
/** A single entry in a ParserRecords config: a parser function, a named parser, or a shape. */
|
|
273
|
+
export type ParserEntry = Parser | NamedParser | TypeGuardShape;
|
|
274
|
+
/** A record describing various types and their parsers or shapes. This can be used to generate
|
|
275
|
+
* a customized Is dictionary. */
|
|
276
|
+
export type ParserRecords = Record<string, ParserEntry>;
|
|
277
|
+
/** Infers the guarded type from a ParserEntry (parser function, named parser, or shape). */
|
|
278
|
+
export type InferEntry<P> = P extends Parser<infer T> ? T : P extends NamedParser ? (P["parse"] extends Parser<infer T> ? T : never) : P extends TypeGuardShape ? InferShape<P> : never;
|
|
279
|
+
/** Any valid primitive json value. */
|
|
280
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
281
|
+
/** An array of JSON-able values. */
|
|
282
|
+
export type JsonArray = JsonValue[] | readonly JsonValue[];
|
|
283
|
+
/** An object containing only JSON-able values. */
|
|
284
|
+
export type JsonObject = {
|
|
285
|
+
[Key in string]: JsonValue;
|
|
286
|
+
} & {
|
|
287
|
+
[Key in string]?: JsonValue | undefined;
|
|
288
|
+
};
|
|
289
|
+
/** The complete set of JSON-able data types. */
|
|
290
|
+
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
291
|
+
/** Construct a tuple of unknowns, up to size 10. */
|
|
292
|
+
export type TupleOfLength<N extends number> = N extends 0 ? [] : N extends 1 ? [unknown] : N extends 2 ? [unknown, unknown] : N extends 3 ? [unknown, unknown, unknown] : N extends 4 ? [unknown, unknown, unknown, unknown] : N extends 5 ? [unknown, unknown, unknown, unknown, unknown] : N extends 6 ? [unknown, unknown, unknown, unknown, unknown, unknown] : N extends 7 ? [unknown, unknown, unknown, unknown, unknown, unknown, unknown] : N extends 8 ? [unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown] : N extends 9 ? [unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown] : unknown[];
|
|
293
|
+
/** Replace the type at position X in tuple T with type R */
|
|
294
|
+
export type ReplaceTupleIndex<T extends readonly unknown[], X extends number, R> = {
|
|
295
|
+
readonly [K in keyof T]: K extends `${X}` ? R : T[K];
|
|
296
|
+
};
|
|
297
|
+
/** Utility type to determine if a type can be "empty" */
|
|
298
|
+
export type CanBeEmpty<T> = T extends null | undefined | string | unknown[] | readonly unknown[] | Record<PropertyKey, unknown> ? true : false;
|
|
299
|
+
/** Utility type to determine if a type is extensible */
|
|
300
|
+
export type IsExtensible<T> = T extends null | undefined ? false : true;
|
|
301
|
+
/** A guard predicate function — broad enough for TypeGuard, .optional, .notEmpty, and custom guards */
|
|
302
|
+
export type TypeGuardPredicate = ((value: unknown) => boolean) & {
|
|
303
|
+
validate?: (value: unknown) => any;
|
|
304
|
+
};
|
|
305
|
+
/** A shape object mapping property names to guard predicates or nested shapes */
|
|
306
|
+
export type TypeGuardShape = {
|
|
307
|
+
[key: string]: TypeGuardPredicate | TypeGuardShape;
|
|
308
|
+
};
|
|
309
|
+
/** Cosmetic type flattener for IDE tooltips */
|
|
310
|
+
export type Simplify<T> = {
|
|
311
|
+
[K in keyof T]: T[K];
|
|
312
|
+
} & {};
|
|
313
|
+
/** Recursively infers the TypeScript type from a TypeGuardShape */
|
|
314
|
+
export type InferShape<S extends TypeGuardShape> = Simplify<{
|
|
315
|
+
[K in keyof S]: S[K] extends (value: unknown) => value is infer T ? T : S[K] extends TypeGuardShape ? InferShape<S[K]> : never;
|
|
316
|
+
}>;
|
|
317
|
+
export {};
|
|
318
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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AAM,QAAQ,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,EAAE,CAAC;AAExD,mEAAmE;AACnE,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,cAAc,IAAI,QAAQ,CAAC;KACzD,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,MAAM,CAAC,GAAG,CAAC,GACjE,CAAC,CAAC,CAAC,CAAC,SAAS,cAAc,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAC9C,KAAK;CACV,CAAC,CAAC"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { Context, GuardedType, TypeGuard } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Utility to verify if a property exists in an object. Checks that
|
|
4
|
+
* k is a key in t. If a guard method is provided, it will also check
|
|
5
|
+
* that the value at k passes the guard.
|
|
6
|
+
* @param {object} t The object to check for property k.
|
|
7
|
+
* @param {string|number|Symbol} k The key for property k.
|
|
8
|
+
* @param {Function|undefined} guard The optional type guard to validate t[k].
|
|
9
|
+
* @param {Context|undefined} ctx Optional validation context for path tracking.
|
|
10
|
+
* Note: ctx is expected to already include the key in its path when passed.
|
|
11
|
+
* @param {string|undefined} errorMessage Optional custom error message to use instead of default.
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
14
|
+
export declare function hasProperty<K extends PropertyKey, G = unknown>(t: object, k: K, guard?: (v: unknown) => v is G, ctx?: Context, errorMessage?: string): t is {
|
|
15
|
+
[K2 in K]: G;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Checks if an object does not have a specific property.
|
|
19
|
+
*
|
|
20
|
+
* This function determines whether the given property key `k` does not exist
|
|
21
|
+
* in the object `t`. It uses TypeScript's type guard feature to refine the type
|
|
22
|
+
* of the object, ensuring that the property `k` is absent or explicitly `undefined`.
|
|
23
|
+
*
|
|
24
|
+
* @template K - The type of the property key to check.
|
|
25
|
+
* @param t - The object to check for the absence of the property.
|
|
26
|
+
* @param k - The property key to verify.
|
|
27
|
+
* @param ctx - Optional validation context for path tracking.
|
|
28
|
+
* @param errorMessage - Optional custom error message to use instead of default.
|
|
29
|
+
* @returns A boolean indicating whether the property `k` does not exist in the object `t`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function doesNotHaveProperty<K extends PropertyKey>(t: object, k: K, ctx?: Context, errorMessage?: string): t is {
|
|
32
|
+
[K2 in K]: never;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Checks if an object has an optional property that passes a type guard.
|
|
36
|
+
*
|
|
37
|
+
* This function verifies if a property is either:
|
|
38
|
+
* - Undefined (which is valid for optional properties)
|
|
39
|
+
* - Present and passes the specified type guard
|
|
40
|
+
|
|
41
|
+
* @param t - The object to check
|
|
42
|
+
* @param k - The property key to look for
|
|
43
|
+
* @param guard - Optional function that checks if the value is of type G
|
|
44
|
+
* @param ctx - Optional validation context for path tracking
|
|
45
|
+
* @param errorMessage - Optional custom error message to use instead of default
|
|
46
|
+
* @returns Type predicate indicating if the object has the optional property of type G
|
|
47
|
+
*/
|
|
48
|
+
export declare function hasOptionalProperty<K extends PropertyKey, G = unknown>(t: object, k: K, guard?: (v: unknown) => v is G, ctx?: Context, errorMessage?: string): t is {
|
|
49
|
+
[K2 in K]+?: G;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Utility to verify if a value is included in a tuple.
|
|
53
|
+
* @param {array} t The tuple to check for the presence of index i.
|
|
54
|
+
* @param {unknown} i The index to check.
|
|
55
|
+
* @param {Function} guard The type guard to validate t[i].
|
|
56
|
+
* @returns {boolean}
|
|
57
|
+
*/
|
|
58
|
+
export declare function tupleHas<T extends readonly unknown[], I extends number, G = unknown>(t: T, i: I, guard: (v: unknown) => v is G): t is T & {
|
|
59
|
+
[K in I]: G;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Determines whether the specified value `i` is included in the array `t`.
|
|
63
|
+
* Useful for checking if a value is part of a union type represented by a tuple.
|
|
64
|
+
* @typeParam T - A readonly array of unknown elements.
|
|
65
|
+
* @param t - The array to search within.
|
|
66
|
+
* @param v - The value to search for in the array.
|
|
67
|
+
* @returns `true` if `v` is found in `t`, otherwise `false`.
|
|
68
|
+
*/
|
|
69
|
+
export declare function includes<T extends readonly unknown[]>(t: T, v: unknown): v is T[number];
|
|
70
|
+
/**
|
|
71
|
+
* Determines if a given property key exists as a key in the specified object.
|
|
72
|
+
*
|
|
73
|
+
* @template T - The type of the object to check against.
|
|
74
|
+
* @param k - The property key to check.
|
|
75
|
+
* @param t - The object to check the property key against.
|
|
76
|
+
* @returns A boolean indicating whether the property key exists in the object.
|
|
77
|
+
*/
|
|
78
|
+
export declare function keyOf<T extends object>(k: unknown, t: T, ctx?: Context, errorMessage?: string): k is keyof T;
|
|
79
|
+
/**
|
|
80
|
+
* Formats an error message based on the provided value and optional metadata.
|
|
81
|
+
*
|
|
82
|
+
* @param value - The value that caused the error.
|
|
83
|
+
* @param name - An optional name of the expected type or condition.
|
|
84
|
+
* @returns A formatted error message string indicating the expected type (if provided) and the received value.
|
|
85
|
+
*/
|
|
86
|
+
export declare function formatErrorMessage(value: unknown, name?: string): string;
|
|
87
|
+
/**
|
|
88
|
+
* Creates a union type guard from a list of type guards.
|
|
89
|
+
*
|
|
90
|
+
* @typeParam G - A tuple of `TypeGuard<unknown>` types (must have at least one guard).
|
|
91
|
+
* @param guards - The type guards to combine into a union.
|
|
92
|
+
* @returns A type guard that accepts any value accepted by at least one of the provided guards.
|
|
93
|
+
*/
|
|
94
|
+
export declare const unionOf: <G extends readonly [TypeGuard<unknown>, ...TypeGuard<unknown>[]]>(...guards: G) => TypeGuard<GuardedType<G[number]>>;
|
|
95
|
+
//# sourceMappingURL=utilities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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J,CAAC,EAAE,CAAC,EACJ,KAAK,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,GAC5B,CAAC,IAAI,CAAC,GAAG;KAAG,CAAC,IAAI,CAAC,GAAG,CAAC;CAAE,CAE1B;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,SAAS,OAAO,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAEvF;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,MAAM,EACpC,CAAC,EAAE,OAAO,EACV,CAAC,EAAE,CAAC,EACJ,GAAG,CAAC,EAAE,OAAO,EACb,YAAY,CAAC,EAAE,MAAM,GACpB,CAAC,IAAI,MAAM,CAAC,CAYd;AAiCD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAQxE;AAED;;;;;;GAMG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,GAAG,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,EACtF,GAAG,QAAQ,CAAC,KACX,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAYlC,CAAC"}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unionOf = void 0;
|
|
4
|
+
exports.hasProperty = hasProperty;
|
|
5
|
+
exports.doesNotHaveProperty = doesNotHaveProperty;
|
|
6
|
+
exports.hasOptionalProperty = hasOptionalProperty;
|
|
7
|
+
exports.tupleHas = tupleHas;
|
|
8
|
+
exports.includes = includes;
|
|
9
|
+
exports.keyOf = keyOf;
|
|
10
|
+
exports.formatErrorMessage = formatErrorMessage;
|
|
11
|
+
const guard_js_1 = require("./guard.js");
|
|
12
|
+
const introspect_js_1 = require("./introspect.js");
|
|
13
|
+
/**
|
|
14
|
+
* Utility to verify if a property exists in an object. Checks that
|
|
15
|
+
* k is a key in t. If a guard method is provided, it will also check
|
|
16
|
+
* that the value at k passes the guard.
|
|
17
|
+
* @param {object} t The object to check for property k.
|
|
18
|
+
* @param {string|number|Symbol} k The key for property k.
|
|
19
|
+
* @param {Function|undefined} guard The optional type guard to validate t[k].
|
|
20
|
+
* @param {Context|undefined} ctx Optional validation context for path tracking.
|
|
21
|
+
* Note: ctx is expected to already include the key in its path when passed.
|
|
22
|
+
* @param {string|undefined} errorMessage Optional custom error message to use instead of default.
|
|
23
|
+
* @returns {boolean}
|
|
24
|
+
*/
|
|
25
|
+
function hasProperty(t, k, guard, ctx, errorMessage) {
|
|
26
|
+
if (!(k in t)) {
|
|
27
|
+
if (ctx)
|
|
28
|
+
ctx.addIssue(errorMessage ?? `Missing required property: ${String(k)}`);
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
if (!guard)
|
|
32
|
+
return true;
|
|
33
|
+
const value = t[k];
|
|
34
|
+
// If custom errorMessage provided, use simple boolean check and add custom message on failure
|
|
35
|
+
if (errorMessage) {
|
|
36
|
+
if (!guard(value)) {
|
|
37
|
+
if (ctx) {
|
|
38
|
+
ctx.addIssue(errorMessage);
|
|
39
|
+
// Note: Returns true despite failure to avoid short-circuiting && chains in parsers,
|
|
40
|
+
// allowing collection of multiple property errors. This is safe because validate()
|
|
41
|
+
// checks ctx.issues and returns { issues } instead of { value } when errors exist.
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
// If context is provided, use context-aware validation if available on the guard
|
|
49
|
+
if (ctx && (0, introspect_js_1.hasContext)(guard)) {
|
|
50
|
+
guard._.context(value, ctx); // Run validation, errors accumulate in ctx
|
|
51
|
+
// Note: Returns true despite potential failure to avoid short-circuiting && chains,
|
|
52
|
+
// enabling multi-property error collection. Safe because validate() checks ctx.issues.
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return guard(value);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Checks if an object does not have a specific property.
|
|
59
|
+
*
|
|
60
|
+
* This function determines whether the given property key `k` does not exist
|
|
61
|
+
* in the object `t`. It uses TypeScript's type guard feature to refine the type
|
|
62
|
+
* of the object, ensuring that the property `k` is absent or explicitly `undefined`.
|
|
63
|
+
*
|
|
64
|
+
* @template K - The type of the property key to check.
|
|
65
|
+
* @param t - The object to check for the absence of the property.
|
|
66
|
+
* @param k - The property key to verify.
|
|
67
|
+
* @param ctx - Optional validation context for path tracking.
|
|
68
|
+
* @param errorMessage - Optional custom error message to use instead of default.
|
|
69
|
+
* @returns A boolean indicating whether the property `k` does not exist in the object `t`.
|
|
70
|
+
*/
|
|
71
|
+
function doesNotHaveProperty(t, k, ctx, errorMessage) {
|
|
72
|
+
if (k in t) {
|
|
73
|
+
if (ctx)
|
|
74
|
+
ctx.addIssue(errorMessage ?? `Unexpected property: ${String(k)}`);
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Checks if an object has an optional property that passes a type guard.
|
|
81
|
+
*
|
|
82
|
+
* This function verifies if a property is either:
|
|
83
|
+
* - Undefined (which is valid for optional properties)
|
|
84
|
+
* - Present and passes the specified type guard
|
|
85
|
+
|
|
86
|
+
* @param t - The object to check
|
|
87
|
+
* @param k - The property key to look for
|
|
88
|
+
* @param guard - Optional function that checks if the value is of type G
|
|
89
|
+
* @param ctx - Optional validation context for path tracking
|
|
90
|
+
* @param errorMessage - Optional custom error message to use instead of default
|
|
91
|
+
* @returns Type predicate indicating if the object has the optional property of type G
|
|
92
|
+
*/
|
|
93
|
+
function hasOptionalProperty(t, k, guard, ctx, errorMessage) {
|
|
94
|
+
if ((0, guard_js_1.isUndefined)(t[k]))
|
|
95
|
+
return true;
|
|
96
|
+
return hasProperty(t, k, guard, ctx, errorMessage);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Utility to verify if a value is included in a tuple.
|
|
100
|
+
* @param {array} t The tuple to check for the presence of index i.
|
|
101
|
+
* @param {unknown} i The index to check.
|
|
102
|
+
* @param {Function} guard The type guard to validate t[i].
|
|
103
|
+
* @returns {boolean}
|
|
104
|
+
*/
|
|
105
|
+
function tupleHas(t, i, guard) {
|
|
106
|
+
return (i in t) && guard(t[i]);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Determines whether the specified value `i` is included in the array `t`.
|
|
110
|
+
* Useful for checking if a value is part of a union type represented by a tuple.
|
|
111
|
+
* @typeParam T - A readonly array of unknown elements.
|
|
112
|
+
* @param t - The array to search within.
|
|
113
|
+
* @param v - The value to search for in the array.
|
|
114
|
+
* @returns `true` if `v` is found in `t`, otherwise `false`.
|
|
115
|
+
*/
|
|
116
|
+
function includes(t, v) {
|
|
117
|
+
return t.includes(v);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Determines if a given property key exists as a key in the specified object.
|
|
121
|
+
*
|
|
122
|
+
* @template T - The type of the object to check against.
|
|
123
|
+
* @param k - The property key to check.
|
|
124
|
+
* @param t - The object to check the property key against.
|
|
125
|
+
* @returns A boolean indicating whether the property key exists in the object.
|
|
126
|
+
*/
|
|
127
|
+
function keyOf(k, t, ctx, errorMessage) {
|
|
128
|
+
if (typeof k !== "string" && typeof k !== "number" && typeof k !== "symbol") {
|
|
129
|
+
if (ctx)
|
|
130
|
+
ctx.addIssue(errorMessage ?? `Invalid key type: ${typeof k}`);
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (!(k in t)) {
|
|
134
|
+
if (ctx)
|
|
135
|
+
ctx.addIssue(errorMessage ?? `Key "${String(k)}" not found in object`);
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Safely stringifies a value for error messages, handling edge cases that JSON.stringify cannot.
|
|
142
|
+
*/
|
|
143
|
+
function safeStringify(value) {
|
|
144
|
+
if (value === null)
|
|
145
|
+
return "null";
|
|
146
|
+
if (value === undefined)
|
|
147
|
+
return "undefined";
|
|
148
|
+
const type = typeof value;
|
|
149
|
+
if (type === "string")
|
|
150
|
+
return `'${value}'`;
|
|
151
|
+
if (type === "boolean")
|
|
152
|
+
return String(value);
|
|
153
|
+
if (type === "symbol")
|
|
154
|
+
return value.toString();
|
|
155
|
+
if (type === "function") {
|
|
156
|
+
const fn = value;
|
|
157
|
+
return `[Function${fn.name ? `: ${fn.name}` : ""}]`;
|
|
158
|
+
}
|
|
159
|
+
if (type === "bigint")
|
|
160
|
+
return `${String(value)}n`;
|
|
161
|
+
if (type === "number") {
|
|
162
|
+
if (Number.isNaN(value))
|
|
163
|
+
return "NaN";
|
|
164
|
+
if (!Number.isFinite(value))
|
|
165
|
+
return value === Infinity ? "Infinity" : "-Infinity";
|
|
166
|
+
return String(value);
|
|
167
|
+
}
|
|
168
|
+
// Objects and arrays - wrap in try/catch for circular references
|
|
169
|
+
try {
|
|
170
|
+
return JSON.stringify(value, (_key, val) => (typeof val === "bigint" ? `${String(val)}n` : val));
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
return Object.prototype.toString.call(value);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Formats an error message based on the provided value and optional metadata.
|
|
178
|
+
*
|
|
179
|
+
* @param value - The value that caused the error.
|
|
180
|
+
* @param name - An optional name of the expected type or condition.
|
|
181
|
+
* @returns A formatted error message string indicating the expected type (if provided) and the received value.
|
|
182
|
+
*/
|
|
183
|
+
function formatErrorMessage(value, name) {
|
|
184
|
+
const received = safeStringify(value);
|
|
185
|
+
if (name) {
|
|
186
|
+
return `Expected ${name}. Received: ${received}`;
|
|
187
|
+
}
|
|
188
|
+
return `Invalid value. Received: ${received}`;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Creates a union type guard from a list of type guards.
|
|
192
|
+
*
|
|
193
|
+
* @typeParam G - A tuple of `TypeGuard<unknown>` types (must have at least one guard).
|
|
194
|
+
* @param guards - The type guards to combine into a union.
|
|
195
|
+
* @returns A type guard that accepts any value accepted by at least one of the provided guards.
|
|
196
|
+
*/
|
|
197
|
+
const unionOf = (...guards) => {
|
|
198
|
+
if (guards.length === 0) {
|
|
199
|
+
throw new Error("unionOf requires at least one type guard");
|
|
200
|
+
}
|
|
201
|
+
let store = guards[0];
|
|
202
|
+
for (let i = 1; i < guards.length; i++) {
|
|
203
|
+
store = store.or(guards[i]);
|
|
204
|
+
}
|
|
205
|
+
return store;
|
|
206
|
+
};
|
|
207
|
+
exports.unionOf = unionOf;
|