@tsed/platform-mcp 8.28.6 → 8.28.7

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 (86) hide show
  1. package/lib/esm/index.js +0 -26
  2. package/lib/esm/utils/toZod.js +1 -1
  3. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  4. package/lib/types/fn/defineResource.d.ts +4 -4
  5. package/lib/types/index.d.ts +0 -26
  6. package/package.json +8 -7
  7. package/src/index.ts +0 -26
  8. package/src/utils/toZod.ts +1 -2
  9. package/lib/esm/utils/json-schema-to-zod/Types.js +0 -1
  10. package/lib/esm/utils/json-schema-to-zod/cli.js +0 -83
  11. package/lib/esm/utils/json-schema-to-zod/index.js +0 -25
  12. package/lib/esm/utils/json-schema-to-zod/jsonSchemaToZod.js +0 -52
  13. package/lib/esm/utils/json-schema-to-zod/parsers/parseAllOf.js +0 -43
  14. package/lib/esm/utils/json-schema-to-zod/parsers/parseAnyOf.js +0 -17
  15. package/lib/esm/utils/json-schema-to-zod/parsers/parseArray.js +0 -22
  16. package/lib/esm/utils/json-schema-to-zod/parsers/parseBoolean.js +0 -9
  17. package/lib/esm/utils/json-schema-to-zod/parsers/parseConst.js +0 -9
  18. package/lib/esm/utils/json-schema-to-zod/parsers/parseDefault.js +0 -9
  19. package/lib/esm/utils/json-schema-to-zod/parsers/parseEnum.js +0 -21
  20. package/lib/esm/utils/json-schema-to-zod/parsers/parseIfThenElse.js +0 -26
  21. package/lib/esm/utils/json-schema-to-zod/parsers/parseMultipleType.js +0 -10
  22. package/lib/esm/utils/json-schema-to-zod/parsers/parseNot.js +0 -13
  23. package/lib/esm/utils/json-schema-to-zod/parsers/parseNull.js +0 -9
  24. package/lib/esm/utils/json-schema-to-zod/parsers/parseNullable.js +0 -11
  25. package/lib/esm/utils/json-schema-to-zod/parsers/parseNumber.js +0 -52
  26. package/lib/esm/utils/json-schema-to-zod/parsers/parseObject.js +0 -177
  27. package/lib/esm/utils/json-schema-to-zod/parsers/parseOneOf.js +0 -40
  28. package/lib/esm/utils/json-schema-to-zod/parsers/parseSchema.js +0 -153
  29. package/lib/esm/utils/json-schema-to-zod/parsers/parseString.js +0 -63
  30. package/lib/esm/utils/json-schema-to-zod/utils/cliTools.js +0 -126
  31. package/lib/esm/utils/json-schema-to-zod/utils/half.js +0 -9
  32. package/lib/esm/utils/json-schema-to-zod/utils/jsdocs.js +0 -24
  33. package/lib/esm/utils/json-schema-to-zod/utils/omit.js +0 -12
  34. package/lib/esm/utils/json-schema-to-zod/utils/withMessage.js +0 -28
  35. package/lib/types/utils/json-schema-to-zod/Types.d.ts +0 -116
  36. package/lib/types/utils/json-schema-to-zod/cli.d.ts +0 -2
  37. package/lib/types/utils/json-schema-to-zod/index.d.ts +0 -25
  38. package/lib/types/utils/json-schema-to-zod/jsonSchemaToZod.d.ts +0 -11
  39. package/lib/types/utils/json-schema-to-zod/parsers/parseAllOf.d.ts +0 -10
  40. package/lib/types/utils/json-schema-to-zod/parsers/parseAnyOf.d.ts +0 -10
  41. package/lib/types/utils/json-schema-to-zod/parsers/parseArray.d.ts +0 -10
  42. package/lib/types/utils/json-schema-to-zod/parsers/parseBoolean.d.ts +0 -10
  43. package/lib/types/utils/json-schema-to-zod/parsers/parseConst.d.ts +0 -10
  44. package/lib/types/utils/json-schema-to-zod/parsers/parseDefault.d.ts +0 -8
  45. package/lib/types/utils/json-schema-to-zod/parsers/parseEnum.d.ts +0 -10
  46. package/lib/types/utils/json-schema-to-zod/parsers/parseIfThenElse.d.ts +0 -12
  47. package/lib/types/utils/json-schema-to-zod/parsers/parseMultipleType.d.ts +0 -10
  48. package/lib/types/utils/json-schema-to-zod/parsers/parseNot.d.ts +0 -10
  49. package/lib/types/utils/json-schema-to-zod/parsers/parseNull.d.ts +0 -10
  50. package/lib/types/utils/json-schema-to-zod/parsers/parseNullable.d.ts +0 -10
  51. package/lib/types/utils/json-schema-to-zod/parsers/parseNumber.d.ts +0 -10
  52. package/lib/types/utils/json-schema-to-zod/parsers/parseObject.d.ts +0 -10
  53. package/lib/types/utils/json-schema-to-zod/parsers/parseOneOf.d.ts +0 -10
  54. package/lib/types/utils/json-schema-to-zod/parsers/parseSchema.d.ts +0 -61
  55. package/lib/types/utils/json-schema-to-zod/parsers/parseString.d.ts +0 -10
  56. package/lib/types/utils/json-schema-to-zod/utils/cliTools.d.ts +0 -67
  57. package/lib/types/utils/json-schema-to-zod/utils/half.d.ts +0 -7
  58. package/lib/types/utils/json-schema-to-zod/utils/jsdocs.d.ts +0 -15
  59. package/lib/types/utils/json-schema-to-zod/utils/omit.d.ts +0 -7
  60. package/lib/types/utils/json-schema-to-zod/utils/withMessage.d.ts +0 -19
  61. package/src/utils/json-schema-to-zod/Types.ts +0 -123
  62. package/src/utils/json-schema-to-zod/cli.ts +0 -88
  63. package/src/utils/json-schema-to-zod/index.ts +0 -26
  64. package/src/utils/json-schema-to-zod/jsonSchemaToZod.ts +0 -60
  65. package/src/utils/json-schema-to-zod/parsers/parseAllOf.ts +0 -50
  66. package/src/utils/json-schema-to-zod/parsers/parseAnyOf.ts +0 -19
  67. package/src/utils/json-schema-to-zod/parsers/parseArray.ts +0 -28
  68. package/src/utils/json-schema-to-zod/parsers/parseBoolean.ts +0 -11
  69. package/src/utils/json-schema-to-zod/parsers/parseConst.ts +0 -11
  70. package/src/utils/json-schema-to-zod/parsers/parseDefault.ts +0 -11
  71. package/src/utils/json-schema-to-zod/parsers/parseEnum.ts +0 -20
  72. package/src/utils/json-schema-to-zod/parsers/parseIfThenElse.ts +0 -35
  73. package/src/utils/json-schema-to-zod/parsers/parseMultipleType.ts +0 -12
  74. package/src/utils/json-schema-to-zod/parsers/parseNot.ts +0 -15
  75. package/src/utils/json-schema-to-zod/parsers/parseNull.ts +0 -11
  76. package/src/utils/json-schema-to-zod/parsers/parseNullable.ts +0 -13
  77. package/src/utils/json-schema-to-zod/parsers/parseNumber.ts +0 -56
  78. package/src/utils/json-schema-to-zod/parsers/parseObject.ts +0 -220
  79. package/src/utils/json-schema-to-zod/parsers/parseOneOf.ts +0 -44
  80. package/src/utils/json-schema-to-zod/parsers/parseSchema.ts +0 -190
  81. package/src/utils/json-schema-to-zod/parsers/parseString.ts +0 -73
  82. package/src/utils/json-schema-to-zod/utils/cliTools.ts +0 -189
  83. package/src/utils/json-schema-to-zod/utils/half.ts +0 -9
  84. package/src/utils/json-schema-to-zod/utils/jsdocs.ts +0 -29
  85. package/src/utils/json-schema-to-zod/utils/omit.ts +0 -14
  86. package/src/utils/json-schema-to-zod/utils/withMessage.ts +0 -42
