@jakubmazanec/zod-utils 2.0.0-unstable.e1ae458 → 2.0.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/build/main.js +4 -0
- package/build/main.js.map +7 -0
- package/build/types/Json.js +7 -0
- package/build/types/Json.js.map +7 -0
- package/build/types/PackageJson.js +236 -0
- package/build/types/PackageJson.js.map +7 -0
- package/build/types/TsconfigJson.js +320 -0
- package/build/types/TsconfigJson.js.map +7 -0
- package/build/types.js +5 -0
- package/build/types.js.map +7 -0
- package/build/utils/is.js +5 -0
- package/build/utils/is.js.map +7 -0
- package/build/utils/isPackageJson.js +6 -0
- package/build/utils/isPackageJson.js.map +7 -0
- package/build/utils/isTsconfigJson.js +6 -0
- package/build/utils/isTsconfigJson.js.map +7 -0
- package/{dist → build}/utils/readFile.d.ts +1 -1
- package/build/utils/readFile.js +12 -0
- package/build/utils/readFile.js.map +7 -0
- package/{dist → build}/utils/readJson.d.ts +1 -1
- package/build/utils/readJson.js +6 -0
- package/build/utils/readJson.js.map +7 -0
- package/build/utils/readPackageJson.js +7 -0
- package/build/utils/readPackageJson.js.map +7 -0
- package/build/utils/zodIssuesToString.js +32 -0
- package/build/utils/zodIssuesToString.js.map +7 -0
- package/build/utils.js +9 -0
- package/build/utils.js.map +7 -0
- package/package.json +23 -12
- package/{src → source}/utils/readFile.ts +3 -4
- package/{src → source}/utils/readJson.ts +3 -4
- package/{src → source}/utils/readPackageJson.ts +2 -3
- package/{src → source}/utils/zodIssuesToString.ts +6 -6
- package/dist/main.js +0 -4
- package/dist/main.js.map +0 -1
- package/dist/types/Json.js +0 -14
- package/dist/types/Json.js.map +0 -1
- package/dist/types/PackageJson.js +0 -292
- package/dist/types/PackageJson.js.map +0 -1
- package/dist/types/TsconfigJson.js +0 -338
- package/dist/types/TsconfigJson.js.map +0 -1
- package/dist/types.js +0 -5
- package/dist/types.js.map +0 -1
- package/dist/utils/is.js +0 -5
- package/dist/utils/is.js.map +0 -1
- package/dist/utils/isPackageJson.js +0 -6
- package/dist/utils/isPackageJson.js.map +0 -1
- package/dist/utils/isTsconfigJson.js +0 -6
- package/dist/utils/isTsconfigJson.js.map +0 -1
- package/dist/utils/readFile.js +0 -16
- package/dist/utils/readFile.js.map +0 -1
- package/dist/utils/readJson.js +0 -7
- package/dist/utils/readJson.js.map +0 -1
- package/dist/utils/readPackageJson.js +0 -7
- package/dist/utils/readPackageJson.js.map +0 -1
- package/dist/utils/zodIssuesToString.js +0 -30
- package/dist/utils/zodIssuesToString.js.map +0 -1
- package/dist/utils.js +0 -9
- package/dist/utils.js.map +0 -1
- /package/{dist → build}/main.d.ts +0 -0
- /package/{dist → build}/types/Json.d.ts +0 -0
- /package/{dist → build}/types/PackageJson.d.ts +0 -0
- /package/{dist → build}/types/TsconfigJson.d.ts +0 -0
- /package/{dist → build}/types.d.ts +0 -0
- /package/{dist → build}/utils/is.d.ts +0 -0
- /package/{dist → build}/utils/isPackageJson.d.ts +0 -0
- /package/{dist → build}/utils/isTsconfigJson.d.ts +0 -0
- /package/{dist → build}/utils/readPackageJson.d.ts +0 -0
- /package/{dist → build}/utils/zodIssuesToString.d.ts +0 -0
- /package/{dist → build}/utils.d.ts +0 -0
- /package/{src → source}/main.ts +0 -0
- /package/{src → source}/types/Json.ts +0 -0
- /package/{src → source}/types/PackageJson.ts +0 -0
- /package/{src → source}/types/TsconfigJson.ts +0 -0
- /package/{src → source}/types.ts +0 -0
- /package/{src → source}/utils/is.ts +0 -0
- /package/{src → source}/utils/isPackageJson.ts +0 -0
- /package/{src → source}/utils/isTsconfigJson.ts +0 -0
- /package/{src → source}/utils.ts +0 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../source/utils/is.ts"],
|
|
4
|
+
"sourcesContent": ["import {type z, type ZodTypeAny} from 'zod';\n\nexport function is<S extends ZodTypeAny>(value: unknown, schema: S): value is z.infer<S> {\n return schema.safeParse(value).success;\n}\n"],
|
|
5
|
+
"mappings": ";AAEO,gBAAS,GAAyB,OAAgB,QAAgC;AACvF,SAAO,OAAO,UAAU,KAAK,EAAE;AACjC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../source/utils/isPackageJson.ts"],
|
|
4
|
+
"sourcesContent": ["import {type PackageJson, packageJsonSchema} from '../types.js';\n\nexport function isPackageJson(value: unknown): value is PackageJson {\n return packageJsonSchema.safeParse(value).success;\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAA0B,yBAAwB;AAE3C,gBAAS,cAAc,OAAsC;AAClE,SAAO,kBAAkB,UAAU,KAAK,EAAE;AAC5C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../source/utils/isTsconfigJson.ts"],
|
|
4
|
+
"sourcesContent": ["import {type TsconfigJson, tsconfigJsonSchema} from '../types.js';\n\nexport function isTsconfigJson(value: unknown): value is TsconfigJson {\n return tsconfigJsonSchema.safeParse(value).success;\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAA2B,0BAAyB;AAE7C,gBAAS,eAAe,OAAuC;AACpE,SAAO,mBAAmB,UAAU,KAAK,EAAE;AAC7C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { type If } from '@jakubmazanec/ts-utils';
|
|
4
4
|
import { type PathLike } from 'node:fs';
|
|
5
5
|
import fs from 'node:fs/promises';
|
|
6
|
-
import { type z, type
|
|
6
|
+
import { type z, type ZodError, type ZodTypeAny } from 'zod';
|
|
7
7
|
export interface ReadFileOptions {
|
|
8
8
|
parser?: (rawString: string) => unknown;
|
|
9
9
|
throwOnZodError?: boolean;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
export async function readFile(path, schema, options) {
|
|
4
|
+
let fileParser = options?.parser ?? JSON.parse;
|
|
5
|
+
let rawJson = fileParser(await fs.readFile(path, { encoding: "utf8" }));
|
|
6
|
+
if (options?.throwOnZodError) {
|
|
7
|
+
return schema.parse(rawJson);
|
|
8
|
+
}
|
|
9
|
+
let result = schema.safeParse(rawJson);
|
|
10
|
+
return result.success ? result.data : result.error;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=readFile.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../source/utils/readFile.ts"],
|
|
4
|
+
"sourcesContent": ["import {type If} from '@jakubmazanec/ts-utils';\nimport {type PathLike} from 'node:fs';\nimport fs from 'node:fs/promises';\nimport {type z, type ZodError, type ZodTypeAny} from 'zod';\n\nexport interface ReadFileOptions {\n parser?: (rawString: string) => unknown;\n throwOnZodError?: boolean;\n}\n\nexport async function readFile<\n S extends ZodTypeAny,\n O extends ReadFileOptions | undefined = undefined,\n>(\n path: fs.FileHandle | PathLike,\n schema: S,\n options?: O,\n): Promise<\n O extends undefined ? z.infer<S> | ZodError<z.infer<S>>\n : If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>\n>;\nexport async function readFile(\n path: fs.FileHandle | PathLike,\n schema: ZodTypeAny,\n options?: ReadFileOptions,\n) {\n let fileParser = options?.parser ?? JSON.parse;\n let rawJson = fileParser(await fs.readFile(path, {encoding: 'utf8'})) as unknown;\n\n if (options?.throwOnZodError) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above\n return schema.parse(rawJson);\n }\n\n let result = schema.safeParse(rawJson);\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above\n return result.success ? result.data : result.error;\n}\n"],
|
|
5
|
+
"mappings": ";AAEA,OAAO,QAAQ;AAmBf,sBAAsB,SACpB,MACA,QACA,SACA;AACA,MAAI,aAAa,SAAS,UAAU,KAAK;AACzC,MAAI,UAAU,WAAW,MAAM,GAAG,SAAS,MAAM,EAAC,UAAU,OAAM,CAAC,CAAC;AAEpE,MAAI,SAAS,iBAAiB;AAE5B,WAAO,OAAO,MAAM,OAAO;AAAA,EAC7B;AAEA,MAAI,SAAS,OAAO,UAAU,OAAO;AAGrC,SAAO,OAAO,UAAU,OAAO,OAAO,OAAO;AAC/C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { type If } from '@jakubmazanec/ts-utils';
|
|
4
4
|
import { type PathLike } from 'node:fs';
|
|
5
5
|
import type fs from 'node:fs/promises';
|
|
6
|
-
import { type z, type
|
|
6
|
+
import { type z, type ZodError, type ZodTypeAny } from 'zod';
|
|
7
7
|
export interface ReadJsonOptions {
|
|
8
8
|
throwOnZodError?: boolean;
|
|
9
9
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../source/utils/readJson.ts"],
|
|
4
|
+
"sourcesContent": ["import {type If} from '@jakubmazanec/ts-utils';\nimport {type PathLike} from 'node:fs';\nimport type fs from 'node:fs/promises';\nimport {type z, type ZodError, type ZodTypeAny} from 'zod';\n\nimport {readFile} from './readFile.js';\n\nexport interface ReadJsonOptions {\n throwOnZodError?: boolean;\n}\n\nexport async function readJson<\n S extends ZodTypeAny,\n O extends ReadJsonOptions | undefined = undefined,\n>(\n path: fs.FileHandle | PathLike,\n schema: S,\n options?: O,\n): Promise<\n O extends undefined ? z.infer<S> | ZodError<z.infer<S>>\n : If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>\n>;\nexport async function readJson(\n path: fs.FileHandle | PathLike,\n schema: ZodTypeAny,\n options?: ReadJsonOptions,\n) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above\n return readFile(path, schema, options);\n}\n"],
|
|
5
|
+
"mappings": ";AAKA,SAAQ,gBAAe;AAiBvB,sBAAsB,SACpB,MACA,QACA,SACA;AAEA,SAAO,SAAS,MAAM,QAAQ,OAAO;AACvC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { packageJsonSchema } from "../types.js";
|
|
3
|
+
import { readFile } from "./readFile.js";
|
|
4
|
+
export async function readPackageJson(path, options) {
|
|
5
|
+
return readFile(path, packageJsonSchema, options);
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=readPackageJson.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../source/utils/readPackageJson.ts"],
|
|
4
|
+
"sourcesContent": ["import {type If} from '@jakubmazanec/ts-utils';\nimport {type PathLike} from 'node:fs';\nimport type fs from 'node:fs/promises';\nimport {type ZodError} from 'zod';\n\nimport {type PackageJson, packageJsonSchema} from '../types.js';\nimport {readFile} from './readFile.js';\n\nexport interface ReadPackageJsonOptions {\n throwOnZodError?: boolean;\n}\n\nexport async function readPackageJson<O extends ReadPackageJsonOptions | undefined = undefined>(\n path: fs.FileHandle | PathLike,\n options?: O,\n): Promise<\n O extends undefined ? PackageJson | ZodError<PackageJson>\n : If<NonNullable<O>['throwOnZodError'], true, PackageJson, PackageJson | ZodError<PackageJson>>\n>;\nexport async function readPackageJson(\n path: fs.FileHandle | PathLike,\n options?: ReadPackageJsonOptions,\n) {\n return readFile(path, packageJsonSchema, options);\n}\n"],
|
|
5
|
+
"mappings": ";AAKA,SAA0B,yBAAwB;AAClD,SAAQ,gBAAe;AAavB,sBAAsB,gBACpB,MACA,SACA;AACA,SAAO,SAAS,MAAM,mBAAmB,OAAO;AAClD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
function zodIssuePathToText(zodIssuePath) {
|
|
4
|
+
return zodIssuePath.length > 0 ? ` at path "${zodIssuePath.join(".")}"` : "";
|
|
5
|
+
}
|
|
6
|
+
function zodIssueToString(zodIssue) {
|
|
7
|
+
if (zodIssue.code === z.ZodIssueCode.invalid_type) {
|
|
8
|
+
return `Expected "${zodIssue.expected}" but received "${zodIssue.received}"${zodIssuePathToText(
|
|
9
|
+
zodIssue.path
|
|
10
|
+
)}`;
|
|
11
|
+
}
|
|
12
|
+
if (zodIssue.code === z.ZodIssueCode.unrecognized_keys) {
|
|
13
|
+
return `Unrecognized ${zodIssue.keys.length >= 2 ? "keys" : "key"} ${zodIssue.keys.map((key) => `"${key}"`).join(", ")}${zodIssuePathToText(zodIssue.path)}`;
|
|
14
|
+
}
|
|
15
|
+
if (zodIssue.code === z.ZodIssueCode.invalid_union) {
|
|
16
|
+
return `Invalid union: ${zodIssue.unionErrors.map(
|
|
17
|
+
(unionError) => unionError.issues.map((issue) => {
|
|
18
|
+
let zodIssueString = [...zodIssueToString(issue)];
|
|
19
|
+
zodIssueString[0] &&= zodIssueString[0].toLowerCase();
|
|
20
|
+
return zodIssueString.join("");
|
|
21
|
+
}).join(", ")
|
|
22
|
+
).join("; ")}`;
|
|
23
|
+
}
|
|
24
|
+
if (zodIssue.code === z.ZodIssueCode.invalid_enum_value) {
|
|
25
|
+
return `Expected ${zodIssue.options.length === 1 ? `"${zodIssue.options[0]}"` : `${zodIssue.options.map((option) => `"${option}"`).slice(0, -1).join(", ")} or "${zodIssue.options.at(-1)}"`}${zodIssuePathToText(zodIssue.path)}`;
|
|
26
|
+
}
|
|
27
|
+
return `${zodIssue.message}${zodIssuePathToText(zodIssue.path)}`;
|
|
28
|
+
}
|
|
29
|
+
export function zodIssuesToStrings(zodIssues) {
|
|
30
|
+
return zodIssues.map((zodIssue) => zodIssueToString(zodIssue));
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=zodIssuesToString.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../source/utils/zodIssuesToString.ts"],
|
|
4
|
+
"sourcesContent": ["import * as z from 'zod';\n\nfunction zodIssuePathToText(zodIssuePath: Array<number | string>): string {\n return zodIssuePath.length > 0 ? ` at path \"${zodIssuePath.join('.')}\"` : '';\n}\n\nfunction zodIssueToString(zodIssue: z.ZodIssue): string {\n if (zodIssue.code === z.ZodIssueCode.invalid_type) {\n return `Expected \"${zodIssue.expected}\" but received \"${zodIssue.received}\"${zodIssuePathToText(\n zodIssue.path,\n )}`;\n }\n\n if (zodIssue.code === z.ZodIssueCode.unrecognized_keys) {\n return `Unrecognized ${zodIssue.keys.length >= 2 ? 'keys' : 'key'} ${zodIssue.keys\n .map((key) => `\"${key}\"`)\n .join(', ')}${zodIssuePathToText(zodIssue.path)}`;\n }\n\n if (zodIssue.code === z.ZodIssueCode.invalid_union) {\n return `Invalid union: ${zodIssue.unionErrors\n .map((unionError) =>\n unionError.issues\n .map((issue) => {\n let zodIssueString = [...zodIssueToString(issue)];\n\n zodIssueString[0] &&= zodIssueString[0].toLowerCase();\n\n return zodIssueString.join('');\n })\n .join(', '),\n )\n .join('; ')}`;\n }\n\n if (zodIssue.code === z.ZodIssueCode.invalid_enum_value) {\n return `Expected ${\n zodIssue.options.length === 1 ?\n `\"${zodIssue.options[0]}\"`\n : `${zodIssue.options\n .map((option) => `\"${option}\"`)\n .slice(0, -1)\n .join(', ')} or \"${zodIssue.options.at(-1)}\"`\n }${zodIssuePathToText(zodIssue.path)}`;\n }\n\n return `${zodIssue.message}${zodIssuePathToText(zodIssue.path)}`;\n}\n\nexport function zodIssuesToStrings(zodIssues: z.ZodIssue[]): string[] {\n return zodIssues.map((zodIssue) => zodIssueToString(zodIssue));\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,YAAY,OAAO;AAEnB,SAAS,mBAAmB,cAA8C;AACxE,SAAO,aAAa,SAAS,IAAI,aAAa,aAAa,KAAK,GAAG,CAAC,MAAM;AAC5E;AAEA,SAAS,iBAAiB,UAA8B;AACtD,MAAI,SAAS,SAAS,EAAE,aAAa,cAAc;AACjD,WAAO,aAAa,SAAS,QAAQ,mBAAmB,SAAS,QAAQ,IAAI;AAAA,MAC3E,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AAEA,MAAI,SAAS,SAAS,EAAE,aAAa,mBAAmB;AACtD,WAAO,gBAAgB,SAAS,KAAK,UAAU,IAAI,SAAS,KAAK,IAAI,SAAS,KAC3E,IAAI,CAAC,QAAQ,IAAI,GAAG,GAAG,EACvB,KAAK,IAAI,CAAC,GAAG,mBAAmB,SAAS,IAAI,CAAC;AAAA,EACnD;AAEA,MAAI,SAAS,SAAS,EAAE,aAAa,eAAe;AAClD,WAAO,kBAAkB,SAAS,YAC/B;AAAA,MAAI,CAAC,eACJ,WAAW,OACR,IAAI,CAAC,UAAU;AACd,YAAI,iBAAiB,CAAC,GAAG,iBAAiB,KAAK,CAAC;AAEhD,uBAAe,CAAC,MAAM,eAAe,CAAC,EAAE,YAAY;AAEpD,eAAO,eAAe,KAAK,EAAE;AAAA,MAC/B,CAAC,EACA,KAAK,IAAI;AAAA,IACd,EACC,KAAK,IAAI,CAAC;AAAA,EACf;AAEA,MAAI,SAAS,SAAS,EAAE,aAAa,oBAAoB;AACvD,WAAO,YACL,SAAS,QAAQ,WAAW,IAC1B,IAAI,SAAS,QAAQ,CAAC,CAAC,MACvB,GAAG,SAAS,QACT,IAAI,CAAC,WAAW,IAAI,MAAM,GAAG,EAC7B,MAAM,GAAG,EAAE,EACX,KAAK,IAAI,CAAC,QAAQ,SAAS,QAAQ,GAAG,EAAE,CAAC,GAChD,GAAG,mBAAmB,SAAS,IAAI,CAAC;AAAA,EACtC;AAEA,SAAO,GAAG,SAAS,OAAO,GAAG,mBAAmB,SAAS,IAAI,CAAC;AAChE;AAEO,gBAAS,mBAAmB,WAAmC;AACpE,SAAO,UAAU,IAAI,CAAC,aAAa,iBAAiB,QAAQ,CAAC;AAC/D;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/build/utils.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
export * from "./utils/is.js";
|
|
3
|
+
export * from "./utils/isPackageJson.js";
|
|
4
|
+
export * from "./utils/isTsconfigJson.js";
|
|
5
|
+
export * from "./utils/readFile.js";
|
|
6
|
+
export * from "./utils/readJson.js";
|
|
7
|
+
export * from "./utils/readPackageJson.js";
|
|
8
|
+
export * from "./utils/zodIssuesToString.js";
|
|
9
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../source/utils.ts"],
|
|
4
|
+
"sourcesContent": ["export * from './utils/is.js';\nexport * from './utils/isPackageJson.js';\nexport * from './utils/isTsconfigJson.js';\nexport * from './utils/readFile.js';\nexport * from './utils/readJson.js';\nexport * from './utils/readPackageJson.js';\nexport * from './utils/zodIssuesToString.js';\n"],
|
|
5
|
+
"mappings": ";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jakubmazanec/zod-utils",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Collection of Zod schemas and utilities.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -11,27 +11,38 @@
|
|
|
11
11
|
"author": "Jakub Mazanec <jakub@mazanec.dev>",
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"type": "module",
|
|
14
|
-
"exports": "./
|
|
14
|
+
"exports": "./build/main.js",
|
|
15
15
|
"files": [
|
|
16
|
-
"
|
|
17
|
-
"
|
|
16
|
+
"build",
|
|
17
|
+
"source"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"build": "
|
|
21
|
-
"clean": "del-cli coverage
|
|
22
|
-
"
|
|
23
|
-
"
|
|
20
|
+
"build": "del-cli build tsconfig.tsbuildinfo && esbuild \"source/**/*\" --platform=node --target=node20.8 --outdir=build --sourcemap && tsc",
|
|
21
|
+
"clean": "del-cli coverage build *.tsbuildinfo",
|
|
22
|
+
"develop": "concurrently \"esbuild \"source/**/*\" --platform=node --target=node20.8 --outdir=build --sourcemap --watch\" \"tsc --watch\"",
|
|
23
|
+
"document": "del-cli docs && typedoc --options typedoc.config.cjs",
|
|
24
|
+
"format": "prettier . --write --ignore-unknown",
|
|
24
25
|
"lint": "eslint .",
|
|
25
|
-
"test": "
|
|
26
|
+
"test": "del-cli coverage && vitest run --coverage --passWithNoTests",
|
|
26
27
|
"typecheck": "tsc --project tsconfig.typecheck.json"
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"@jakubmazanec/ts-utils": "3.0.0
|
|
30
|
+
"@jakubmazanec/ts-utils": "^3.0.0"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"@jest/globals": "^29.5.0",
|
|
33
33
|
"@types/node": "^20.8.5",
|
|
34
|
-
"
|
|
34
|
+
"@vitest/coverage-v8": "^1.2.2",
|
|
35
|
+
"concurrently": "^8.2.2",
|
|
36
|
+
"del-cli": "^5.1.0",
|
|
37
|
+
"esbuild": "^0.20.0",
|
|
38
|
+
"eslint": "^8.56.0",
|
|
39
|
+
"prettier": "^3.2.5",
|
|
40
|
+
"prettier-plugin-packagejson": "^2.4.10",
|
|
41
|
+
"typedoc": "^0.25.7",
|
|
42
|
+
"typedoc-plugin-markdown": "^3.17.1",
|
|
43
|
+
"typescript": "^5.3.3",
|
|
44
|
+
"vitest": "^1.2.2",
|
|
45
|
+
"zod": "^3.22.4"
|
|
35
46
|
},
|
|
36
47
|
"peerDependencies": {
|
|
37
48
|
"zod": "^3.19.1"
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {type If} from '@jakubmazanec/ts-utils';
|
|
2
2
|
import {type PathLike} from 'node:fs';
|
|
3
3
|
import fs from 'node:fs/promises';
|
|
4
|
-
import {type z, type
|
|
4
|
+
import {type z, type ZodError, type ZodTypeAny} from 'zod';
|
|
5
5
|
|
|
6
6
|
export interface ReadFileOptions {
|
|
7
7
|
parser?: (rawString: string) => unknown;
|
|
@@ -16,9 +16,8 @@ export async function readFile<
|
|
|
16
16
|
schema: S,
|
|
17
17
|
options?: O,
|
|
18
18
|
): Promise<
|
|
19
|
-
O extends undefined
|
|
20
|
-
|
|
21
|
-
: If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>
|
|
19
|
+
O extends undefined ? z.infer<S> | ZodError<z.infer<S>>
|
|
20
|
+
: If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>
|
|
22
21
|
>;
|
|
23
22
|
export async function readFile(
|
|
24
23
|
path: fs.FileHandle | PathLike,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {type If} from '@jakubmazanec/ts-utils';
|
|
2
2
|
import {type PathLike} from 'node:fs';
|
|
3
3
|
import type fs from 'node:fs/promises';
|
|
4
|
-
import {type z, type
|
|
4
|
+
import {type z, type ZodError, type ZodTypeAny} from 'zod';
|
|
5
5
|
|
|
6
6
|
import {readFile} from './readFile.js';
|
|
7
7
|
|
|
@@ -17,9 +17,8 @@ export async function readJson<
|
|
|
17
17
|
schema: S,
|
|
18
18
|
options?: O,
|
|
19
19
|
): Promise<
|
|
20
|
-
O extends undefined
|
|
21
|
-
|
|
22
|
-
: If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>
|
|
20
|
+
O extends undefined ? z.infer<S> | ZodError<z.infer<S>>
|
|
21
|
+
: If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>
|
|
23
22
|
>;
|
|
24
23
|
export async function readJson(
|
|
25
24
|
path: fs.FileHandle | PathLike,
|
|
@@ -14,9 +14,8 @@ export async function readPackageJson<O extends ReadPackageJsonOptions | undefin
|
|
|
14
14
|
path: fs.FileHandle | PathLike,
|
|
15
15
|
options?: O,
|
|
16
16
|
): Promise<
|
|
17
|
-
O extends undefined
|
|
18
|
-
|
|
19
|
-
: If<NonNullable<O>['throwOnZodError'], true, PackageJson, PackageJson | ZodError<PackageJson>>
|
|
17
|
+
O extends undefined ? PackageJson | ZodError<PackageJson>
|
|
18
|
+
: If<NonNullable<O>['throwOnZodError'], true, PackageJson, PackageJson | ZodError<PackageJson>>
|
|
20
19
|
>;
|
|
21
20
|
export async function readPackageJson(
|
|
22
21
|
path: fs.FileHandle | PathLike,
|
|
@@ -35,12 +35,12 @@ function zodIssueToString(zodIssue: z.ZodIssue): string {
|
|
|
35
35
|
|
|
36
36
|
if (zodIssue.code === z.ZodIssueCode.invalid_enum_value) {
|
|
37
37
|
return `Expected ${
|
|
38
|
-
zodIssue.options.length === 1
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
zodIssue.options.length === 1 ?
|
|
39
|
+
`"${zodIssue.options[0]}"`
|
|
40
|
+
: `${zodIssue.options
|
|
41
|
+
.map((option) => `"${option}"`)
|
|
42
|
+
.slice(0, -1)
|
|
43
|
+
.join(', ')} or "${zodIssue.options.at(-1)}"`
|
|
44
44
|
}${zodIssuePathToText(zodIssue.path)}`;
|
|
45
45
|
}
|
|
46
46
|
|
package/dist/main.js
DELETED
package/dist/main.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/main.ts"],"sourcesContent":["export * from './types.js';\nexport * from './utils.js';\n"],"names":[],"mappings":"AAAA,cAAc,aAAa;AAC3B,cAAc,aAAa"}
|
package/dist/types/Json.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import * as z from 'zod';
|
|
2
|
-
const literalSchema = z.union([
|
|
3
|
-
z.string(),
|
|
4
|
-
z.number(),
|
|
5
|
-
z.boolean(),
|
|
6
|
-
z.null()
|
|
7
|
-
]);
|
|
8
|
-
export const jsonSchema = z.lazy(()=>z.union([
|
|
9
|
-
literalSchema,
|
|
10
|
-
z.array(jsonSchema),
|
|
11
|
-
z.record(jsonSchema)
|
|
12
|
-
]));
|
|
13
|
-
|
|
14
|
-
//# sourceMappingURL=Json.js.map
|
package/dist/types/Json.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/types/Json.ts"],"sourcesContent":["import * as z from 'zod';\n\nconst literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);\n\ntype Literal = z.infer<typeof literalSchema>;\n\nexport const jsonSchema: z.ZodSchema<Json> = z.lazy(() =>\n z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]),\n);\n\nexport type Json = Json[] | Literal | {[key: string]: Json};\n"],"names":["z","literalSchema","union","string","number","boolean","null","jsonSchema","lazy","array","record"],"mappings":"AAAA,YAAYA,OAAO,MAAM;AAEzB,MAAMC,gBAAgBD,EAAEE,KAAK,CAAC;IAACF,EAAEG,MAAM;IAAIH,EAAEI,MAAM;IAAIJ,EAAEK,OAAO;IAAIL,EAAEM,IAAI;CAAG;AAI7E,OAAO,MAAMC,aAAgCP,EAAEQ,IAAI,CAAC,IAClDR,EAAEE,KAAK,CAAC;QAACD;QAAeD,EAAES,KAAK,CAACF;QAAaP,EAAEU,MAAM,CAACH;KAAY,GAClE"}
|