@ptolemy2002/regex-utils 4.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 +55 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -0
- package/package.json +1 -1
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
|
|
@@ -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;
|
|
@@ -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|Á|À|Ä|Â|Ã)",
|
|
@@ -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
|
+
}
|