@jakubmazanec/zod-utils 1.0.0-9029e49

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.
Files changed (55) hide show
  1. package/LICENSE.md +840 -0
  2. package/README.md +38 -0
  3. package/dist/main.d.ts +2 -0
  4. package/dist/main.js +4 -0
  5. package/dist/main.js.map +1 -0
  6. package/dist/types/Json.d.ts +8 -0
  7. package/dist/types/Json.js +14 -0
  8. package/dist/types/Json.js.map +1 -0
  9. package/dist/types/PackageJson.d.ts +517 -0
  10. package/dist/types/PackageJson.js +292 -0
  11. package/dist/types/PackageJson.js.map +1 -0
  12. package/dist/types/TsconfigJson.d.ts +609 -0
  13. package/dist/types/TsconfigJson.js +338 -0
  14. package/dist/types/TsconfigJson.js.map +1 -0
  15. package/dist/types.d.ts +3 -0
  16. package/dist/types.js +5 -0
  17. package/dist/types.js.map +1 -0
  18. package/dist/utils/is.d.ts +2 -0
  19. package/dist/utils/is.js +5 -0
  20. package/dist/utils/is.js.map +1 -0
  21. package/dist/utils/isPackageJson.d.ts +2 -0
  22. package/dist/utils/isPackageJson.js +6 -0
  23. package/dist/utils/isPackageJson.js.map +1 -0
  24. package/dist/utils/isTsconfigJson.d.ts +2 -0
  25. package/dist/utils/isTsconfigJson.js +6 -0
  26. package/dist/utils/isTsconfigJson.js.map +1 -0
  27. package/dist/utils/readFile.d.ts +11 -0
  28. package/dist/utils/readFile.js +16 -0
  29. package/dist/utils/readFile.js.map +1 -0
  30. package/dist/utils/readJson.d.ts +10 -0
  31. package/dist/utils/readJson.js +7 -0
  32. package/dist/utils/readJson.js.map +1 -0
  33. package/dist/utils/readPackageJson.d.ts +11 -0
  34. package/dist/utils/readPackageJson.js +7 -0
  35. package/dist/utils/readPackageJson.js.map +1 -0
  36. package/dist/utils/zodIssuesToString.d.ts +2 -0
  37. package/dist/utils/zodIssuesToString.js +30 -0
  38. package/dist/utils/zodIssuesToString.js.map +1 -0
  39. package/dist/utils.d.ts +7 -0
  40. package/dist/utils.js +9 -0
  41. package/dist/utils.js.map +1 -0
  42. package/package.json +43 -0
  43. package/src/main.ts +2 -0
  44. package/src/types/Json.ts +11 -0
  45. package/src/types/PackageJson.ts +294 -0
  46. package/src/types/TsconfigJson.ts +346 -0
  47. package/src/types.ts +3 -0
  48. package/src/utils/is.ts +5 -0
  49. package/src/utils/isPackageJson.ts +5 -0
  50. package/src/utils/isTsconfigJson.ts +5 -0
  51. package/src/utils/readFile.ts +40 -0
  52. package/src/utils/readJson.ts +31 -0
  53. package/src/utils/readPackageJson.ts +26 -0
  54. package/src/utils/zodIssuesToString.ts +52 -0
  55. package/src/utils.ts +7 -0
