@ptolemy2002/regex-utils 4.0.0 → 4.2.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 CHANGED
@@ -33,6 +33,16 @@ type ZodValidateWithErrorsOptions = {
33
33
  joinSeparator?: string;
34
34
  pathSeparator?: string;
35
35
  };
36
+
37
+ type ArrayWithOptional<AR extends unknown[], AO extends unknown[]> = AR | [...AR, ...AO];
38
+ type ZodGenericFactory<ZTA extends Array<ZodType<unknown>>=[ZodType], ZT extends ZodType=ZodType> =
39
+ <ZTAO extends ZTA>(...genericArgs: ZTAO) => (
40
+ <ZTO extends ZT>(
41
+ fn: (...args: ZTAO) => ZTO
42
+ ) => ZTO
43
+ )
44
+ ;
45
+ type MaybeZodOptional<ZT extends ZodType> = ZT | ZodOptional<ZT>;
36
46
  ```
37
47
 
38
48
  ## Classes
@@ -259,6 +269,58 @@ Uses `zodValidate` to check if a string is a valid Social Security Number (SSN).
259
269
  #### Returns
260
270
  `boolean` - `true` if the string is a valid SSN, `false` otherwise.
261
271
 
272
+ ### zodGenericFactory<ZTA extends Array<ZodType<unknown>=[ZodType]>, ZT extends ZodType=ZodType>
273
+
274
+ #### Description
275
+ Creates a factory for building generic Zod schemas with reusable type parameters. This is useful when you want to create schema templates where certain types (like ID or name fields) can be customized while maintaining type safety.
276
+
277
+ #### Type Parameters
278
+ * `ZTA` - An array of ZodType schemas that will serve as the generic parameters
279
+ * `ZT` - (Optional) The base type constraint for the resulting schema. Defaults to `ZodType`.
280
+
281
+ #### Returns
282
+ `ZodGenericFactory<ZTA, ZT>` - A factory function that:
283
+ 1. Takes the actual Zod schemas matching `ZTA`
284
+ 2. Returns a builder function that accepts an implementation function.
285
+ 3. The implementation receives the schemas as typed arguments and returns the final schema
286
+
287
+ #### Example
288
+ ```typescript
289
+ // Create a factory for object schemas with customizable id and name types
290
+ const genericFactory = zodGenericFactory<
291
+ // Here, the second type parameter is optional.
292
+ ArrayWithOptional<[ZodNumber | ZodUUID], [ZodString]>,
293
+ // An object schema with id and name fields of any type
294
+ ZodObject<{ id: ZodType; name: ZodType }>
295
+ >();
296
+
297
+ // Apply specific schemas
298
+ const template1 = genericFactory(z.number().min(0), z.string());
299
+
300
+ // Define the final schema structure
301
+ // Since the template provides the second parameter, it will be available in the function
302
+ const schema1 = template1((idSchema, nameSchema) => {
303
+ return z.object({
304
+ id: idSchema,
305
+ name: nameSchema
306
+ });
307
+ });
308
+
309
+ // Now `schema1` is typed as z.object({ id: ZodNumber, name: ZodString })
310
+
311
+ // We omit the second parameter, meaning the function will not have a nameSchema argument
312
+ // and detect that it can only be provided with `ZodUUID` for the idSchema
313
+ const template2 = genericFactory(z.uuid());
314
+ const schema2 = template2((idSchema) => {
315
+ return z.object({
316
+ id: idSchema,
317
+ name: z.string()
318
+ });
319
+ });
320
+
321
+ // Now `schema2` is typed as z.object({ id: ZodUUID, name: ZodString })
322
+ ```
323
+
262
324
  ## Schemas
263
325
  The following schemas are available in the library:
264
326
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { z, ZodError } from "zod";
1
+ import { z, ZodError, ZodOptional, ZodType } from "zod";
2
2
  export type RegexInput = string | RegExp;
3
3
  export declare function combineFlags(...flags: string[]): string;
4
4
  export declare function escapeRegex(value: RegexInput, flags?: string): RegExp;
@@ -74,3 +74,7 @@ export declare const ZodCoercedBoolean: z.ZodPipe<z.ZodEnum<{
74
74
  on: "on";
75
75
  off: "off";
76
76
  }>, z.ZodTransform<boolean, "0" | "1" | "true" | "false" | "t" | "f" | "yes" | "no" | "y" | "n" | "on" | "off">>;
77
+ export type ArrayWithOptional<AR extends unknown[], AO extends unknown[]> = AR | [...AR, ...AO];
78
+ export type ZodGenericFactory<ZTA extends Array<ZodType<unknown>> = [ZodType], ZT extends ZodType = ZodType> = <ZTAO extends ZTA>(...genericArgs: ZTAO) => (<ZTO extends ZT>(fn: (...args: ZTAO) => ZTO) => ZTO);
79
+ export declare function zodGenericFactory<ZTA extends Array<ZodType> = [ZodType], ZT extends ZodType = ZodType>(): ZodGenericFactory<ZTA, ZT>;
80
+ export type MaybeZodOptional<ZT extends ZodType> = ZT | ZodOptional<ZT>;
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ exports.isValidEmail = isValidEmail;
20
20
  exports.isValidURL = isValidURL;
21
21
  exports.isValidPhoneNumber = isValidPhoneNumber;
22
22
  exports.isValidSSN = isValidSSN;
23
+ exports.zodGenericFactory = zodGenericFactory;
23
24
  const zod_1 = require("zod");
24
25
  const accentPatterns = [
25
26
  "(a|á|à|ä|â|ã)", "(A|Á|À|Ä|Â|Ã)",
@@ -215,3 +216,6 @@ exports.ZodCoercedBoolean = exports.ZodCoercedBooleanEnum.transform((val) => {
215
216
  return false;
216
217
  }
217
218
  });
219
+ function zodGenericFactory() {
220
+ return (...genericArgs) => (fn) => fn(...genericArgs);
221
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "4.0.0",
3
+ "version": "4.2.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",