@tsed/cli-mcp 7.5.0 → 7.5.1

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 (56) hide show
  1. package/lib/esm/utils/toZod.js +1 -1
  2. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  3. package/lib/types/fn/defineTool.d.ts +2 -2
  4. package/package.json +4 -3
  5. package/lib/esm/utils/json-schema-to-zod/Types.js +0 -1
  6. package/lib/esm/utils/json-schema-to-zod/cli.js +0 -74
  7. package/lib/esm/utils/json-schema-to-zod/index.js +0 -25
  8. package/lib/esm/utils/json-schema-to-zod/jsonSchemaToZod.js +0 -43
  9. package/lib/esm/utils/json-schema-to-zod/parsers/parseAllOf.js +0 -37
  10. package/lib/esm/utils/json-schema-to-zod/parsers/parseAnyOf.js +0 -11
  11. package/lib/esm/utils/json-schema-to-zod/parsers/parseArray.js +0 -16
  12. package/lib/esm/utils/json-schema-to-zod/parsers/parseBoolean.js +0 -3
  13. package/lib/esm/utils/json-schema-to-zod/parsers/parseConst.js +0 -3
  14. package/lib/esm/utils/json-schema-to-zod/parsers/parseDefault.js +0 -3
  15. package/lib/esm/utils/json-schema-to-zod/parsers/parseEnum.js +0 -15
  16. package/lib/esm/utils/json-schema-to-zod/parsers/parseIfThenElse.js +0 -20
  17. package/lib/esm/utils/json-schema-to-zod/parsers/parseMultipleType.js +0 -4
  18. package/lib/esm/utils/json-schema-to-zod/parsers/parseNot.js +0 -7
  19. package/lib/esm/utils/json-schema-to-zod/parsers/parseNull.js +0 -3
  20. package/lib/esm/utils/json-schema-to-zod/parsers/parseNullable.js +0 -8
  21. package/lib/esm/utils/json-schema-to-zod/parsers/parseNumber.js +0 -46
  22. package/lib/esm/utils/json-schema-to-zod/parsers/parseObject.js +0 -171
  23. package/lib/esm/utils/json-schema-to-zod/parsers/parseOneOf.js +0 -34
  24. package/lib/esm/utils/json-schema-to-zod/parsers/parseSchema.js +0 -138
  25. package/lib/esm/utils/json-schema-to-zod/parsers/parseString.js +0 -57
  26. package/lib/esm/utils/json-schema-to-zod/utils/cliTools.js +0 -99
  27. package/lib/esm/utils/json-schema-to-zod/utils/half.js +0 -3
  28. package/lib/esm/utils/json-schema-to-zod/utils/jsdocs.js +0 -12
  29. package/lib/esm/utils/json-schema-to-zod/utils/omit.js +0 -6
  30. package/lib/esm/utils/json-schema-to-zod/utils/withMessage.js +0 -19
  31. package/lib/types/utils/json-schema-to-zod/Types.d.ts +0 -68
  32. package/lib/types/utils/json-schema-to-zod/cli.d.ts +0 -2
  33. package/lib/types/utils/json-schema-to-zod/index.d.ts +0 -25
  34. package/lib/types/utils/json-schema-to-zod/jsonSchemaToZod.d.ts +0 -2
  35. package/lib/types/utils/json-schema-to-zod/parsers/parseAllOf.d.ts +0 -4
  36. package/lib/types/utils/json-schema-to-zod/parsers/parseAnyOf.d.ts +0 -4
  37. package/lib/types/utils/json-schema-to-zod/parsers/parseArray.d.ts +0 -4
  38. package/lib/types/utils/json-schema-to-zod/parsers/parseBoolean.d.ts +0 -4
  39. package/lib/types/utils/json-schema-to-zod/parsers/parseConst.d.ts +0 -4
  40. package/lib/types/utils/json-schema-to-zod/parsers/parseDefault.d.ts +0 -2
  41. package/lib/types/utils/json-schema-to-zod/parsers/parseEnum.d.ts +0 -4
  42. package/lib/types/utils/json-schema-to-zod/parsers/parseIfThenElse.d.ts +0 -6
  43. package/lib/types/utils/json-schema-to-zod/parsers/parseMultipleType.d.ts +0 -4
  44. package/lib/types/utils/json-schema-to-zod/parsers/parseNot.d.ts +0 -4
  45. package/lib/types/utils/json-schema-to-zod/parsers/parseNull.d.ts +0 -4
  46. package/lib/types/utils/json-schema-to-zod/parsers/parseNullable.d.ts +0 -7
  47. package/lib/types/utils/json-schema-to-zod/parsers/parseNumber.d.ts +0 -4
  48. package/lib/types/utils/json-schema-to-zod/parsers/parseObject.d.ts +0 -4
  49. package/lib/types/utils/json-schema-to-zod/parsers/parseOneOf.d.ts +0 -4
  50. package/lib/types/utils/json-schema-to-zod/parsers/parseSchema.d.ts +0 -46
  51. package/lib/types/utils/json-schema-to-zod/parsers/parseString.d.ts +0 -4
  52. package/lib/types/utils/json-schema-to-zod/utils/cliTools.d.ts +0 -28
  53. package/lib/types/utils/json-schema-to-zod/utils/half.d.ts +0 -1
  54. package/lib/types/utils/json-schema-to-zod/utils/jsdocs.d.ts +0 -3
  55. package/lib/types/utils/json-schema-to-zod/utils/omit.d.ts +0 -1
  56. package/lib/types/utils/json-schema-to-zod/utils/withMessage.d.ts +0 -10
