@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.
- package/lib/esm/index.js +0 -26
- package/lib/esm/utils/toZod.js +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/fn/defineResource.d.ts +4 -4
- package/lib/types/index.d.ts +0 -26
- package/package.json +8 -7
- package/src/index.ts +0 -26
- package/src/utils/toZod.ts +1 -2
- package/lib/esm/utils/json-schema-to-zod/Types.js +0 -1
- package/lib/esm/utils/json-schema-to-zod/cli.js +0 -83
- package/lib/esm/utils/json-schema-to-zod/index.js +0 -25
- package/lib/esm/utils/json-schema-to-zod/jsonSchemaToZod.js +0 -52
- package/lib/esm/utils/json-schema-to-zod/parsers/parseAllOf.js +0 -43
- package/lib/esm/utils/json-schema-to-zod/parsers/parseAnyOf.js +0 -17
- package/lib/esm/utils/json-schema-to-zod/parsers/parseArray.js +0 -22
- package/lib/esm/utils/json-schema-to-zod/parsers/parseBoolean.js +0 -9
- package/lib/esm/utils/json-schema-to-zod/parsers/parseConst.js +0 -9
- package/lib/esm/utils/json-schema-to-zod/parsers/parseDefault.js +0 -9
- package/lib/esm/utils/json-schema-to-zod/parsers/parseEnum.js +0 -21
- package/lib/esm/utils/json-schema-to-zod/parsers/parseIfThenElse.js +0 -26
- package/lib/esm/utils/json-schema-to-zod/parsers/parseMultipleType.js +0 -10
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNot.js +0 -13
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNull.js +0 -9
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNullable.js +0 -11
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNumber.js +0 -52
- package/lib/esm/utils/json-schema-to-zod/parsers/parseObject.js +0 -177
- package/lib/esm/utils/json-schema-to-zod/parsers/parseOneOf.js +0 -40
- package/lib/esm/utils/json-schema-to-zod/parsers/parseSchema.js +0 -153
- package/lib/esm/utils/json-schema-to-zod/parsers/parseString.js +0 -63
- package/lib/esm/utils/json-schema-to-zod/utils/cliTools.js +0 -126
- package/lib/esm/utils/json-schema-to-zod/utils/half.js +0 -9
- package/lib/esm/utils/json-schema-to-zod/utils/jsdocs.js +0 -24
- package/lib/esm/utils/json-schema-to-zod/utils/omit.js +0 -12
- package/lib/esm/utils/json-schema-to-zod/utils/withMessage.js +0 -28
- package/lib/types/utils/json-schema-to-zod/Types.d.ts +0 -116
- package/lib/types/utils/json-schema-to-zod/cli.d.ts +0 -2
- package/lib/types/utils/json-schema-to-zod/index.d.ts +0 -25
- package/lib/types/utils/json-schema-to-zod/jsonSchemaToZod.d.ts +0 -11
- package/lib/types/utils/json-schema-to-zod/parsers/parseAllOf.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseAnyOf.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseArray.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseBoolean.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseConst.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseDefault.d.ts +0 -8
- package/lib/types/utils/json-schema-to-zod/parsers/parseEnum.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseIfThenElse.d.ts +0 -12
- package/lib/types/utils/json-schema-to-zod/parsers/parseMultipleType.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseNot.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseNull.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseNullable.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseNumber.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseObject.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseOneOf.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/parsers/parseSchema.d.ts +0 -61
- package/lib/types/utils/json-schema-to-zod/parsers/parseString.d.ts +0 -10
- package/lib/types/utils/json-schema-to-zod/utils/cliTools.d.ts +0 -67
- package/lib/types/utils/json-schema-to-zod/utils/half.d.ts +0 -7
- package/lib/types/utils/json-schema-to-zod/utils/jsdocs.d.ts +0 -15
- package/lib/types/utils/json-schema-to-zod/utils/omit.d.ts +0 -7
- package/lib/types/utils/json-schema-to-zod/utils/withMessage.d.ts +0 -19
- package/src/utils/json-schema-to-zod/Types.ts +0 -123
- package/src/utils/json-schema-to-zod/cli.ts +0 -88
- package/src/utils/json-schema-to-zod/index.ts +0 -26
- package/src/utils/json-schema-to-zod/jsonSchemaToZod.ts +0 -60
- package/src/utils/json-schema-to-zod/parsers/parseAllOf.ts +0 -50
- package/src/utils/json-schema-to-zod/parsers/parseAnyOf.ts +0 -19
- package/src/utils/json-schema-to-zod/parsers/parseArray.ts +0 -28
- package/src/utils/json-schema-to-zod/parsers/parseBoolean.ts +0 -11
- package/src/utils/json-schema-to-zod/parsers/parseConst.ts +0 -11
- package/src/utils/json-schema-to-zod/parsers/parseDefault.ts +0 -11
- package/src/utils/json-schema-to-zod/parsers/parseEnum.ts +0 -20
- package/src/utils/json-schema-to-zod/parsers/parseIfThenElse.ts +0 -35
- package/src/utils/json-schema-to-zod/parsers/parseMultipleType.ts +0 -12
- package/src/utils/json-schema-to-zod/parsers/parseNot.ts +0 -15
- package/src/utils/json-schema-to-zod/parsers/parseNull.ts +0 -11
- package/src/utils/json-schema-to-zod/parsers/parseNullable.ts +0 -13
- package/src/utils/json-schema-to-zod/parsers/parseNumber.ts +0 -56
- package/src/utils/json-schema-to-zod/parsers/parseObject.ts +0 -220
- package/src/utils/json-schema-to-zod/parsers/parseOneOf.ts +0 -44
- package/src/utils/json-schema-to-zod/parsers/parseSchema.ts +0 -190
- package/src/utils/json-schema-to-zod/parsers/parseString.ts +0 -73
- package/src/utils/json-schema-to-zod/utils/cliTools.ts +0 -189
- package/src/utils/json-schema-to-zod/utils/half.ts +0 -9
- package/src/utils/json-schema-to-zod/utils/jsdocs.ts +0 -29
- package/src/utils/json-schema-to-zod/utils/omit.ts +0 -14
- package/src/utils/json-schema-to-zod/utils/withMessage.ts +0 -42
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import {JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
-
import {addJsdocs} from "../utils/jsdocs.js";
|
|
3
|
-
import {parseAllOf} from "./parseAllOf.js";
|
|
4
|
-
import {parseAnyOf} from "./parseAnyOf.js";
|
|
5
|
-
import {parseOneOf} from "./parseOneOf.js";
|
|
6
|
-
import {its, parseSchema} from "./parseSchema.js";
|
|
7
|
-
|
|
8
|
-
// Helper for z.record() generation - Zod v4 requires explicit key type
|
|
9
|
-
function emitRecord(valueSchema: string, refs: Refs): string {
|
|
10
|
-
if (refs.zodVersion === 3) {
|
|
11
|
-
return `z.record(${valueSchema})`;
|
|
12
|
-
}
|
|
13
|
-
// Default to v4 syntax
|
|
14
|
-
return `z.record(z.string(), ${valueSchema})`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Helper for error path in superRefine - Zod v4 uses simplified path
|
|
18
|
-
function emitErrorPath(refs: Refs): string {
|
|
19
|
-
if (refs.zodVersion === 3) {
|
|
20
|
-
return `path: [...ctx.path, key]`;
|
|
21
|
-
}
|
|
22
|
-
// Default to v4 syntax
|
|
23
|
-
return `path: [key]`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Parses object schemas, wiring properties, patternProperties, and catchall behavior into the resulting Zod object.
|
|
28
|
-
*
|
|
29
|
-
* @module platform/mcp
|
|
30
|
-
* @since 8.17.0
|
|
31
|
-
*/
|
|
32
|
-
export function parseObject(objectSchema: JsonSchemaObject & {type: "object"}, refs: Refs): string {
|
|
33
|
-
let properties: string | undefined = undefined;
|
|
34
|
-
|
|
35
|
-
if (objectSchema.properties) {
|
|
36
|
-
if (!Object.keys(objectSchema.properties).length) {
|
|
37
|
-
properties = "z.object({})";
|
|
38
|
-
} else {
|
|
39
|
-
properties = "z.object({ ";
|
|
40
|
-
|
|
41
|
-
properties += Object.keys(objectSchema.properties)
|
|
42
|
-
.map((key) => {
|
|
43
|
-
const propSchema = objectSchema.properties![key];
|
|
44
|
-
|
|
45
|
-
let result = `${JSON.stringify(key)}: ${parseSchema(propSchema, {
|
|
46
|
-
...refs,
|
|
47
|
-
path: [...refs.path, "properties", key]
|
|
48
|
-
})}`;
|
|
49
|
-
|
|
50
|
-
if (refs.withJsdocs && typeof propSchema === "object") {
|
|
51
|
-
result = addJsdocs(propSchema, result);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const hasDefault = typeof propSchema === "object" && propSchema.default !== undefined;
|
|
55
|
-
|
|
56
|
-
const required = Array.isArray(objectSchema.required)
|
|
57
|
-
? objectSchema.required.includes(key)
|
|
58
|
-
: typeof propSchema === "object" && propSchema.required === true;
|
|
59
|
-
|
|
60
|
-
const optional = !hasDefault && !required;
|
|
61
|
-
|
|
62
|
-
return optional ? `${result}.optional()` : result;
|
|
63
|
-
})
|
|
64
|
-
.join(", ");
|
|
65
|
-
|
|
66
|
-
properties += " })";
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const additionalProperties =
|
|
71
|
-
objectSchema.additionalProperties !== undefined
|
|
72
|
-
? parseSchema(objectSchema.additionalProperties, {
|
|
73
|
-
...refs,
|
|
74
|
-
path: [...refs.path, "additionalProperties"]
|
|
75
|
-
})
|
|
76
|
-
: undefined;
|
|
77
|
-
|
|
78
|
-
let patternProperties: string | undefined = undefined;
|
|
79
|
-
|
|
80
|
-
if (objectSchema.patternProperties) {
|
|
81
|
-
const parsedPatternProperties = Object.fromEntries(
|
|
82
|
-
Object.entries(objectSchema.patternProperties).map(([key, value]) => {
|
|
83
|
-
return [
|
|
84
|
-
key,
|
|
85
|
-
parseSchema(value, {
|
|
86
|
-
...refs,
|
|
87
|
-
path: [...refs.path, "patternProperties", key]
|
|
88
|
-
})
|
|
89
|
-
];
|
|
90
|
-
}, {})
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
patternProperties = "";
|
|
94
|
-
|
|
95
|
-
if (properties) {
|
|
96
|
-
if (additionalProperties) {
|
|
97
|
-
patternProperties += `.catchall(z.union([${[...Object.values(parsedPatternProperties), additionalProperties].join(", ")}]))`;
|
|
98
|
-
} else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
99
|
-
patternProperties += `.catchall(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
100
|
-
} else {
|
|
101
|
-
patternProperties += `.catchall(${Object.values(parsedPatternProperties)})`;
|
|
102
|
-
}
|
|
103
|
-
} else {
|
|
104
|
-
if (additionalProperties) {
|
|
105
|
-
patternProperties += emitRecord(`z.union([${[...Object.values(parsedPatternProperties), additionalProperties].join(", ")}])`, refs);
|
|
106
|
-
} else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
107
|
-
patternProperties += emitRecord(`z.union([${Object.values(parsedPatternProperties).join(", ")}])`, refs);
|
|
108
|
-
} else {
|
|
109
|
-
patternProperties += emitRecord(`${Object.values(parsedPatternProperties)}`, refs);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
patternProperties += ".superRefine((value, ctx) => {\n";
|
|
114
|
-
|
|
115
|
-
patternProperties += "for (const key in value) {\n";
|
|
116
|
-
|
|
117
|
-
if (additionalProperties) {
|
|
118
|
-
if (objectSchema.properties) {
|
|
119
|
-
patternProperties += `let evaluated = [${Object.keys(objectSchema.properties)
|
|
120
|
-
.map((key) => JSON.stringify(key))
|
|
121
|
-
.join(", ")}].includes(key)\n`;
|
|
122
|
-
} else {
|
|
123
|
-
patternProperties += `let evaluated = false\n`;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
for (const key in objectSchema.patternProperties) {
|
|
128
|
-
patternProperties += "if (key.match(new RegExp(" + JSON.stringify(key) + "))) {\n";
|
|
129
|
-
if (additionalProperties) {
|
|
130
|
-
patternProperties += "evaluated = true\n";
|
|
131
|
-
}
|
|
132
|
-
patternProperties += "const result = " + parsedPatternProperties[key] + ".safeParse(value[key])\n";
|
|
133
|
-
patternProperties += "if (!result.success) {\n";
|
|
134
|
-
|
|
135
|
-
patternProperties += `ctx.addIssue({
|
|
136
|
-
${emitErrorPath(refs)},
|
|
137
|
-
code: 'custom',
|
|
138
|
-
message: \`Invalid input: Key matching regex /\${key}/ must match schema\`,
|
|
139
|
-
params: {
|
|
140
|
-
issues: result.error.issues
|
|
141
|
-
}
|
|
142
|
-
})\n`;
|
|
143
|
-
|
|
144
|
-
patternProperties += "}\n";
|
|
145
|
-
patternProperties += "}\n";
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (additionalProperties) {
|
|
149
|
-
patternProperties += "if (!evaluated) {\n";
|
|
150
|
-
patternProperties += "const result = " + additionalProperties + ".safeParse(value[key])\n";
|
|
151
|
-
patternProperties += "if (!result.success) {\n";
|
|
152
|
-
|
|
153
|
-
patternProperties += `ctx.addIssue({
|
|
154
|
-
${emitErrorPath(refs)},
|
|
155
|
-
code: 'custom',
|
|
156
|
-
message: \`Invalid input: must match catchall schema\`,
|
|
157
|
-
params: {
|
|
158
|
-
issues: result.error.issues
|
|
159
|
-
}
|
|
160
|
-
})\n`;
|
|
161
|
-
|
|
162
|
-
patternProperties += "}\n";
|
|
163
|
-
patternProperties += "}\n";
|
|
164
|
-
}
|
|
165
|
-
patternProperties += "}\n";
|
|
166
|
-
patternProperties += "})";
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
let output = properties
|
|
170
|
-
? patternProperties
|
|
171
|
-
? properties + patternProperties
|
|
172
|
-
: additionalProperties
|
|
173
|
-
? additionalProperties === "z.never()"
|
|
174
|
-
? properties + ".strict()"
|
|
175
|
-
: properties + `.catchall(${additionalProperties})`
|
|
176
|
-
: properties
|
|
177
|
-
: patternProperties
|
|
178
|
-
? patternProperties
|
|
179
|
-
: additionalProperties
|
|
180
|
-
? emitRecord(additionalProperties, refs)
|
|
181
|
-
: emitRecord("z.any()", refs);
|
|
182
|
-
|
|
183
|
-
if (its.an.anyOf(objectSchema)) {
|
|
184
|
-
output += `.and(${parseAnyOf(
|
|
185
|
-
{
|
|
186
|
-
...objectSchema,
|
|
187
|
-
anyOf: objectSchema.anyOf.map((x) =>
|
|
188
|
-
typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? {...x, type: "object"} : x
|
|
189
|
-
) as any
|
|
190
|
-
},
|
|
191
|
-
refs
|
|
192
|
-
)})`;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (its.a.oneOf(objectSchema)) {
|
|
196
|
-
output += `.and(${parseOneOf(
|
|
197
|
-
{
|
|
198
|
-
...objectSchema,
|
|
199
|
-
oneOf: objectSchema.oneOf.map((x) =>
|
|
200
|
-
typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? {...x, type: "object"} : x
|
|
201
|
-
) as any
|
|
202
|
-
},
|
|
203
|
-
refs
|
|
204
|
-
)})`;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (its.an.allOf(objectSchema)) {
|
|
208
|
-
output += `.and(${parseAllOf(
|
|
209
|
-
{
|
|
210
|
-
...objectSchema,
|
|
211
|
-
allOf: objectSchema.allOf.map((x) =>
|
|
212
|
-
typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? {...x, type: "object"} : x
|
|
213
|
-
) as any
|
|
214
|
-
},
|
|
215
|
-
refs
|
|
216
|
-
)})`;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return output;
|
|
220
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
-
import {parseSchema} from "./parseSchema.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Parses `oneOf` schemas by generating super refinements that ensure exactly one child schema matches.
|
|
6
|
-
*
|
|
7
|
-
* @module platform/mcp
|
|
8
|
-
* @since 8.17.0
|
|
9
|
-
*/
|
|
10
|
-
export const parseOneOf = (schema: JsonSchemaObject & {oneOf: JsonSchema[]}, refs: Refs) => {
|
|
11
|
-
return schema.oneOf.length
|
|
12
|
-
? schema.oneOf.length === 1
|
|
13
|
-
? parseSchema(schema.oneOf[0], {
|
|
14
|
-
...refs,
|
|
15
|
-
path: [...refs.path, "oneOf", 0]
|
|
16
|
-
})
|
|
17
|
-
: `z.any().superRefine((x, ctx) => {
|
|
18
|
-
const schemas = [${schema.oneOf
|
|
19
|
-
.map((schema, i) =>
|
|
20
|
-
parseSchema(schema, {
|
|
21
|
-
...refs,
|
|
22
|
-
path: [...refs.path, "oneOf", i]
|
|
23
|
-
})
|
|
24
|
-
)
|
|
25
|
-
.join(", ")}];
|
|
26
|
-
const errors = schemas.reduce<z.ZodError[]>(
|
|
27
|
-
(errors, schema) =>
|
|
28
|
-
((result) =>
|
|
29
|
-
result.error ? [...errors, result.error] : errors)(
|
|
30
|
-
schema.safeParse(x),
|
|
31
|
-
),
|
|
32
|
-
[],
|
|
33
|
-
);
|
|
34
|
-
if (schemas.length - errors.length !== 1) {
|
|
35
|
-
ctx.addIssue({
|
|
36
|
-
path: ctx.path,
|
|
37
|
-
code: "invalid_union",
|
|
38
|
-
unionErrors: errors,
|
|
39
|
-
message: "Invalid input: Should pass single schema",
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
})`
|
|
43
|
-
: "z.any()";
|
|
44
|
-
};
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import {JsonSchema, JsonSchemaObject, ParserSelector, Refs, Serializable} from "../Types.js";
|
|
2
|
-
import {parseAllOf} from "./parseAllOf.js";
|
|
3
|
-
import {parseAnyOf} from "./parseAnyOf.js";
|
|
4
|
-
import {parseArray} from "./parseArray.js";
|
|
5
|
-
import {parseBoolean} from "./parseBoolean.js";
|
|
6
|
-
import {parseConst} from "./parseConst.js";
|
|
7
|
-
import {parseDefault} from "./parseDefault.js";
|
|
8
|
-
import {parseEnum} from "./parseEnum.js";
|
|
9
|
-
import {parseIfThenElse} from "./parseIfThenElse.js";
|
|
10
|
-
import {parseMultipleType} from "./parseMultipleType.js";
|
|
11
|
-
import {parseNot} from "./parseNot.js";
|
|
12
|
-
import {parseNull} from "./parseNull.js";
|
|
13
|
-
import {parseNullable} from "./parseNullable.js";
|
|
14
|
-
import {parseNumber} from "./parseNumber.js";
|
|
15
|
-
import {parseObject} from "./parseObject.js";
|
|
16
|
-
import {parseOneOf} from "./parseOneOf.js";
|
|
17
|
-
import {parseString} from "./parseString.js";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Recursively parses a JSON Schema node into a Zod expression string.
|
|
21
|
-
*
|
|
22
|
-
* @param schema JSON Schema node to inspect.
|
|
23
|
-
* @param refs Shared parsing context and options.
|
|
24
|
-
* @param blockMeta When true, skips describe/default annotations for nested invocations.
|
|
25
|
-
* @module platform/mcp
|
|
26
|
-
* @since 8.17.0
|
|
27
|
-
*/
|
|
28
|
-
export const parseSchema = (schema: JsonSchema, refs: Refs = {seen: new Map(), path: []}, blockMeta?: boolean): string => {
|
|
29
|
-
if (typeof schema !== "object") return schema ? "z.any()" : "z.never()";
|
|
30
|
-
|
|
31
|
-
if (refs.parserOverride) {
|
|
32
|
-
const custom = refs.parserOverride(schema, refs);
|
|
33
|
-
|
|
34
|
-
if (typeof custom === "string") {
|
|
35
|
-
return custom;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let seen = refs.seen.get(schema);
|
|
40
|
-
|
|
41
|
-
if (seen) {
|
|
42
|
-
if (seen.r !== undefined) {
|
|
43
|
-
return seen.r;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (refs.depth === undefined || seen.n >= refs.depth) {
|
|
47
|
-
return "z.any()";
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
seen.n += 1;
|
|
51
|
-
} else {
|
|
52
|
-
seen = {r: undefined, n: 0};
|
|
53
|
-
refs.seen.set(schema, seen);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let parsed = selectParser(schema, refs);
|
|
57
|
-
if (!blockMeta) {
|
|
58
|
-
if (!refs.withoutDescribes) {
|
|
59
|
-
parsed = addDescribes(schema, parsed);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (!refs.withoutDefaults) {
|
|
63
|
-
parsed = addDefaults(schema, parsed);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
parsed = addAnnotations(schema, parsed);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
seen.r = parsed;
|
|
70
|
-
|
|
71
|
-
return parsed;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const addDescribes = (schema: JsonSchemaObject, parsed: string): string => {
|
|
75
|
-
if (schema.description) {
|
|
76
|
-
parsed += `.describe(${JSON.stringify(schema.description)})`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return parsed;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const addDefaults = (schema: JsonSchemaObject, parsed: string): string => {
|
|
83
|
-
if (schema.default !== undefined) {
|
|
84
|
-
parsed += `.default(${JSON.stringify(schema.default)})`;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return parsed;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const addAnnotations = (schema: JsonSchemaObject, parsed: string): string => {
|
|
91
|
-
if (schema.readOnly) {
|
|
92
|
-
parsed += ".readonly()";
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return parsed;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const selectParser: ParserSelector = (schema, refs) => {
|
|
99
|
-
if (its.a.nullable(schema)) {
|
|
100
|
-
return parseNullable(schema, refs);
|
|
101
|
-
} else if (its.an.object(schema)) {
|
|
102
|
-
return parseObject(schema, refs);
|
|
103
|
-
} else if (its.an.array(schema)) {
|
|
104
|
-
return parseArray(schema, refs);
|
|
105
|
-
} else if (its.an.anyOf(schema)) {
|
|
106
|
-
return parseAnyOf(schema, refs);
|
|
107
|
-
} else if (its.an.allOf(schema)) {
|
|
108
|
-
return parseAllOf(schema, refs);
|
|
109
|
-
} else if (its.a.oneOf(schema)) {
|
|
110
|
-
return parseOneOf(schema, refs);
|
|
111
|
-
} else if (its.a.not(schema)) {
|
|
112
|
-
return parseNot(schema, refs);
|
|
113
|
-
} else if (its.an.enum(schema)) {
|
|
114
|
-
return parseEnum(schema); //<-- needs to come before primitives
|
|
115
|
-
} else if (its.a.const(schema)) {
|
|
116
|
-
return parseConst(schema);
|
|
117
|
-
} else if (its.a.multipleType(schema)) {
|
|
118
|
-
return parseMultipleType(schema, refs);
|
|
119
|
-
} else if (its.a.primitive(schema, "string")) {
|
|
120
|
-
return parseString(schema);
|
|
121
|
-
} else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) {
|
|
122
|
-
return parseNumber(schema);
|
|
123
|
-
} else if (its.a.primitive(schema, "boolean")) {
|
|
124
|
-
return parseBoolean(schema);
|
|
125
|
-
} else if (its.a.primitive(schema, "null")) {
|
|
126
|
-
return parseNull(schema);
|
|
127
|
-
} else if (its.a.conditional(schema)) {
|
|
128
|
-
return parseIfThenElse(schema, refs);
|
|
129
|
-
} else {
|
|
130
|
-
return parseDefault(schema);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Type guards used by the schema parser to detect structural patterns.
|
|
136
|
-
*
|
|
137
|
-
* @module platform/mcp
|
|
138
|
-
* @since 8.17.0
|
|
139
|
-
*/
|
|
140
|
-
export const its = {
|
|
141
|
-
an: {
|
|
142
|
-
object: (x: JsonSchemaObject): x is JsonSchemaObject & {type: "object"} => x.type === "object",
|
|
143
|
-
array: (x: JsonSchemaObject): x is JsonSchemaObject & {type: "array"} => x.type === "array",
|
|
144
|
-
anyOf: (
|
|
145
|
-
x: JsonSchemaObject
|
|
146
|
-
): x is JsonSchemaObject & {
|
|
147
|
-
anyOf: JsonSchema[];
|
|
148
|
-
} => x.anyOf !== undefined,
|
|
149
|
-
allOf: (
|
|
150
|
-
x: JsonSchemaObject
|
|
151
|
-
): x is JsonSchemaObject & {
|
|
152
|
-
allOf: JsonSchema[];
|
|
153
|
-
} => x.allOf !== undefined,
|
|
154
|
-
enum: (
|
|
155
|
-
x: JsonSchemaObject
|
|
156
|
-
): x is JsonSchemaObject & {
|
|
157
|
-
enum: Serializable | Serializable[];
|
|
158
|
-
} => x.enum !== undefined
|
|
159
|
-
},
|
|
160
|
-
a: {
|
|
161
|
-
nullable: (x: JsonSchemaObject): x is JsonSchemaObject & {nullable: true} => (x as any).nullable === true,
|
|
162
|
-
multipleType: (x: JsonSchemaObject): x is JsonSchemaObject & {type: string[]} => Array.isArray(x.type),
|
|
163
|
-
not: (
|
|
164
|
-
x: JsonSchemaObject
|
|
165
|
-
): x is JsonSchemaObject & {
|
|
166
|
-
not: JsonSchema;
|
|
167
|
-
} => x.not !== undefined,
|
|
168
|
-
const: (
|
|
169
|
-
x: JsonSchemaObject
|
|
170
|
-
): x is JsonSchemaObject & {
|
|
171
|
-
const: Serializable;
|
|
172
|
-
} => x.const !== undefined,
|
|
173
|
-
primitive: <T extends "string" | "number" | "integer" | "boolean" | "null">(
|
|
174
|
-
x: JsonSchemaObject,
|
|
175
|
-
p: T
|
|
176
|
-
): x is JsonSchemaObject & {type: T} => x.type === p,
|
|
177
|
-
conditional: (
|
|
178
|
-
x: JsonSchemaObject
|
|
179
|
-
): x is JsonSchemaObject & {
|
|
180
|
-
if: JsonSchema;
|
|
181
|
-
then: JsonSchema;
|
|
182
|
-
else: JsonSchema;
|
|
183
|
-
} => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
|
|
184
|
-
oneOf: (
|
|
185
|
-
x: JsonSchemaObject
|
|
186
|
-
): x is JsonSchemaObject & {
|
|
187
|
-
oneOf: JsonSchema[];
|
|
188
|
-
} => x.oneOf !== undefined
|
|
189
|
-
}
|
|
190
|
-
};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import {JsonSchemaObject} from "../Types.js";
|
|
2
|
-
import {withMessage} from "../utils/withMessage.js";
|
|
3
|
-
import {parseSchema} from "./parseSchema.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Parses string schemas, supporting format-specific helpers, regex constraints, and content media transformations.
|
|
7
|
-
*
|
|
8
|
-
* @module platform/mcp
|
|
9
|
-
* @since 8.17.0
|
|
10
|
-
*/
|
|
11
|
-
export const parseString = (schema: JsonSchemaObject & {type: "string"}) => {
|
|
12
|
-
let r = "z.string()";
|
|
13
|
-
|
|
14
|
-
r += withMessage(schema, "format", ({value}) => {
|
|
15
|
-
switch (value) {
|
|
16
|
-
case "email":
|
|
17
|
-
return [".email(", ")"];
|
|
18
|
-
case "ip":
|
|
19
|
-
return [".ip(", ")"];
|
|
20
|
-
case "ipv4":
|
|
21
|
-
return ['.ip({ version: "v4"', ", message: ", " })"];
|
|
22
|
-
case "ipv6":
|
|
23
|
-
return ['.ip({ version: "v6"', ", message: ", " })"];
|
|
24
|
-
case "uri":
|
|
25
|
-
return [".url(", ")"];
|
|
26
|
-
case "uuid":
|
|
27
|
-
return [".uuid(", ")"];
|
|
28
|
-
case "date-time":
|
|
29
|
-
return [".datetime({ offset: true", ", message: ", " })"];
|
|
30
|
-
case "time":
|
|
31
|
-
return [".time(", ")"];
|
|
32
|
-
case "date":
|
|
33
|
-
return [".date(", ")"];
|
|
34
|
-
case "binary":
|
|
35
|
-
return [".base64(", ")"];
|
|
36
|
-
case "duration":
|
|
37
|
-
return [".duration(", ")"];
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
r += withMessage(schema, "pattern", ({json}) => [`.regex(new RegExp(${json})`, ", ", ")"]);
|
|
42
|
-
|
|
43
|
-
r += withMessage(schema, "minLength", ({json}) => [`.min(${json}`, ", ", ")"]);
|
|
44
|
-
|
|
45
|
-
r += withMessage(schema, "maxLength", ({json}) => [`.max(${json}`, ", ", ")"]);
|
|
46
|
-
|
|
47
|
-
r += withMessage(schema, "contentEncoding", ({value}) => {
|
|
48
|
-
if (value === "base64") {
|
|
49
|
-
return [".base64(", ")"];
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const contentMediaType = withMessage(schema, "contentMediaType", ({value}) => {
|
|
54
|
-
if (value === "application/json") {
|
|
55
|
-
return [
|
|
56
|
-
'.transform((str, ctx) => { try { return JSON.parse(str); } catch (err) { ctx.addIssue({ code: "custom", message: "Invalid JSON" }); }}',
|
|
57
|
-
", ",
|
|
58
|
-
")"
|
|
59
|
-
];
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
if (contentMediaType != "") {
|
|
64
|
-
r += contentMediaType;
|
|
65
|
-
r += withMessage(schema, "contentSchema", ({value}) => {
|
|
66
|
-
if (value && value instanceof Object) {
|
|
67
|
-
return [`.pipe(${parseSchema(value)}`, ", ", ")"];
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return r;
|
|
73
|
-
};
|