@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.
Files changed (106) hide show
  1. package/README.md +901 -0
  2. package/esm/mod.d.ts +55 -0
  3. package/esm/mod.d.ts.map +1 -0
  4. package/esm/mod.js +43 -0
  5. package/esm/package.json +3 -0
  6. package/esm/specs/standard-schema-spec.v1.d.ts +56 -0
  7. package/esm/specs/standard-schema-spec.v1.d.ts.map +1 -0
  8. package/esm/specs/standard-schema-spec.v1.js +1 -0
  9. package/esm/src/batch.d.ts +23 -0
  10. package/esm/src/batch.d.ts.map +1 -0
  11. package/esm/src/batch.js +23 -0
  12. package/esm/src/brand.d.ts +62 -0
  13. package/esm/src/brand.d.ts.map +1 -0
  14. package/esm/src/brand.js +9 -0
  15. package/esm/src/context.d.ts +19 -0
  16. package/esm/src/context.d.ts.map +1 -0
  17. package/esm/src/context.js +41 -0
  18. package/esm/src/extend.d.ts +23 -0
  19. package/esm/src/extend.d.ts.map +1 -0
  20. package/esm/src/extend.js +9 -0
  21. package/esm/src/guard.d.ts +288 -0
  22. package/esm/src/guard.d.ts.map +1 -0
  23. package/esm/src/guard.js +631 -0
  24. package/esm/src/helpers/http.helpers.d.ts +3 -0
  25. package/esm/src/helpers/http.helpers.d.ts.map +1 -0
  26. package/esm/src/helpers/http.helpers.js +2 -0
  27. package/esm/src/helpers/strings.helpers.d.ts +18 -0
  28. package/esm/src/helpers/strings.helpers.d.ts.map +1 -0
  29. package/esm/src/helpers/strings.helpers.js +47 -0
  30. package/esm/src/introspect.d.ts +36 -0
  31. package/esm/src/introspect.d.ts.map +1 -0
  32. package/esm/src/introspect.js +25 -0
  33. package/esm/src/modules/async.d.ts +27 -0
  34. package/esm/src/modules/async.d.ts.map +1 -0
  35. package/esm/src/modules/async.js +38 -0
  36. package/esm/src/modules/http.branded.d.ts +42 -0
  37. package/esm/src/modules/http.branded.d.ts.map +1 -0
  38. package/esm/src/modules/http.branded.js +24 -0
  39. package/esm/src/modules/http.d.ts +46 -0
  40. package/esm/src/modules/http.d.ts.map +1 -0
  41. package/esm/src/modules/http.js +59 -0
  42. package/esm/src/modules/strings.branded.d.ts +185 -0
  43. package/esm/src/modules/strings.branded.d.ts.map +1 -0
  44. package/esm/src/modules/strings.branded.js +123 -0
  45. package/esm/src/modules/strings.d.ts +109 -0
  46. package/esm/src/modules/strings.d.ts.map +1 -0
  47. package/esm/src/modules/strings.js +147 -0
  48. package/esm/src/types.d.ts +318 -0
  49. package/esm/src/types.d.ts.map +1 -0
  50. package/esm/src/types.js +1 -0
  51. package/esm/src/utilities.d.ts +95 -0
  52. package/esm/src/utilities.d.ts.map +1 -0
  53. package/esm/src/utilities.js +196 -0
  54. package/package.json +40 -0
  55. package/script/mod.d.ts +55 -0
  56. package/script/mod.d.ts.map +1 -0
  57. package/script/mod.js +83 -0
  58. package/script/package.json +3 -0
  59. package/script/specs/standard-schema-spec.v1.d.ts +56 -0
  60. package/script/specs/standard-schema-spec.v1.d.ts.map +1 -0
  61. package/script/specs/standard-schema-spec.v1.js +2 -0
  62. package/script/src/batch.d.ts +23 -0
  63. package/script/src/batch.d.ts.map +1 -0
  64. package/script/src/batch.js +26 -0
  65. package/script/src/brand.d.ts +62 -0
  66. package/script/src/brand.d.ts.map +1 -0
  67. package/script/src/brand.js +12 -0
  68. package/script/src/context.d.ts +19 -0
  69. package/script/src/context.d.ts.map +1 -0
  70. package/script/src/context.js +45 -0
  71. package/script/src/extend.d.ts +23 -0
  72. package/script/src/extend.d.ts.map +1 -0
  73. package/script/src/extend.js +12 -0
  74. package/script/src/guard.d.ts +288 -0
  75. package/script/src/guard.d.ts.map +1 -0
  76. package/script/src/guard.js +640 -0
  77. package/script/src/helpers/http.helpers.d.ts +3 -0
  78. package/script/src/helpers/http.helpers.d.ts.map +1 -0
  79. package/script/src/helpers/http.helpers.js +5 -0
  80. package/script/src/helpers/strings.helpers.d.ts +18 -0
  81. package/script/src/helpers/strings.helpers.d.ts.map +1 -0
  82. package/script/src/helpers/strings.helpers.js +52 -0
  83. package/script/src/introspect.d.ts +36 -0
  84. package/script/src/introspect.d.ts.map +1 -0
  85. package/script/src/introspect.js +31 -0
  86. package/script/src/modules/async.d.ts +27 -0
  87. package/script/src/modules/async.d.ts.map +1 -0
  88. package/script/src/modules/async.js +41 -0
  89. package/script/src/modules/http.branded.d.ts +42 -0
  90. package/script/src/modules/http.branded.d.ts.map +1 -0
  91. package/script/src/modules/http.branded.js +30 -0
  92. package/script/src/modules/http.d.ts +46 -0
  93. package/script/src/modules/http.d.ts.map +1 -0
  94. package/script/src/modules/http.js +62 -0
  95. package/script/src/modules/strings.branded.d.ts +185 -0
  96. package/script/src/modules/strings.branded.d.ts.map +1 -0
  97. package/script/src/modules/strings.branded.js +126 -0
  98. package/script/src/modules/strings.d.ts +109 -0
  99. package/script/src/modules/strings.d.ts.map +1 -0
  100. package/script/src/modules/strings.js +150 -0
  101. package/script/src/types.d.ts +318 -0
  102. package/script/src/types.d.ts.map +1 -0
  103. package/script/src/types.js +2 -0
  104. package/script/src/utilities.d.ts +95 -0
  105. package/script/src/utilities.d.ts.map +1 -0
  106. 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
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAC5E,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACT,MAAM,gBAAgB,CAAC;AAExB;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB,4EAA4E;IAC5E,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;IAC1C,sCAAsC;IACtC,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC,KAAK,EAAE,CAAC;IAC1C,+DAA+D;IAC/D,QAAQ,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC;IACxC,wCAAwC;IACxC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACjC;AAED,KAAK,OAAO,GAAG;IACb,qEAAqE;IACrE,GAAG,EAAE,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,GAAG,OAAO,EACtC,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,CAAC,EACJ,KAAK,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,EAC9B,YAAY,CAAC,EAAE,MAAM,KAClB,CAAC,IAAI;SAAG,EAAE,IAAI,CAAC,GAAG,CAAC;K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,CAAC,SAAS,IAAI,GAAG,SAAS,GAAG,KAAK,GAAG,IAAI,CAAC;AAExE,uGAAuG;AAEvG,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,GAAG;IAAE,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,GAAG,CAAA;CAAE,CAAC;AAExG,iFAAiF;AACjF,MAAM,MAAM,cAAc,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,kBAAkB,GAAG,cAAc,CAAA;CAAE,CAAC;AAEpF,+CAA+C;AAE/C,MAAM,MAAM,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 @@
1
+ export {};
@@ -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
+ {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../../src/src/utilities.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAElE;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,GAAG,OAAO,EAC5D,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,CAAC,EACJ,KAAK,C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AAC,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,196 @@
1
+ import { isUndefined } from "./guard.js";
2
+ import { hasContext } from "./introspect.js";
3
+ /**
4
+ * Utility to verify if a property exists in an object. Checks that
5
+ * k is a key in t. If a guard method is provided, it will also check
6
+ * that the value at k passes the guard.
7
+ * @param {object} t The object to check for property k.
8
+ * @param {string|number|Symbol} k The key for property k.
9
+ * @param {Function|undefined} guard The optional type guard to validate t[k].
10
+ * @param {Context|undefined} ctx Optional validation context for path tracking.
11
+ * Note: ctx is expected to already include the key in its path when passed.
12
+ * @param {string|undefined} errorMessage Optional custom error message to use instead of default.
13
+ * @returns {boolean}
14
+ */
15
+ export function hasProperty(t, k, guard, ctx, errorMessage) {
16
+ if (!(k in t)) {
17
+ if (ctx)
18
+ ctx.addIssue(errorMessage ?? `Missing required property: ${String(k)}`);
19
+ return false;
20
+ }
21
+ if (!guard)
22
+ return true;
23
+ const value = t[k];
24
+ // If custom errorMessage provided, use simple boolean check and add custom message on failure
25
+ if (errorMessage) {
26
+ if (!guard(value)) {
27
+ if (ctx) {
28
+ ctx.addIssue(errorMessage);
29
+ // Note: Returns true despite failure to avoid short-circuiting && chains in parsers,
30
+ // allowing collection of multiple property errors. This is safe because validate()
31
+ // checks ctx.issues and returns { issues } instead of { value } when errors exist.
32
+ return true;
33
+ }
34
+ return false;
35
+ }
36
+ return true;
37
+ }
38
+ // If context is provided, use context-aware validation if available on the guard
39
+ if (ctx && hasContext(guard)) {
40
+ guard._.context(value, ctx); // Run validation, errors accumulate in ctx
41
+ // Note: Returns true despite potential failure to avoid short-circuiting && chains,
42
+ // enabling multi-property error collection. Safe because validate() checks ctx.issues.
43
+ return true;
44
+ }
45
+ return guard(value);
46
+ }
47
+ /**
48
+ * Checks if an object does not have a specific property.
49
+ *
50
+ * This function determines whether the given property key `k` does not exist
51
+ * in the object `t`. It uses TypeScript's type guard feature to refine the type
52
+ * of the object, ensuring that the property `k` is absent or explicitly `undefined`.
53
+ *
54
+ * @template K - The type of the property key to check.
55
+ * @param t - The object to check for the absence of the property.
56
+ * @param k - The property key to verify.
57
+ * @param ctx - Optional validation context for path tracking.
58
+ * @param errorMessage - Optional custom error message to use instead of default.
59
+ * @returns A boolean indicating whether the property `k` does not exist in the object `t`.
60
+ */
61
+ export function doesNotHaveProperty(t, k, ctx, errorMessage) {
62
+ if (k in t) {
63
+ if (ctx)
64
+ ctx.addIssue(errorMessage ?? `Unexpected property: ${String(k)}`);
65
+ return false;
66
+ }
67
+ return true;
68
+ }
69
+ /**
70
+ * Checks if an object has an optional property that passes a type guard.
71
+ *
72
+ * This function verifies if a property is either:
73
+ * - Undefined (which is valid for optional properties)
74
+ * - Present and passes the specified type guard
75
+
76
+ * @param t - The object to check
77
+ * @param k - The property key to look for
78
+ * @param guard - Optional function that checks if the value is of type G
79
+ * @param ctx - Optional validation context for path tracking
80
+ * @param errorMessage - Optional custom error message to use instead of default
81
+ * @returns Type predicate indicating if the object has the optional property of type G
82
+ */
83
+ export function hasOptionalProperty(t, k, guard, ctx, errorMessage) {
84
+ if (isUndefined(t[k]))
85
+ return true;
86
+ return hasProperty(t, k, guard, ctx, errorMessage);
87
+ }
88
+ /**
89
+ * Utility to verify if a value is included in a tuple.
90
+ * @param {array} t The tuple to check for the presence of index i.
91
+ * @param {unknown} i The index to check.
92
+ * @param {Function} guard The type guard to validate t[i].
93
+ * @returns {boolean}
94
+ */
95
+ export function tupleHas(t, i, guard) {
96
+ return (i in t) && guard(t[i]);
97
+ }
98
+ /**
99
+ * Determines whether the specified value `i` is included in the array `t`.
100
+ * Useful for checking if a value is part of a union type represented by a tuple.
101
+ * @typeParam T - A readonly array of unknown elements.
102
+ * @param t - The array to search within.
103
+ * @param v - The value to search for in the array.
104
+ * @returns `true` if `v` is found in `t`, otherwise `false`.
105
+ */
106
+ export function includes(t, v) {
107
+ return t.includes(v);
108
+ }
109
+ /**
110
+ * Determines if a given property key exists as a key in the specified object.
111
+ *
112
+ * @template T - The type of the object to check against.
113
+ * @param k - The property key to check.
114
+ * @param t - The object to check the property key against.
115
+ * @returns A boolean indicating whether the property key exists in the object.
116
+ */
117
+ export function keyOf(k, t, ctx, errorMessage) {
118
+ if (typeof k !== "string" && typeof k !== "number" && typeof k !== "symbol") {
119
+ if (ctx)
120
+ ctx.addIssue(errorMessage ?? `Invalid key type: ${typeof k}`);
121
+ return false;
122
+ }
123
+ if (!(k in t)) {
124
+ if (ctx)
125
+ ctx.addIssue(errorMessage ?? `Key "${String(k)}" not found in object`);
126
+ return false;
127
+ }
128
+ return true;
129
+ }
130
+ /**
131
+ * Safely stringifies a value for error messages, handling edge cases that JSON.stringify cannot.
132
+ */
133
+ function safeStringify(value) {
134
+ if (value === null)
135
+ return "null";
136
+ if (value === undefined)
137
+ return "undefined";
138
+ const type = typeof value;
139
+ if (type === "string")
140
+ return `'${value}'`;
141
+ if (type === "boolean")
142
+ return String(value);
143
+ if (type === "symbol")
144
+ return value.toString();
145
+ if (type === "function") {
146
+ const fn = value;
147
+ return `[Function${fn.name ? `: ${fn.name}` : ""}]`;
148
+ }
149
+ if (type === "bigint")
150
+ return `${String(value)}n`;
151
+ if (type === "number") {
152
+ if (Number.isNaN(value))
153
+ return "NaN";
154
+ if (!Number.isFinite(value))
155
+ return value === Infinity ? "Infinity" : "-Infinity";
156
+ return String(value);
157
+ }
158
+ // Objects and arrays - wrap in try/catch for circular references
159
+ try {
160
+ return JSON.stringify(value, (_key, val) => (typeof val === "bigint" ? `${String(val)}n` : val));
161
+ }
162
+ catch {
163
+ return Object.prototype.toString.call(value);
164
+ }
165
+ }
166
+ /**
167
+ * Formats an error message based on the provided value and optional metadata.
168
+ *
169
+ * @param value - The value that caused the error.
170
+ * @param name - An optional name of the expected type or condition.
171
+ * @returns A formatted error message string indicating the expected type (if provided) and the received value.
172
+ */
173
+ export function formatErrorMessage(value, name) {
174
+ const received = safeStringify(value);
175
+ if (name) {
176
+ return `Expected ${name}. Received: ${received}`;
177
+ }
178
+ return `Invalid value. Received: ${received}`;
179
+ }
180
+ /**
181
+ * Creates a union type guard from a list of type guards.
182
+ *
183
+ * @typeParam G - A tuple of `TypeGuard<unknown>` types (must have at least one guard).
184
+ * @param guards - The type guards to combine into a union.
185
+ * @returns A type guard that accepts any value accepted by at least one of the provided guards.
186
+ */
187
+ export const unionOf = (...guards) => {
188
+ if (guards.length === 0) {
189
+ throw new Error("unionOf requires at least one type guard");
190
+ }
191
+ let store = guards[0];
192
+ for (let i = 1; i < guards.length; i++) {
193
+ store = store.or(guards[i]);
194
+ }
195
+ return store;
196
+ };