@@ -125,8 +125,8 @@ export declare function defineTool<Input, Output = undefined>(options: ToolProps
125
125
  message: any;
126
126
  };
127
127
  }>;
128
- description?: string | undefined;
129
128
  _meta?: Record<string, unknown> | undefined;
129
+ description?: string | undefined;
130
130
  annotations?: import("@modelcontextprotocol/sdk/types.js").ToolAnnotations;
131
131
  title?: string | undefined;
132
132
  } | {
@@ -235,8 +235,8 @@ export declare function defineTool<Input, Output = undefined>(options: ToolProps
235
235
  message: any;
236
236
  };
237
237
  }>;
238
- description?: string | undefined;
239
238
  _meta?: Record<string, unknown> | undefined;
239
+ description?: string | undefined;
240
240
  annotations?: import("@modelcontextprotocol/sdk/types.js").ToolAnnotations;
241
241
  title?: string | undefined;
242
242
  token: Type | AbstractType<any>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tsed/cli-mcp",
3
3
  "description": "MCP CLI for Ts.ED.",
4
- "version": "7.5.0",
4
+ "version": "7.5.1",
5
5
  "type": "module",
6
6
  "main": "./lib/esm/index.js",
7
7
  "source": "./src/index.ts",
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@modelcontextprotocol/sdk": ">=1.26.0",
50
- "@tsed/cli-core": "7.5.0",
50
+ "@tsed/cli-core": "7.5.1",
51
51
  "@tsed/core": ">=8.21.0",
52
52
  "@tsed/di": ">=8.21.0",
53
53
  "@tsed/hooks": ">=8.21.0",
@@ -62,6 +62,7 @@
62
62
  "create-frame": "^1.0.0",
63
63
  "fs-extra": "^11.2.0",
64
64
  "globby": "^14.0.2",
65
+ "json-schema-to-zod": "^2.8.1",
65
66
  "read-pkg-up": "^11.0.0",
66
67
  "rxjs": "^7.8.1",
67
68
  "semver": "^7.6.3",
@@ -73,7 +74,7 @@
73
74
  "@swc-node/register": "^1.10.9",
74
75
  "@swc/core": "1.7.26",
75
76
  "@swc/helpers": "^0.5.13",
76
- "@tsed/typescript": "7.5.0",
77
+ "@tsed/typescript": "7.5.1",
77
78
  "@types/change-case": "^2.3.1",
78
79
  "@types/consolidate": "0.14.4",
79
80
  "cross-env": "7.0.3",
