@alextheman/utility 4.6.0 → 4.8.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/dist/index.cjs +19 -9
- package/dist/index.d.cts +4 -6
- package/dist/index.d.ts +4 -6
- package/dist/index.js +19 -9
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -857,27 +857,37 @@ var parseBoolean_default = parseBoolean;
|
|
|
857
857
|
*
|
|
858
858
|
* @category Parsers
|
|
859
859
|
*
|
|
860
|
-
* @template
|
|
861
|
-
* @template Input - The Zod input type.
|
|
862
|
-
* @template Internals - The Zod internal types based on the output and input types.
|
|
860
|
+
* @template SchemaType - The Zod schema type.
|
|
863
861
|
* @template ErrorType - The type of error to throw on invalid data.
|
|
864
862
|
*
|
|
865
863
|
* @param schema - The Zod schema to use in parsing.
|
|
866
864
|
* @param data - The data to parse.
|
|
867
|
-
* @param
|
|
865
|
+
* @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
|
|
868
866
|
*
|
|
869
867
|
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
870
868
|
*
|
|
871
869
|
* @returns The parsed data from the Zod schema.
|
|
872
870
|
*/
|
|
873
|
-
function parseZodSchema(schema, data,
|
|
871
|
+
function parseZodSchema(schema, data, onError) {
|
|
874
872
|
const parsedResult = schema.safeParse(data);
|
|
875
873
|
if (!parsedResult.success) {
|
|
876
|
-
if (
|
|
877
|
-
if (
|
|
878
|
-
else if (typeof
|
|
874
|
+
if (onError) {
|
|
875
|
+
if (onError instanceof Error) throw onError;
|
|
876
|
+
else if (typeof onError === "function") {
|
|
877
|
+
const evaluatedError = onError(parsedResult.error);
|
|
878
|
+
if (evaluatedError instanceof Error) throw evaluatedError;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
const allErrorCodes = {};
|
|
882
|
+
for (const issue of parsedResult.error.issues) {
|
|
883
|
+
const code = issue.code.toUpperCase();
|
|
884
|
+
allErrorCodes[code] = (allErrorCodes[code] ?? 0) + 1;
|
|
879
885
|
}
|
|
880
|
-
throw new DataError_default(data,
|
|
886
|
+
throw new DataError_default(data, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
|
|
887
|
+
return secondCount - firstCount;
|
|
888
|
+
}).map(([code, count], _, allErrorCodes$1) => {
|
|
889
|
+
return allErrorCodes$1.length === 1 && count === 1 ? code : `${code}×${count}`;
|
|
890
|
+
}).join(","), `\n\n${zod.z.prettifyError(parsedResult.error)}\n`);
|
|
881
891
|
}
|
|
882
892
|
return parsedResult.data;
|
|
883
893
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DotenvParseOutput } from "dotenv";
|
|
2
|
-
import { ZodError, ZodType,
|
|
2
|
+
import { ZodError, ZodType, z } from "zod";
|
|
3
3
|
|
|
4
4
|
//#region src/constants/NAMESPACE_EXPORT_REGEX.d.ts
|
|
5
5
|
declare const NAMESPACE_EXPORT_REGEX = "export\\s+\\*\\s+from";
|
|
@@ -684,20 +684,18 @@ declare function parseVersionType(data: unknown): VersionType;
|
|
|
684
684
|
*
|
|
685
685
|
* @category Parsers
|
|
686
686
|
*
|
|
687
|
-
* @template
|
|
688
|
-
* @template Input - The Zod input type.
|
|
689
|
-
* @template Internals - The Zod internal types based on the output and input types.
|
|
687
|
+
* @template SchemaType - The Zod schema type.
|
|
690
688
|
* @template ErrorType - The type of error to throw on invalid data.
|
|
691
689
|
*
|
|
692
690
|
* @param schema - The Zod schema to use in parsing.
|
|
693
691
|
* @param data - The data to parse.
|
|
694
|
-
* @param
|
|
692
|
+
* @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
|
|
695
693
|
*
|
|
696
694
|
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
697
695
|
*
|
|
698
696
|
* @returns The parsed data from the Zod schema.
|
|
699
697
|
*/
|
|
700
|
-
declare function parseZodSchema<
|
|
698
|
+
declare function parseZodSchema<SchemaType extends ZodType, ErrorType extends Error>(schema: SchemaType, data: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): z.infer<SchemaType>;
|
|
701
699
|
//#endregion
|
|
702
700
|
//#region src/functions/recursive/deepCopy.d.ts
|
|
703
701
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ZodError, ZodType,
|
|
1
|
+
import { ZodError, ZodType, z as z$1 } from "zod";
|
|
2
2
|
import { DotenvParseOutput } from "dotenv";
|
|
3
3
|
|
|
4
4
|
//#region src/constants/NAMESPACE_EXPORT_REGEX.d.ts
|
|
@@ -684,20 +684,18 @@ declare function parseVersionType(data: unknown): VersionType;
|
|
|
684
684
|
*
|
|
685
685
|
* @category Parsers
|
|
686
686
|
*
|
|
687
|
-
* @template
|
|
688
|
-
* @template Input - The Zod input type.
|
|
689
|
-
* @template Internals - The Zod internal types based on the output and input types.
|
|
687
|
+
* @template SchemaType - The Zod schema type.
|
|
690
688
|
* @template ErrorType - The type of error to throw on invalid data.
|
|
691
689
|
*
|
|
692
690
|
* @param schema - The Zod schema to use in parsing.
|
|
693
691
|
* @param data - The data to parse.
|
|
694
|
-
* @param
|
|
692
|
+
* @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
|
|
695
693
|
*
|
|
696
694
|
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
697
695
|
*
|
|
698
696
|
* @returns The parsed data from the Zod schema.
|
|
699
697
|
*/
|
|
700
|
-
declare function parseZodSchema<
|
|
698
|
+
declare function parseZodSchema<SchemaType extends ZodType, ErrorType extends Error>(schema: SchemaType, data: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): z$1.infer<SchemaType>;
|
|
701
699
|
//#endregion
|
|
702
700
|
//#region src/functions/recursive/deepCopy.d.ts
|
|
703
701
|
/**
|
package/dist/index.js
CHANGED
|
@@ -827,27 +827,37 @@ var parseBoolean_default = parseBoolean;
|
|
|
827
827
|
*
|
|
828
828
|
* @category Parsers
|
|
829
829
|
*
|
|
830
|
-
* @template
|
|
831
|
-
* @template Input - The Zod input type.
|
|
832
|
-
* @template Internals - The Zod internal types based on the output and input types.
|
|
830
|
+
* @template SchemaType - The Zod schema type.
|
|
833
831
|
* @template ErrorType - The type of error to throw on invalid data.
|
|
834
832
|
*
|
|
835
833
|
* @param schema - The Zod schema to use in parsing.
|
|
836
834
|
* @param data - The data to parse.
|
|
837
|
-
* @param
|
|
835
|
+
* @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
|
|
838
836
|
*
|
|
839
837
|
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
840
838
|
*
|
|
841
839
|
* @returns The parsed data from the Zod schema.
|
|
842
840
|
*/
|
|
843
|
-
function parseZodSchema(schema, data,
|
|
841
|
+
function parseZodSchema(schema, data, onError) {
|
|
844
842
|
const parsedResult = schema.safeParse(data);
|
|
845
843
|
if (!parsedResult.success) {
|
|
846
|
-
if (
|
|
847
|
-
if (
|
|
848
|
-
else if (typeof
|
|
844
|
+
if (onError) {
|
|
845
|
+
if (onError instanceof Error) throw onError;
|
|
846
|
+
else if (typeof onError === "function") {
|
|
847
|
+
const evaluatedError = onError(parsedResult.error);
|
|
848
|
+
if (evaluatedError instanceof Error) throw evaluatedError;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
const allErrorCodes = {};
|
|
852
|
+
for (const issue of parsedResult.error.issues) {
|
|
853
|
+
const code = issue.code.toUpperCase();
|
|
854
|
+
allErrorCodes[code] = (allErrorCodes[code] ?? 0) + 1;
|
|
849
855
|
}
|
|
850
|
-
throw new DataError_default(data,
|
|
856
|
+
throw new DataError_default(data, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
|
|
857
|
+
return secondCount - firstCount;
|
|
858
|
+
}).map(([code, count], _, allErrorCodes$1) => {
|
|
859
|
+
return allErrorCodes$1.length === 1 && count === 1 ? code : `${code}×${count}`;
|
|
860
|
+
}).join(","), `\n\n${z$1.prettifyError(parsedResult.error)}\n`);
|
|
851
861
|
}
|
|
852
862
|
return parsedResult.data;
|
|
853
863
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alextheman/utility",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.8.0",
|
|
4
4
|
"description": "Helpful utility functions.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,20 +23,20 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@alextheman/eslint-plugin": "^5.4.2",
|
|
25
25
|
"@types/libsodium-wrappers": "^0.7.14",
|
|
26
|
-
"@types/node": "^25.0.
|
|
27
|
-
"@typescript-eslint/types": "^8.
|
|
28
|
-
"alex-c-line": "^1.
|
|
26
|
+
"@types/node": "^25.0.9",
|
|
27
|
+
"@typescript-eslint/types": "^8.53.0",
|
|
28
|
+
"alex-c-line": "^1.18.4",
|
|
29
29
|
"dotenv-cli": "^11.0.0",
|
|
30
30
|
"eslint": "^9.39.2",
|
|
31
31
|
"globals": "^17.0.0",
|
|
32
32
|
"husky": "^9.1.7",
|
|
33
33
|
"jsdom": "^27.4.0",
|
|
34
|
-
"prettier": "^3.
|
|
35
|
-
"tsdown": "^0.
|
|
36
|
-
"typedoc": "^0.28.
|
|
34
|
+
"prettier": "^3.8.0",
|
|
35
|
+
"tsdown": "^0.19.0",
|
|
36
|
+
"typedoc": "^0.28.16",
|
|
37
37
|
"typescript": "^5.9.3",
|
|
38
38
|
"vite-tsconfig-paths": "^6.0.4",
|
|
39
|
-
"vitest": "^4.0.
|
|
39
|
+
"vitest": "^4.0.17"
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
|
42
42
|
"node": ">=22.0.0"
|