@ptolemy2002/regex-utils 2.2.3 → 2.3.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
@@ -21,7 +21,7 @@ type TransformRegexOptions = {
21
21
  export type ZodValidator<O> = (v: O) => boolean;
22
22
  export type ZodValidatorWithErrors<O> = (v: O) => true | string | string[];
23
23
  export type ZodSafeParseable<O> = {
24
- safeParse: (v: unknown) => { success: boolean, data?: O, error?: ZodError };
24
+ safeParse: (v: unknown) => z.SafeParseReturnType<unknown, O>;
25
25
  };
26
26
  ```
27
27
 
@@ -154,6 +154,7 @@ This is a simple function that takes a zod schema, returning a function that tak
154
154
 
155
155
  #### Parameters
156
156
  - `p` (`ZodSafeParseable<O>`): The zod schema to be used for validation.
157
+ - `_throw` (`boolean`): Whether to throw an error if the value does not match the schema. If true, errors will be delimited by newlines. Default is `false`.
157
158
 
158
159
  #### Returns
159
160
  `ZodValidatorWithErrors<O>` - A function that takes a value and returns `true` if the value matches the schema, an error message if there is a single error, or an array of error messages if there are multiple errors.
package/dist/index.d.ts CHANGED
@@ -22,7 +22,7 @@ export type ZodSafeParseable<O> = {
22
22
  safeParse: (v: unknown) => z.SafeParseReturnType<unknown, O>;
23
23
  };
24
24
  export declare function zodValidate<O>(p: ZodSafeParseable<O>): ZodValidator<O>;
25
- export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>): ZodValidatorWithErrors<O>;
25
+ export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>, _throw?: boolean): ZodValidatorWithErrors<O>;
26
26
  export declare function isAlphanumeric(str: string): boolean;
27
27
  export declare function toAlphanumeric(str: string, separator?: string): string;
28
28
  export declare function isValidEmail(v: string): boolean;
package/dist/index.js CHANGED
@@ -131,12 +131,21 @@ function zodValidate(p) {
131
131
  return result.success;
132
132
  };
133
133
  }
134
- function zodValidateWithErrors(p) {
134
+ function zodValidateWithErrors(p, _throw = false) {
135
135
  return (v) => {
136
136
  const result = p.safeParse(v);
137
137
  if (result.success)
138
138
  return true;
139
- return interpretZodError(result.error);
139
+ const error = interpretZodError(result.error);
140
+ if (_throw) {
141
+ if (Array.isArray(error)) {
142
+ throw new Error(error.join("\n"));
143
+ }
144
+ else {
145
+ throw new Error(error);
146
+ }
147
+ }
148
+ return error;
140
149
  };
141
150
  }
142
151
  const alphanumericPattern = /[\w_-]+/i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "2.2.3",
3
+ "version": "2.3.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",