@@ -0,0 +1,5 @@
1
+ import {type PackageJson, packageJsonSchema} from '../types.js';
2
+
3
+ export function isPackageJson(value: unknown): value is PackageJson {
4
+ return packageJsonSchema.safeParse(value).success;
5
+ }
@@ -0,0 +1,5 @@
1
+ import {type TsconfigJson, tsconfigJsonSchema} from '../types.js';
2
+
3
+ export function isTsconfigJson(value: unknown): value is TsconfigJson {
4
+ return tsconfigJsonSchema.safeParse(value).success;
5
+ }
@@ -0,0 +1,40 @@
1
+ import {type If} from '@jakubmazanec/ts-utils';
2
+ import {type PathLike} from 'node:fs';
3
+ import fs from 'node:fs/promises';
4
+ import {type z, type ZodTypeAny, type ZodError} from 'zod';
5
+
6
+ export interface ReadFileOptions {
7
+ parser?: (rawString: string) => unknown;
8
+ throwOnZodError?: boolean;
9
+ }
10
+
11
+ export async function readFile<
12
+ S extends ZodTypeAny,
13
+ O extends ReadFileOptions | undefined = undefined,
14
+ >(
15
+ path: fs.FileHandle | PathLike,
16
+ schema: S,
17
+ options?: O,
18
+ ): Promise<
19
+ O extends undefined
20
+ ? z.infer<S> | ZodError<z.infer<S>>
21
+ : If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>
22
+ >;
23
+ export async function readFile(
24
+ path: fs.FileHandle | PathLike,
25
+ schema: ZodTypeAny,
26
+ options?: ReadFileOptions,
27
+ ) {
28
+ let fileParser = options?.parser ?? JSON.parse;
29
+ let rawJson = fileParser(await fs.readFile(path, {encoding: 'utf8'})) as unknown;
30
+
31
+ if (options?.throwOnZodError) {
32
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above
33
+ return schema.parse(rawJson);
34
+ }
35
+
36
+ let result = schema.safeParse(rawJson);
37
+
38
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above
39
+ return result.success ? result.data : result.error;
40
+ }
@@ -0,0 +1,31 @@
1
+ import {type If} from '@jakubmazanec/ts-utils';
2
+ import {type PathLike} from 'node:fs';
3
+ import type fs from 'node:fs/promises';
4
+ import {type z, type ZodTypeAny, type ZodError} from 'zod';
5
+
6
+ import {readFile} from './readFile.js';
7
+
8
+ export interface ReadJsonOptions {
9
+ throwOnZodError?: boolean;
10
+ }
11
+
12
+ export async function readJson<
13
+ S extends ZodTypeAny,
14
+ O extends ReadJsonOptions | undefined = undefined,
15
+ >(
16
+ path: fs.FileHandle | PathLike,
17
+ schema: S,
18
+ options?: O,
19
+ ): Promise<
20
+ O extends undefined
21
+ ? z.infer<S> | ZodError<z.infer<S>>
22
+ : If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>
23
+ >;
24
+ export async function readJson(
25
+ path: fs.FileHandle | PathLike,
26
+ schema: ZodTypeAny,
27
+ options?: ReadJsonOptions,
28
+ ) {
29
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above
30
+ return readFile(path, schema, options);
31
+ }
@@ -0,0 +1,26 @@
1
+ import {type If} from '@jakubmazanec/ts-utils';
2
+ import {type PathLike} from 'node:fs';
3
+ import type fs from 'node:fs/promises';
4
+ import {type ZodError} from 'zod';
5
+
6
+ import {type PackageJson, packageJsonSchema} from '../types.js';
7
+ import {readFile} from './readFile.js';
8
+
9
+ export interface ReadPackageJsonOptions {
10
+ throwOnZodError?: boolean;
11
+ }
12
+
13
+ export async function readPackageJson<O extends ReadPackageJsonOptions | undefined = undefined>(
14
+ path: fs.FileHandle | PathLike,
15
+ options?: O,
16
+ ): Promise<
17
+ O extends undefined
18
+ ? PackageJson | ZodError<PackageJson>
19
+ : If<NonNullable<O>['throwOnZodError'], true, PackageJson, PackageJson | ZodError<PackageJson>>
20
+ >;
21
+ export async function readPackageJson(
22
+ path: fs.FileHandle | PathLike,
23
+ options?: ReadPackageJsonOptions,
24
+ ) {
25
+ return readFile(path, packageJsonSchema, options);
26
+ }
@@ -0,0 +1,52 @@
1
+ import * as z from 'zod';
2
+
3
+ function zodIssuePathToText(zodIssuePath: Array<number | string>): string {
4
+ return zodIssuePath.length > 0 ? ` at path "${zodIssuePath.join('.')}"` : '';
5
+ }
6
+
7
+ function zodIssueToString(zodIssue: z.ZodIssue): string {
8
+ if (zodIssue.code === z.ZodIssueCode.invalid_type) {
9
+ return `Expected "${zodIssue.expected}" but received "${zodIssue.received}"${zodIssuePathToText(
10
+ zodIssue.path,
11
+ )}`;
12
+ }
13
+
14
+ if (zodIssue.code === z.ZodIssueCode.unrecognized_keys) {
15
+ return `Unrecognized ${zodIssue.keys.length >= 2 ? 'keys' : 'key'} ${zodIssue.keys
16
+ .map((key) => `"${key}"`)
17
+ .join(', ')}${zodIssuePathToText(zodIssue.path)}`;
18
+ }
19
+
20
+ if (zodIssue.code === z.ZodIssueCode.invalid_union) {
21
+ return `Invalid union: ${zodIssue.unionErrors
22
+ .map((unionError) =>
23
+ unionError.issues
24
+ .map((issue) => {
25
+ let zodIssueString = [...zodIssueToString(issue)];
26
+
27
+ zodIssueString[0] &&= zodIssueString[0].toLowerCase();
28
+
29
+ return zodIssueString.join('');
30
+ })
31
+ .join(', '),
32
+ )
33
+ .join('; ')}`;
34
+ }
35
+
36
+ if (zodIssue.code === z.ZodIssueCode.invalid_enum_value) {
37
+ return `Expected ${
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
+ }${zodIssuePathToText(zodIssue.path)}`;
45
+ }
46
+
47
+ return `${zodIssue.message}${zodIssuePathToText(zodIssue.path)}`;
48
+ }
49
+
50
+ export function zodIssuesToStrings(zodIssues: z.ZodIssue[]): string[] {
51
+ return zodIssues.map((zodIssue) => zodIssueToString(zodIssue));
52
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './utils/is.js';
2
+ export * from './utils/isPackageJson.js';
3
+ export * from './utils/isTsconfigJson.js';
4
+ export * from './utils/readFile.js';
5
+ export * from './utils/readJson.js';
6
+ export * from './utils/readPackageJson.js';
7
+ export * from './utils/zodIssuesToString.js';