@@ -1,88 +0,0 @@
1
- #!/usr/bin/env node
2
- import {mkdirSync, writeFileSync} from "fs";
3
- import {dirname} from "path";
4
- import {fileURLToPath} from "url";
5
-
6
- import {jsonSchemaToZod} from "./jsonSchemaToZod.js";
7
- import {JsonSchema, ZodVersion} from "./Types.js";
8
- import {parseArgs, parseOrReadJSON, readPipe} from "./utils/cliTools.js";
9
-
10
- const params = {
11
- input: {
12
- shorthand: "i",
13
- value: "string",
14
- required: process.stdin.isTTY && "input is required when no JSON or file path is piped",
15
- description: "JSON or a source file path. Required if no data is piped."
16
- },
17
- output: {
18
- shorthand: "o",
19
- value: "string",
20
- description: "A file path to write to. If not supplied stdout will be used."
21
- },
22
- name: {
23
- shorthand: "n",
24
- value: "string",
25
- description: "The name of the schema in the output."
26
- },
27
- depth: {
28
- shorthand: "d",
29
- value: "number",
30
- description: "Maximum depth of recursion before falling back to z.any(). Defaults to 0."
31
- },
32
- module: {
33
- shorthand: "m",
34
- value: ["esm", "cjs", "none"],
35
- description: "Module syntax; 'esm', 'cjs' or 'none'. Defaults to 'esm'."
36
- },
37
- type: {
38
- shorthand: "t",
39
- value: "string",
40
- description: "The name of the (optional) inferred type export."
41
- },
42
- noImport: {
43
- shorthand: "ni",
44
- description: "Removes the `import { z } from 'zod';` or equivalent from the output."
45
- },
46
- withJsdocs: {
47
- shorthand: "wj",
48
- description: "Generate jsdocs off of the description property."
49
- },
50
- zodVersion: {
51
- shorthand: "zv",
52
- value: "number",
53
- description: "Target Zod version: 3 or 4. Defaults to 4."
54
- }
55
- } as const;
56
-
57
- /**
58
- * Entry point for the `json-schema-to-zod` CLI that converts JSON Schema files into Zod source code.
59
- *
60
- * @module platform/mcp
61
- * @since 8.17.0
62
- */
63
- async function main() {
64
- const args = parseArgs(params, process.argv, true);
65
- const input = args.input || (await readPipe());
66
- const jsonSchema = parseOrReadJSON(input);
67
- const zodVersion = (args.zodVersion === 3 ? 3 : 4) as ZodVersion;
68
- const zodSchema = jsonSchemaToZod(jsonSchema as JsonSchema, {
69
- name: args.name,
70
- depth: args.depth,
71
- module: args.module || "esm",
72
- noImport: args.noImport,
73
- type: args.type,
74
- withJsdocs: args.withJsdocs,
75
- zodVersion
76
- });
77
-
78
- if (args.output) {
79
- mkdirSync(dirname(args.output), {recursive: true});
80
- writeFileSync(args.output, zodSchema);
81
- } else {
82
- console.log(zodSchema);
83
- }
84
- }
85
-
86
- if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
87
- void main();
88
- }
@@ -1,26 +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
-
26
- export default jsonSchemaToZod;
@@ -1,60 +0,0 @@
1
- import {parseSchema} from "./parsers/parseSchema.js";
2
- import {JsonSchema, Options} from "./Types.js";
3
- import {expandJsdocs} from "./utils/jsdocs.js";
4
-
5
- /**
6
- * Generates Zod code from a JSON Schema, optionally targeting specific module systems and versions.
7
- *
8
- * @param schema JSON Schema definition to translate.
9
- * @param options Generation options such as module format, naming, and version targets.
10
- * @returns A string of executable JavaScript/TypeScript that constructs an equivalent Zod schema.
11
- * @module platform/mcp
12
- * @since 8.17.0
13
- */
14
- export const jsonSchemaToZod = (schema: JsonSchema, {module, name, type, noImport, zodVersion = 4, ...rest}: Options = {}): string => {
15
- if (type && (!name || module !== "esm")) {
16
- throw new Error("Option `type` requires `name` to be set and `module` to be `esm`");
17
- }
18
-
19
- let result = parseSchema(schema, {
20
- module,
21
- name,
22
- path: [],
23
- seen: new Map(),
24
- zodVersion,
25
- ...rest
26
- });
27
-
28
- const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description ? expandJsdocs(schema.description) : "";
29
-
30
- if (module === "cjs") {
31
- result = `${jsdocs}module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result}
32
- `;
33
-
34
- if (!noImport) {
35
- result = `${jsdocs}const { z } = require("zod")
36
-
37
- ${result}`;
38
- }
39
- } else if (module === "esm") {
40
- result = `${jsdocs}export ${name ? `const ${name} =` : `default`} ${result}
41
- `;
42
-
43
- if (!noImport) {
44
- result = `import { z } from "zod"
45
-
46
- ${result}`;
47
- }
48
- } else if (name) {
49
- result = `${jsdocs}const ${name} = ${result}`;
50
- }
51
-
52
- if (type && name) {
53
- let typeName = typeof type === "string" ? type : `${name[0].toUpperCase()}${name.substring(1)}`;
54
-
55
- result += `export type ${typeName} = z.infer<typeof ${name}>
56
- `;
57
- }
58
-
59
- return result;
60
- };
@@ -1,50 +0,0 @@
1
- import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
2
- import {half} from "../utils/half.js";
3
- import {parseSchema} from "./parseSchema.js";
4
-
5
- const originalIndex = Symbol("Original index");
6
-
7
- const ensureOriginalIndex = (arr: JsonSchema[]) => {
8
- let newArr = [];
9
-
10
- for (let i = 0; i < arr.length; i++) {
11
- const item = arr[i];
12
- if (typeof item === "boolean") {
13
- newArr.push(item ? {[originalIndex]: i} : {[originalIndex]: i, not: {}});
14
- } else if (originalIndex in item) {
15
- return arr;
16
- } else {
17
- newArr.push({...item, [originalIndex]: i});
18
- }
19
- }
20
-
21
- return newArr;
22
- };
23
-
24
- /**
25
- * Parses an `allOf` schema by intersecting all child schemas, preserving evaluation order for accurate metadata.
26
- *
27
- * @module platform/mcp
28
- * @since 8.17.0
29
- */
30
- export function parseAllOf(schema: JsonSchemaObject & {allOf: JsonSchema[]}, refs: Refs): string {
31
- if (schema.allOf.length === 0) {
32
- return "z.never()";
33
- } else if (schema.allOf.length === 1) {
34
- const item = schema.allOf[0];
35
-
36
- return parseSchema(item, {
37
- ...refs,
38
- path: [...refs.path, "allOf", (item as any)[originalIndex]]
39
- });
40
- } else {
41
- const [left, right] = half(ensureOriginalIndex(schema.allOf)) as any;
42
-
43
- return `z.intersection(${parseAllOf({allOf: left}, refs)}, ${parseAllOf(
44
- {
45
- allOf: right
46
- },
47
- refs
48
- )})`;
49
- }
50
- }
@@ -1,19 +0,0 @@
1
- import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
2
- import {parseSchema} from "./parseSchema.js";
3
-
4
- /**
5
- * Parses an `anyOf` node into a Zod union across all nested schemas.
6
- *
7
- * @module platform/mcp
8
- * @since 8.17.0
9
- */
10
- export const parseAnyOf = (schema: JsonSchemaObject & {anyOf: JsonSchema[]}, refs: Refs) => {
11
- return schema.anyOf.length
12
- ? schema.anyOf.length === 1
13
- ? parseSchema(schema.anyOf[0], {
14
- ...refs,
15
- path: [...refs.path, "anyOf", 0]
16
- })
17
- : `z.union([${schema.anyOf.map((schema, i) => parseSchema(schema, {...refs, path: [...refs.path, "anyOf", i]})).join(", ")}])`
18
- : `z.any()`;
19
- };
@@ -1,28 +0,0 @@
1
- import {JsonSchemaObject, Refs} from "../Types.js";
2
- import {withMessage} from "../utils/withMessage.js";
3
- import {parseSchema} from "./parseSchema.js";
4
-
5
- /**
6
- * Parses array schemas into the appropriate tuple or array expressions with validation hints.
7
- *
8
- * @module platform/mcp
9
- * @since 8.17.0
10
- */
11
- export const parseArray = (schema: JsonSchemaObject & {type: "array"}, refs: Refs) => {
12
- if (Array.isArray(schema.items)) {
13
- return `z.tuple([${schema.items.map((v, i) => parseSchema(v, {...refs, path: [...refs.path, "items", i]}))}])`;
14
- }
15
-
16
- let r = !schema.items
17
- ? "z.array(z.any())"
18
- : `z.array(${parseSchema(schema.items, {
19
- ...refs,
20
- path: [...refs.path, "items"]
21
- })})`;
22
-
23
- r += withMessage(schema, "minItems", ({json}) => [`.min(${json}`, ", ", ")"]);
24
-
25
- r += withMessage(schema, "maxItems", ({json}) => [`.max(${json}`, ", ", ")"]);
26
-
27
- return r;
28
- };
@@ -1,11 +0,0 @@
1
- import {JsonSchemaObject} from "../Types.js";
2
-
3
- /**
4
- * Parses boolean schemas into `z.boolean()`.
5
- *
6
- * @module platform/mcp
7
- * @since 8.17.0
8
- */
9
- export const parseBoolean = (_schema: JsonSchemaObject & {type: "boolean"}) => {
10
- return "z.boolean()";
11
- };
@@ -1,11 +0,0 @@
1
- import {JsonSchemaObject, Serializable} from "../Types.js";
2
-
3
- /**
4
- * Parses constant schemas into `z.literal` expressions.
5
- *
6
- * @module platform/mcp
7
- * @since 8.17.0
8
- */
9
- export const parseConst = (schema: JsonSchemaObject & {const: Serializable}) => {
10
- return `z.literal(${JSON.stringify(schema.const)})`;
11
- };
@@ -1,11 +0,0 @@
1
- import {JsonSchemaObject} from "../Types.js";
2
-
3
- /**
4
- * Fallback parser that maps unknown structures to `z.any()`.
5
- *
6
- * @module platform/mcp
7
- * @since 8.17.0
8
- */
9
- export const parseDefault = (_schema: JsonSchemaObject) => {
10
- return "z.any()";
11
- };
@@ -1,20 +0,0 @@
1
- import {JsonSchemaObject, Serializable} from "../Types.js";
2
-
3
- /**
4
- * Parses enum schemas into either `z.enum` or unions of literals depending on value types.
5
- *
6
- * @module platform/mcp
7
- * @since 8.17.0
8
- */
9
- export const parseEnum = (schema: JsonSchemaObject & {enum: Serializable[]}) => {
10
- if (schema.enum.length === 0) {
11
- return "z.never()";
12
- } else if (schema.enum.length === 1) {
13
- // union does not work when there is only one element
14
- return `z.literal(${JSON.stringify(schema.enum[0])})`;
15
- } else if (schema.enum.every((x) => typeof x === "string")) {
16
- return `z.enum([${schema.enum.map((x) => JSON.stringify(x))}])`;
17
- } else {
18
- return `z.union([${schema.enum.map((x) => `z.literal(${JSON.stringify(x)})`).join(", ")}])`;
19
- }
20
- };
@@ -1,35 +0,0 @@
1
- import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
2
- import {parseSchema} from "./parseSchema.js";
3
-
4
- /**
5
- * Parses conditional schemas by generating union refinements that evaluate `if/then/else` logic at runtime.
6
- *
7
- * @module platform/mcp
8
- * @since 8.17.0
9
- */
10
- export const parseIfThenElse = (
11
- schema: JsonSchemaObject & {
12
- if: JsonSchema;
13
- then: JsonSchema;
14
- else: JsonSchema;
15
- },
16
- refs: Refs
17
- ): string => {
18
- const $if = parseSchema(schema.if, {...refs, path: [...refs.path, "if"]});
19
- const $then = parseSchema(schema.then, {
20
- ...refs,
21
- path: [...refs.path, "then"]
22
- });
23
- const $else = parseSchema(schema.else, {
24
- ...refs,
25
- path: [...refs.path, "else"]
26
- });
27
- return `z.union([${$then}, ${$else}]).superRefine((value,ctx) => {
28
- const result = ${$if}.safeParse(value).success
29
- ? ${$then}.safeParse(value)
30
- : ${$else}.safeParse(value);
31
- if (!result.success) {
32
- result.error.errors.forEach((error) => ctx.addIssue(error))
33
- }
34
- })`;
35
- };
@@ -1,12 +0,0 @@
1
- import {JsonSchemaObject, Refs} from "../Types.js";
2
- import {parseSchema} from "./parseSchema.js";
3
-
4
- /**
5
- * Parses schemas whose `type` keyword is an array, creating a union of each primitive variant.
6
- *
7
- * @module platform/mcp
8
- * @since 8.17.0
9
- */
10
- export const parseMultipleType = (schema: JsonSchemaObject & {type: string[]}, refs: Refs) => {
11
- return `z.union([${schema.type.map((type) => parseSchema({...schema, type} as any, {...refs, withoutDefaults: true})).join(", ")}])`;
12
- };
@@ -1,15 +0,0 @@
1
- import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
2
- import {parseSchema} from "./parseSchema.js";
3
-
4
- /**
5
- * Parses the `not` keyword into a refinement that fails when the nested schema succeeds.
6
- *
7
- * @module platform/mcp
8
- * @since 8.17.0
9
- */
10
- export const parseNot = (schema: JsonSchemaObject & {not: JsonSchema}, refs: Refs) => {
11
- return `z.any().refine((value) => !${parseSchema(schema.not, {
12
- ...refs,
13
- path: [...refs.path, "not"]
14
- })}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`;
15
- };
@@ -1,11 +0,0 @@
1
- import {JsonSchemaObject} from "../Types.js";
2
-
3
- /**
4
- * Parses `type: null` nodes into `z.null()`.
5
- *
6
- * @module platform/mcp
7
- * @since 8.17.0
8
- */
9
- export const parseNull = (_schema: JsonSchemaObject & {type: "null"}) => {
10
- return "z.null()";
11
- };
@@ -1,13 +0,0 @@
1
- import {JsonSchemaObject, Refs} from "../Types.js";
2
- import {omit} from "../utils/omit.js";
3
- import {parseSchema} from "./parseSchema.js";
4
-
5
- /**
6
- * Handles OpenAPI-style `nullable` keywords by delegating to the underlying schema and marking it nullable.
7
- *
8
- * @module platform/mcp
9
- * @since 8.17.0
10
- */
11
- export const parseNullable = (schema: JsonSchemaObject & {nullable: true}, refs: Refs) => {
12
- return `${parseSchema(omit(schema, "nullable"), refs, true)}.nullable()`;
13
- };
@@ -1,56 +0,0 @@
1
- import {JsonSchemaObject} from "../Types.js";
2
- import {withMessage} from "../utils/withMessage.js";
3
-
4
- /**
5
- * Parses numeric schemas, emitting integer formats and range constraints as chained Zod modifiers.
6
- *
7
- * @module platform/mcp
8
- * @since 8.17.0
9
- */
10
- export const parseNumber = (schema: JsonSchemaObject & {type: "number" | "integer"}) => {
11
- let r = "z.number()";
12
-
13
- if (schema.type === "integer") {
14
- r += withMessage(schema, "type", () => [".int(", ")"]);
15
- } else {
16
- r += withMessage(schema, "format", ({value}) => {
17
- if (value === "int64") {
18
- return [".int(", ")"];
19
- }
20
- });
21
- }
22
-
23
- r += withMessage(schema, "multipleOf", ({value, json}) => {
24
- if (value === 1) {
25
- if (r.startsWith("z.number().int(")) {
26
- return;
27
- }
28
-
29
- return [".int(", ")"];
30
- }
31
-
32
- return [`.multipleOf(${json}`, ", ", ")"];
33
- });
34
-
35
- if (typeof schema.minimum === "number") {
36
- if (schema.exclusiveMinimum === true) {
37
- r += withMessage(schema, "minimum", ({json}) => [`.gt(${json}`, ", ", ")"]);
38
- } else {
39
- r += withMessage(schema, "minimum", ({json}) => [`.gte(${json}`, ", ", ")"]);
40
- }
41
- } else if (typeof schema.exclusiveMinimum === "number") {
42
- r += withMessage(schema, "exclusiveMinimum", ({json}) => [`.gt(${json}`, ", ", ")"]);
43
- }
44
-
45
- if (typeof schema.maximum === "number") {
46
- if (schema.exclusiveMaximum === true) {
47
- r += withMessage(schema, "maximum", ({json}) => [`.lt(${json}`, ", ", ")"]);
48
- } else {
49
- r += withMessage(schema, "maximum", ({json}) => [`.lte(${json}`, ", ", ")"]);
50
- }
51
- } else if (typeof schema.exclusiveMaximum === "number") {
52
- r += withMessage(schema, "exclusiveMaximum", ({json}) => [`.lt(${json}`, ", ", ")"]);
53
- }
54
-
55
- return r;
56
- };