@@ -1 +0,0 @@
1
- export {};
@@ -1,74 +0,0 @@
1
- #!/usr/bin/env node
2
- import { mkdirSync, writeFileSync } from "fs";
3
- import { dirname } from "path";
4
- import { jsonSchemaToZod } from "./jsonSchemaToZod.js";
5
- import { parseArgs, parseOrReadJSON, readPipe } from "./utils/cliTools.js";
6
- const params = {
7
- input: {
8
- shorthand: "i",
9
- value: "string",
10
- required: process.stdin.isTTY && "input is required when no JSON or file path is piped",
11
- description: "JSON or a source file path. Required if no data is piped."
12
- },
13
- output: {
14
- shorthand: "o",
15
- value: "string",
16
- description: "A file path to write to. If not supplied stdout will be used."
17
- },
18
- name: {
19
- shorthand: "n",
20
- value: "string",
21
- description: "The name of the schema in the output."
22
- },
23
- depth: {
24
- shorthand: "d",
25
- value: "number",
26
- description: "Maximum depth of recursion before falling back to z.any(). Defaults to 0."
27
- },
28
- module: {
29
- shorthand: "m",
30
- value: ["esm", "cjs", "none"],
31
- description: "Module syntax; 'esm', 'cjs' or 'none'. Defaults to 'esm'."
32
- },
33
- type: {
34
- shorthand: "t",
35
- value: "string",
36
- description: "The name of the (optional) inferred type export."
37
- },
38
- noImport: {
39
- shorthand: "ni",
40
- description: "Removes the `import { z } from 'zod';` or equivalent from the output."
41
- },
42
- withJsdocs: {
43
- shorthand: "wj",
44
- description: "Generate jsdocs off of the description property."
45
- },
46
- zodVersion: {
47
- shorthand: "zv",
48
- value: "number",
49
- description: "Target Zod version: 3 or 4. Defaults to 4."
50
- }
51
- };
52
- async function main() {
53
- const args = parseArgs(params, process.argv, true);
54
- const input = args.input || (await readPipe());
55
- const jsonSchema = parseOrReadJSON(input);
56
- const zodVersion = (args.zodVersion === 3 ? 3 : 4);
57
- const zodSchema = jsonSchemaToZod(jsonSchema, {
58
- name: args.name,
59
- depth: args.depth,
60
- module: args.module || "esm",
61
- noImport: args.noImport,
62
- type: args.type,
63
- withJsdocs: args.withJsdocs,
64
- zodVersion
65
- });
66
- if (args.output) {
67
- mkdirSync(dirname(args.output), { recursive: true });
68
- writeFileSync(args.output, zodSchema);
69
- }
70
- else {
71
- console.log(zodSchema);
72
- }
73
- }
74
- void main();
@@ -1,25 +0,0 @@
1
- export * from "./jsonSchemaToZod.js";
2
- export * from "./parsers/parseAllOf.js";
3
- export * from "./parsers/parseAnyOf.js";
4
- export * from "./parsers/parseArray.js";
5
- export * from "./parsers/parseBoolean.js";
6
- export * from "./parsers/parseConst.js";
7
- export * from "./parsers/parseDefault.js";
8
- export * from "./parsers/parseEnum.js";
9
- export * from "./parsers/parseIfThenElse.js";
10
- export * from "./parsers/parseMultipleType.js";
11
- export * from "./parsers/parseNot.js";
12
- export * from "./parsers/parseNull.js";
13
- export * from "./parsers/parseNullable.js";
14
- export * from "./parsers/parseNumber.js";
15
- export * from "./parsers/parseObject.js";
16
- export * from "./parsers/parseOneOf.js";
17
- export * from "./parsers/parseSchema.js";
18
- export * from "./parsers/parseString.js";
19
- export * from "./Types.js";
20
- export * from "./utils/half.js";
21
- export * from "./utils/jsdocs.js";
22
- export * from "./utils/omit.js";
23
- export * from "./utils/withMessage.js";
24
- import { jsonSchemaToZod } from "./jsonSchemaToZod.js";
25
- export default jsonSchemaToZod;
@@ -1,43 +0,0 @@
1
- import { parseSchema } from "./parsers/parseSchema.js";
2
- import { expandJsdocs } from "./utils/jsdocs.js";
3
- export const jsonSchemaToZod = (schema, { module, name, type, noImport, zodVersion = 4, ...rest } = {}) => {
4
- if (type && (!name || module !== "esm")) {
5
- throw new Error("Option `type` requires `name` to be set and `module` to be `esm`");
6
- }
7
- let result = parseSchema(schema, {
8
- module,
9
- name,
10
- path: [],
11
- seen: new Map(),
12
- zodVersion,
13
- ...rest
14
- });
15
- const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description ? expandJsdocs(schema.description) : "";
16
- if (module === "cjs") {
17
- result = `${jsdocs}module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result}
18
- `;
19
- if (!noImport) {
20
- result = `${jsdocs}const { z } = require("zod")
21
-
22
- ${result}`;
23
- }
24
- }
25
- else if (module === "esm") {
26
- result = `${jsdocs}export ${name ? `const ${name} =` : `default`} ${result}
27
- `;
28
- if (!noImport) {
29
- result = `import { z } from "zod"
30
-
31
- ${result}`;
32
- }
33
- }
34
- else if (name) {
35
- result = `${jsdocs}const ${name} = ${result}`;
36
- }
37
- if (type && name) {
38
- let typeName = typeof type === "string" ? type : `${name[0].toUpperCase()}${name.substring(1)}`;
39
- result += `export type ${typeName} = z.infer<typeof ${name}>
40
- `;
41
- }
42
- return result;
43
- };
@@ -1,37 +0,0 @@
1
- import { half } from "../utils/half.js";
2
- import { parseSchema } from "./parseSchema.js";
3
- const originalIndex = Symbol("Original index");
4
- const ensureOriginalIndex = (arr) => {
5
- let newArr = [];
6
- for (let i = 0; i < arr.length; i++) {
7
- const item = arr[i];
8
- if (typeof item === "boolean") {
9
- newArr.push(item ? { [originalIndex]: i } : { [originalIndex]: i, not: {} });
10
- }
11
- else if (originalIndex in item) {
12
- return arr;
13
- }
14
- else {
15
- newArr.push({ ...item, [originalIndex]: i });
16
- }
17
- }
18
- return newArr;
19
- };
20
- export function parseAllOf(schema, refs) {
21
- if (schema.allOf.length === 0) {
22
- return "z.never()";
23
- }
24
- else if (schema.allOf.length === 1) {
25
- const item = schema.allOf[0];
26
- return parseSchema(item, {
27
- ...refs,
28
- path: [...refs.path, "allOf", item[originalIndex]]
29
- });
30
- }
31
- else {
32
- const [left, right] = half(ensureOriginalIndex(schema.allOf));
33
- return `z.intersection(${parseAllOf({ allOf: left }, refs)}, ${parseAllOf({
34
- allOf: right
35
- }, refs)})`;
36
- }
37
- }
@@ -1,11 +0,0 @@
1
- import { parseSchema } from "./parseSchema.js";
2
- export const parseAnyOf = (schema, refs) => {
3
- return schema.anyOf.length
4
- ? schema.anyOf.length === 1
5
- ? parseSchema(schema.anyOf[0], {
6
- ...refs,
7
- path: [...refs.path, "anyOf", 0]
8
- })
9
- : `z.union([${schema.anyOf.map((schema, i) => parseSchema(schema, { ...refs, path: [...refs.path, "anyOf", i] })).join(", ")}])`
10
- : `z.any()`;
11
- };
@@ -1,16 +0,0 @@
1
- import { withMessage } from "../utils/withMessage.js";
2
- import { parseSchema } from "./parseSchema.js";
3
- export const parseArray = (schema, refs) => {
4
- if (Array.isArray(schema.items)) {
5
- return `z.tuple([${schema.items.map((v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
6
- }
7
- let r = !schema.items
8
- ? "z.array(z.any())"
9
- : `z.array(${parseSchema(schema.items, {
10
- ...refs,
11
- path: [...refs.path, "items"]
12
- })})`;
13
- r += withMessage(schema, "minItems", ({ json }) => [`.min(${json}`, ", ", ")"]);
14
- r += withMessage(schema, "maxItems", ({ json }) => [`.max(${json}`, ", ", ")"]);
15
- return r;
16
- };
@@ -1,3 +0,0 @@
1
- export const parseBoolean = (_schema) => {
2
- return "z.boolean()";
3
- };
@@ -1,3 +0,0 @@
1
- export const parseConst = (schema) => {
2
- return `z.literal(${JSON.stringify(schema.const)})`;
3
- };
@@ -1,3 +0,0 @@
1
- export const parseDefault = (_schema) => {
2
- return "z.any()";
3
- };
@@ -1,15 +0,0 @@
1
- export const parseEnum = (schema) => {
2
- if (schema.enum.length === 0) {
3
- return "z.never()";
4
- }
5
- else if (schema.enum.length === 1) {
6
- // union does not work when there is only one element
7
- return `z.literal(${JSON.stringify(schema.enum[0])})`;
8
- }
9
- else if (schema.enum.every((x) => typeof x === "string")) {
10
- return `z.enum([${schema.enum.map((x) => JSON.stringify(x))}])`;
11
- }
12
- else {
13
- return `z.union([${schema.enum.map((x) => `z.literal(${JSON.stringify(x)})`).join(", ")}])`;
14
- }
15
- };
@@ -1,20 +0,0 @@
1
- import { parseSchema } from "./parseSchema.js";
2
- export const parseIfThenElse = (schema, refs) => {
3
- const $if = parseSchema(schema.if, { ...refs, path: [...refs.path, "if"] });
4
- const $then = parseSchema(schema.then, {
5
- ...refs,
6
- path: [...refs.path, "then"]
7
- });
8
- const $else = parseSchema(schema.else, {
9
- ...refs,
10
- path: [...refs.path, "else"]
11
- });
12
- return `z.union([${$then}, ${$else}]).superRefine((value,ctx) => {
13
- const result = ${$if}.safeParse(value).success
14
- ? ${$then}.safeParse(value)
15
- : ${$else}.safeParse(value);
16
- if (!result.success) {
17
- result.error.errors.forEach((error) => ctx.addIssue(error))
18
- }
19
- })`;
20
- };
@@ -1,4 +0,0 @@
1
- import { parseSchema } from "./parseSchema.js";
2
- export const parseMultipleType = (schema, refs) => {
3
- return `z.union([${schema.type.map((type) => parseSchema({ ...schema, type }, { ...refs, withoutDefaults: true })).join(", ")}])`;
4
- };
@@ -1,7 +0,0 @@
1
- import { parseSchema } from "./parseSchema.js";
2
- export const parseNot = (schema, refs) => {
3
- return `z.any().refine((value) => !${parseSchema(schema.not, {
4
- ...refs,
5
- path: [...refs.path, "not"]
6
- })}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`;
7
- };
@@ -1,3 +0,0 @@
1
- export const parseNull = (_schema) => {
2
- return "z.null()";
3
- };
@@ -1,8 +0,0 @@
1
- import { omit } from "../utils/omit.js";
2
- import { parseSchema } from "./parseSchema.js";
3
- /**
4
- * For compatibility with open api 3.0 nullable
5
- */
6
- export const parseNullable = (schema, refs) => {
7
- return `${parseSchema(omit(schema, "nullable"), refs, true)}.nullable()`;
8
- };
@@ -1,46 +0,0 @@
1
- import { withMessage } from "../utils/withMessage.js";
2
- export const parseNumber = (schema) => {
3
- let r = "z.number()";
4
- if (schema.type === "integer") {
5
- r += withMessage(schema, "type", () => [".int(", ")"]);
6
- }
7
- else {
8
- r += withMessage(schema, "format", ({ value }) => {
9
- if (value === "int64") {
10
- return [".int(", ")"];
11
- }
12
- });
13
- }
14
- r += withMessage(schema, "multipleOf", ({ value, json }) => {
15
- if (value === 1) {
16
- if (r.startsWith("z.number().int(")) {
17
- return;
18
- }
19
- return [".int(", ")"];
20
- }
21
- return [`.multipleOf(${json}`, ", ", ")"];
22
- });
23
- if (typeof schema.minimum === "number") {
24
- if (schema.exclusiveMinimum === true) {
25
- r += withMessage(schema, "minimum", ({ json }) => [`.gt(${json}`, ", ", ")"]);
26
- }
27
- else {
28
- r += withMessage(schema, "minimum", ({ json }) => [`.gte(${json}`, ", ", ")"]);
29
- }
30
- }
31
- else if (typeof schema.exclusiveMinimum === "number") {
32
- r += withMessage(schema, "exclusiveMinimum", ({ json }) => [`.gt(${json}`, ", ", ")"]);
33
- }
34
- if (typeof schema.maximum === "number") {
35
- if (schema.exclusiveMaximum === true) {
36
- r += withMessage(schema, "maximum", ({ json }) => [`.lt(${json}`, ", ", ")"]);
37
- }
38
- else {
39
- r += withMessage(schema, "maximum", ({ json }) => [`.lte(${json}`, ", ", ")"]);
40
- }
41
- }
42
- else if (typeof schema.exclusiveMaximum === "number") {
43
- r += withMessage(schema, "exclusiveMaximum", ({ json }) => [`.lt(${json}`, ", ", ")"]);
44
- }
45
- return r;
46
- };
@@ -1,171 +0,0 @@
1
- import { addJsdocs } from "../utils/jsdocs.js";
2
- import { parseAllOf } from "./parseAllOf.js";
3
- import { parseAnyOf } from "./parseAnyOf.js";
4
- import { parseOneOf } from "./parseOneOf.js";
5
- import { its, parseSchema } from "./parseSchema.js";
6
- // Helper for z.record() generation - Zod v4 requires explicit key type
7
- function emitRecord(valueSchema, refs) {
8
- if (refs.zodVersion === 3) {
9
- return `z.record(${valueSchema})`;
10
- }
11
- // Default to v4 syntax
12
- return `z.record(z.string(), ${valueSchema})`;
13
- }
14
- // Helper for error path in superRefine - Zod v4 uses simplified path
15
- function emitErrorPath(refs) {
16
- if (refs.zodVersion === 3) {
17
- return `path: [...ctx.path, key]`;
18
- }
19
- // Default to v4 syntax
20
- return `path: [key]`;
21
- }
22
- export function parseObject(objectSchema, refs) {
23
- let properties = undefined;
24
- if (objectSchema.properties) {
25
- if (!Object.keys(objectSchema.properties).length) {
26
- properties = "z.object({})";
27
- }
28
- else {
29
- properties = "z.object({ ";
30
- properties += Object.keys(objectSchema.properties)
31
- .map((key) => {
32
- const propSchema = objectSchema.properties[key];
33
- let result = `${JSON.stringify(key)}: ${parseSchema(propSchema, {
34
- ...refs,
35
- path: [...refs.path, "properties", key]
36
- })}`;
37
- if (refs.withJsdocs && typeof propSchema === "object") {
38
- result = addJsdocs(propSchema, result);
39
- }
40
- const hasDefault = typeof propSchema === "object" && propSchema.default !== undefined;
41
- const required = Array.isArray(objectSchema.required)
42
- ? objectSchema.required.includes(key)
43
- : typeof propSchema === "object" && propSchema.required === true;
44
- const optional = !hasDefault && !required;
45
- return optional ? `${result}.optional()` : result;
46
- })
47
- .join(", ");
48
- properties += " })";
49
- }
50
- }
51
- const additionalProperties = objectSchema.additionalProperties !== undefined
52
- ? parseSchema(objectSchema.additionalProperties, {
53
- ...refs,
54
- path: [...refs.path, "additionalProperties"]
55
- })
56
- : undefined;
57
- let patternProperties = undefined;
58
- if (objectSchema.patternProperties) {
59
- const parsedPatternProperties = Object.fromEntries(Object.entries(objectSchema.patternProperties).map(([key, value]) => {
60
- return [
61
- key,
62
- parseSchema(value, {
63
- ...refs,
64
- path: [...refs.path, "patternProperties", key]
65
- })
66
- ];
67
- }, {}));
68
- patternProperties = "";
69
- if (properties) {
70
- if (additionalProperties) {
71
- patternProperties += `.catchall(z.union([${[...Object.values(parsedPatternProperties), additionalProperties].join(", ")}]))`;
72
- }
73
- else if (Object.keys(parsedPatternProperties).length > 1) {
74
- patternProperties += `.catchall(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
75
- }
76
- else {
77
- patternProperties += `.catchall(${Object.values(parsedPatternProperties)})`;
78
- }
79
- }
80
- else {
81
- if (additionalProperties) {
82
- patternProperties += emitRecord(`z.union([${[...Object.values(parsedPatternProperties), additionalProperties].join(", ")}])`, refs);
83
- }
84
- else if (Object.keys(parsedPatternProperties).length > 1) {
85
- patternProperties += emitRecord(`z.union([${Object.values(parsedPatternProperties).join(", ")}])`, refs);
86
- }
87
- else {
88
- patternProperties += emitRecord(`${Object.values(parsedPatternProperties)}`, refs);
89
- }
90
- }
91
- patternProperties += ".superRefine((value, ctx) => {\n";
92
- patternProperties += "for (const key in value) {\n";
93
- if (additionalProperties) {
94
- if (objectSchema.properties) {
95
- patternProperties += `let evaluated = [${Object.keys(objectSchema.properties)
96
- .map((key) => JSON.stringify(key))
97
- .join(", ")}].includes(key)\n`;
98
- }
99
- else {
100
- patternProperties += `let evaluated = false\n`;
101
- }
102
- }
103
- for (const key in objectSchema.patternProperties) {
104
- patternProperties += "if (key.match(new RegExp(" + JSON.stringify(key) + "))) {\n";
105
- if (additionalProperties) {
106
- patternProperties += "evaluated = true\n";
107
- }
108
- patternProperties += "const result = " + parsedPatternProperties[key] + ".safeParse(value[key])\n";
109
- patternProperties += "if (!result.success) {\n";
110
- patternProperties += `ctx.addIssue({
111
- ${emitErrorPath(refs)},
112
- code: 'custom',
113
- message: \`Invalid input: Key matching regex /\${key}/ must match schema\`,
114
- params: {
115
- issues: result.error.issues
116
- }
117
- })\n`;
118
- patternProperties += "}\n";
119
- patternProperties += "}\n";
120
- }
121
- if (additionalProperties) {
122
- patternProperties += "if (!evaluated) {\n";
123
- patternProperties += "const result = " + additionalProperties + ".safeParse(value[key])\n";
124
- patternProperties += "if (!result.success) {\n";
125
- patternProperties += `ctx.addIssue({
126
- ${emitErrorPath(refs)},
127
- code: 'custom',
128
- message: \`Invalid input: must match catchall schema\`,
129
- params: {
130
- issues: result.error.issues
131
- }
132
- })\n`;
133
- patternProperties += "}\n";
134
- patternProperties += "}\n";
135
- }
136
- patternProperties += "}\n";
137
- patternProperties += "})";
138
- }
139
- let output = properties
140
- ? patternProperties
141
- ? properties + patternProperties
142
- : additionalProperties
143
- ? additionalProperties === "z.never()"
144
- ? properties + ".strict()"
145
- : properties + `.catchall(${additionalProperties})`
146
- : properties
147
- : patternProperties
148
- ? patternProperties
149
- : additionalProperties
150
- ? emitRecord(additionalProperties, refs)
151
- : emitRecord("z.any()", refs);
152
- if (its.an.anyOf(objectSchema)) {
153
- output += `.and(${parseAnyOf({
154
- ...objectSchema,
155
- anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? { ...x, type: "object" } : x)
156
- }, refs)})`;
157
- }
158
- if (its.a.oneOf(objectSchema)) {
159
- output += `.and(${parseOneOf({
160
- ...objectSchema,
161
- oneOf: objectSchema.oneOf.map((x) => typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? { ...x, type: "object" } : x)
162
- }, refs)})`;
163
- }
164
- if (its.an.allOf(objectSchema)) {
165
- output += `.and(${parseAllOf({
166
- ...objectSchema,
167
- allOf: objectSchema.allOf.map((x) => typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? { ...x, type: "object" } : x)
168
- }, refs)})`;
169
- }
170
- return output;
171
- }
@@ -1,34 +0,0 @@
1
- import { parseSchema } from "./parseSchema.js";
2
- export const parseOneOf = (schema, refs) => {
3
- return schema.oneOf.length
4
- ? schema.oneOf.length === 1
5
- ? parseSchema(schema.oneOf[0], {
6
- ...refs,
7
- path: [...refs.path, "oneOf", 0]
8
- })
9
- : `z.any().superRefine((x, ctx) => {
10
- const schemas = [${schema.oneOf
11
- .map((schema, i) => parseSchema(schema, {
12
- ...refs,
13
- path: [...refs.path, "oneOf", i]
14
- }))
15
- .join(", ")}];
16
- const errors = schemas.reduce<z.ZodError[]>(
17
- (errors, schema) =>
18
- ((result) =>
19
- result.error ? [...errors, result.error] : errors)(
20
- schema.safeParse(x),
21
- ),
22
- [],
23
- );
24
- if (schemas.length - errors.length !== 1) {
25
- ctx.addIssue({
26
- path: ctx.path,
27
- code: "invalid_union",
28
- unionErrors: errors,
29
- message: "Invalid input: Should pass single schema",
30
- });
31
- }
32
- })`
33
- : "z.any()";
34
- };