@ptolemy2002/regex-utils 3.0.0 → 4.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/README.md CHANGED
@@ -33,6 +33,9 @@ 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>>, ZT extends ZodType=ZodType> = <ZTAO extends ZTA>(...genericArgs: ZTAO) => <ZTO extends ZT>(fn: (...args: ZTAO) => ZTO) => ZTO;
36
39
  ```
37
40
 
38
41
  ## Classes
@@ -163,7 +166,7 @@ Checks if an unknown value is a ZodError. This is more reliable than using `inst
163
166
 
164
167
  ### interpretZodError
165
168
  #### Description
166
- Given a zod error, interprets it to `null` if no error is found, a single error message if there is a single error, or an array of error messages if there are multiple errors. The error messages will be in the format `<path>: <message>`. If the error is with function arguments, "arguments.<index>" will be appended to the end of the path. Similarly, if the error is with a function return value, "returnValue" will be appended to the end of the path. Note that for function arguments, only the first seen error will be reported.
169
+ Given a zod error, interprets it to `null` if no error is found, a single error message if there is a single error, or an array of error messages if there are multiple errors. The error messages will be in the format `<path>: <message>`.
167
170
 
168
171
  #### Parameters
169
172
  - `e` (`ZodError`): The zod error to be interpreted.
@@ -259,6 +262,58 @@ Uses `zodValidate` to check if a string is a valid Social Security Number (SSN).
259
262
  #### Returns
260
263
  `boolean` - `true` if the string is a valid SSN, `false` otherwise.
261
264
 
265
+ ### zodGenericFactory<ZTA extends Array<ZodType<unknown>>, ZT extends ZodType=ZodType>
266
+
267
+ #### Description
268
+ 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.
269
+
270
+ #### Type Parameters
271
+ * `ZTA` - An array of ZodType schemas that will serve as the generic parameters
272
+ * `ZT` - (Optional) The base type constraint for the resulting schema. Defaults to `ZodType`.
273
+
274
+ #### Returns
275
+ `ZodGenericFactory<ZTA, ZT>` - A factory function that:
276
+ 1. Takes the actual Zod schemas matching `ZTA`
277
+ 2. Returns a builder function that accepts an implementation function.
278
+ 3. The implementation receives the schemas as typed arguments and returns the final schema
279
+
280
+ #### Example
281
+ ```typescript
282
+ // Create a factory for object schemas with customizable id and name types
283
+ const genericFactory = zodGenericFactory<
284
+ // Here, the second type parameter is optional.
285
+ ArrayWithOptional<[ZodNumber | ZodUUID], [ZodString]>,
286
+ // An object schema with id and name fields of any type
287
+ ZodObject<{ id: ZodType; name: ZodType }>
288
+ >();
289
+
290
+ // Apply specific schemas
291
+ const template1 = genericFactory(z.number().min(0), z.string());
292
+
293
+ // Define the final schema structure
294
+ // Since the template provides the second parameter, it will be available in the function
295
+ const schema1 = template1((idSchema, nameSchema) => {
296
+ return z.object({
297
+ id: idSchema,
298
+ name: nameSchema
299
+ });
300
+ });
301
+
302
+ // Now `schema1` is typed as z.object({ id: ZodNumber, name: ZodString })
303
+
304
+ // We omit the second parameter, meaning the function will not have a nameSchema argument
305
+ // and detect that it can only be provided with `ZodUUID` for the idSchema
306
+ const template2 = genericFactory(z.uuid());
307
+ const schema2 = template2((idSchema) => {
308
+ return z.object({
309
+ id: idSchema,
310
+ name: z.string()
311
+ });
312
+ });
313
+
314
+ // Now `schema2` is typed as z.object({ id: ZodUUID, name: ZodString })
315
+ ```
316
+
262
317
  ## Schemas
263
318
  The following schemas are available in the library:
264
319
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { z, ZodError } from "zod";
1
+ import { z, ZodError, 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;
@@ -24,7 +24,7 @@ export type InterpretZodErrorOptions = {
24
24
  };
25
25
  export declare function interpretZodError(e: ZodError, { prefix, separator }?: InterpretZodErrorOptions): string | string[] | null;
26
26
  export type ZodSafeParseable<O> = {
27
- safeParse: (v: unknown) => z.SafeParseReturnType<unknown, O>;
27
+ safeParse: (v: unknown) => z.ZodSafeParseResult<O>;
28
28
  };
29
29
  export declare function zodValidate<O>(p: ZodSafeParseable<O>): ZodValidator<O>;
30
30
  export type ZodValidateWithErrorsOptions = {
@@ -74,3 +74,6 @@ 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>>, 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<unknown>>, ZT extends ZodType = ZodType>(): ZodGenericFactory<ZTA, 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|Á|À|Ä|Â|Ã)",
@@ -116,7 +117,7 @@ function isZodError(err) {
116
117
  return Boolean(err && (err instanceof zod_1.ZodError || err.name === 'ZodError'));
117
118
  }
118
119
  function interpretZodError(e, { prefix, separator = "." } = {}) {
119
- const { errors } = e;
120
+ const { issues } = e;
120
121
  function formatIssue(issue) {
121
122
  const { code, path: _path, message } = issue;
122
123
  let path = _path;
@@ -126,25 +127,15 @@ function interpretZodError(e, { prefix, separator = "." } = {}) {
126
127
  else if (Array.isArray(prefix)) {
127
128
  path = [...prefix, ...path];
128
129
  }
129
- if (code === "invalid_return_type") {
130
- const returnTypeIssue = issue.returnTypeError.errors[0];
131
- path = [...path, "returnType", ...returnTypeIssue.path];
132
- return `${path.join(separator)}: ${returnTypeIssue.message}`;
133
- }
134
- else if (code === "invalid_arguments") {
135
- const argumentsIssue = issue.argumentsError.errors[0];
136
- path = [...path, "arguments", ...argumentsIssue.path];
137
- return `${path.join(separator)}: ${argumentsIssue.message}`;
138
- }
139
130
  if (path.length === 0)
140
131
  return message;
141
132
  return `${path.join(separator)}: ${message}`;
142
133
  }
143
- if (errors.length === 0)
134
+ if (issues.length === 0)
144
135
  return null;
145
- if (errors.length === 1)
146
- return formatIssue(errors[0]);
147
- return errors.map((e) => formatIssue(e));
136
+ if (issues.length === 1)
137
+ return formatIssue(issues[0]);
138
+ return issues.map((e) => formatIssue(e));
148
139
  }
149
140
  function zodValidate(p) {
150
141
  return (v) => {
@@ -225,3 +216,6 @@ exports.ZodCoercedBoolean = exports.ZodCoercedBooleanEnum.transform((val) => {
225
216
  return false;
226
217
  }
227
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": "3.0.0",
3
+ "version": "4.1.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",