@composio/json-schema-to-zod 0.2.2 → 0.3.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/dist/index.mjs +75 -53
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -309,10 +309,21 @@ const parseOneOf = (jsonSchema, refs) => {
|
|
|
309
309
|
|
|
310
310
|
//#endregion
|
|
311
311
|
//#region src/parsers/parse-object.ts
|
|
312
|
+
/**
|
|
313
|
+
* Build the static part of an object parser, or `undefined` when the schema
|
|
314
|
+
* declares no named properties.
|
|
315
|
+
*
|
|
316
|
+
* A property-less object (`properties` absent or `{}`) deliberately returns
|
|
317
|
+
* `undefined` so the caller can apply the open-object policy. This supersedes
|
|
318
|
+
* an earlier `z.object({})` fallback that was kept "or else this will break
|
|
319
|
+
* openai responses": collapsing `{ type: "object" }` to a strict empty object
|
|
320
|
+
* is the defect it was masking, and the OpenAI provider suite is part of this
|
|
321
|
+
* package's verification precisely so the claim stays checked.
|
|
322
|
+
*/
|
|
312
323
|
function parseObjectProperties(objectSchema, refs) {
|
|
313
|
-
if (!objectSchema.properties) return
|
|
324
|
+
if (!objectSchema.properties) return;
|
|
314
325
|
const propertyKeys = Object.keys(objectSchema.properties);
|
|
315
|
-
if (propertyKeys.length === 0) return
|
|
326
|
+
if (propertyKeys.length === 0) return;
|
|
316
327
|
const properties = {};
|
|
317
328
|
for (const key of propertyKeys) {
|
|
318
329
|
const propJsonSchema = objectSchema.properties[key];
|
|
@@ -336,6 +347,66 @@ function parseObjectProperties(objectSchema, refs) {
|
|
|
336
347
|
}
|
|
337
348
|
return z$1.object(properties);
|
|
338
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Object parser for schemas carrying `patternProperties`.
|
|
352
|
+
*
|
|
353
|
+
* Every key is routed to exactly the schemas that apply to it — each matching
|
|
354
|
+
* pattern, and `additionalProperties` only when no pattern and no named
|
|
355
|
+
* property claims the key. A catch-all union would instead let a key sneak past
|
|
356
|
+
* its own pattern by satisfying an unrelated one.
|
|
357
|
+
*/
|
|
358
|
+
function parseDynamicKeyObject(normalizedSchema, propertiesSchema, additionalProperties, refs) {
|
|
359
|
+
const patterns = Object.entries(normalizedSchema.patternProperties ?? {}).map(([pattern, schema]) => ({
|
|
360
|
+
regex: new RegExp(pattern),
|
|
361
|
+
parser: parseSchema(schema, {
|
|
362
|
+
...refs,
|
|
363
|
+
path: [
|
|
364
|
+
...refs.path,
|
|
365
|
+
"patternProperties",
|
|
366
|
+
pattern
|
|
367
|
+
]
|
|
368
|
+
})
|
|
369
|
+
}));
|
|
370
|
+
const declaredKeys = new Set(Object.keys(normalizedSchema.properties ?? {}));
|
|
371
|
+
const rejectsUnmatched = additionalProperties instanceof z$1.ZodNever || additionalProperties === void 0 && declaredKeys.size > 0;
|
|
372
|
+
const validatesUnmatched = additionalProperties !== void 0 && !(additionalProperties instanceof z$1.ZodNever) && normalizedSchema.additionalProperties !== true;
|
|
373
|
+
return (propertiesSchema ?? z$1.object({})).passthrough().transform((value, ctx) => {
|
|
374
|
+
const result = { ...value };
|
|
375
|
+
const unrecognizedKeys = [];
|
|
376
|
+
for (const key of Object.keys(value)) {
|
|
377
|
+
const matching = patterns.filter(({ regex }) => regex.test(key));
|
|
378
|
+
for (const { parser } of matching) {
|
|
379
|
+
const parsed = parser.safeParse(value[key]);
|
|
380
|
+
if (!parsed.success) ctx.addIssue({
|
|
381
|
+
path: [...ctx.path, key],
|
|
382
|
+
code: "custom",
|
|
383
|
+
message: `Invalid input: key "${key}" must match its patternProperties schema`,
|
|
384
|
+
params: { issues: parsed.error.issues }
|
|
385
|
+
});
|
|
386
|
+
else if (!declaredKeys.has(key)) result[key] = parsed.data;
|
|
387
|
+
}
|
|
388
|
+
if (matching.length > 0 || declaredKeys.has(key)) continue;
|
|
389
|
+
if (rejectsUnmatched) unrecognizedKeys.push(key);
|
|
390
|
+
else if (validatesUnmatched) {
|
|
391
|
+
const parsed = additionalProperties.safeParse(value[key]);
|
|
392
|
+
if (!parsed.success) ctx.addIssue({
|
|
393
|
+
path: [...ctx.path, key],
|
|
394
|
+
code: "custom",
|
|
395
|
+
message: `Invalid input: key "${key}" must match the additionalProperties schema`,
|
|
396
|
+
params: { issues: parsed.error.issues }
|
|
397
|
+
});
|
|
398
|
+
else result[key] = parsed.data;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (unrecognizedKeys.length > 0) ctx.addIssue({
|
|
402
|
+
path: [...ctx.path],
|
|
403
|
+
code: "unrecognized_keys",
|
|
404
|
+
keys: unrecognizedKeys,
|
|
405
|
+
message: `Unrecognized key(s) in object: ${unrecognizedKeys.map((key) => `'${key}'`).join(", ")}`
|
|
406
|
+
});
|
|
407
|
+
return result;
|
|
408
|
+
});
|
|
409
|
+
}
|
|
339
410
|
function parseObject(objectSchema, refs) {
|
|
340
411
|
const hasPatternProperties = Object.keys(objectSchema.patternProperties ?? {}).length > 0;
|
|
341
412
|
const normalizedSchema = objectSchema.type === "object" ? objectSchema : {
|
|
@@ -343,66 +414,17 @@ function parseObject(objectSchema, refs) {
|
|
|
343
414
|
type: "object"
|
|
344
415
|
};
|
|
345
416
|
const propertiesSchema = parseObjectProperties(normalizedSchema, refs);
|
|
346
|
-
let zodSchema = propertiesSchema;
|
|
347
417
|
const additionalProperties = normalizedSchema.additionalProperties !== void 0 ? parseSchema(normalizedSchema.additionalProperties, {
|
|
348
418
|
...refs,
|
|
349
419
|
path: [...refs.path, "additionalProperties"]
|
|
350
420
|
}) : void 0;
|
|
351
421
|
const isAdditionalPropertiesTrue = normalizedSchema.additionalProperties === true;
|
|
352
|
-
if (normalizedSchema.patternProperties) {
|
|
353
|
-
const parsedPatternProperties = Object.fromEntries(Object.entries(normalizedSchema.patternProperties).map(([key, value]) => {
|
|
354
|
-
return [key, parseSchema(value, {
|
|
355
|
-
...refs,
|
|
356
|
-
path: [
|
|
357
|
-
...refs.path,
|
|
358
|
-
"patternProperties",
|
|
359
|
-
key
|
|
360
|
-
]
|
|
361
|
-
})];
|
|
362
|
-
}));
|
|
363
|
-
const patternPropertyValues = Object.values(parsedPatternProperties);
|
|
364
|
-
if (propertiesSchema) if (additionalProperties) zodSchema = propertiesSchema.catchall(z$1.union([...patternPropertyValues, additionalProperties]));
|
|
365
|
-
else if (Object.keys(parsedPatternProperties).length > 1) zodSchema = propertiesSchema.catchall(z$1.union(patternPropertyValues));
|
|
366
|
-
else zodSchema = propertiesSchema.catchall(patternPropertyValues[0]);
|
|
367
|
-
else if (additionalProperties) zodSchema = z$1.record(z$1.union([...patternPropertyValues, additionalProperties]));
|
|
368
|
-
else if (patternPropertyValues.length > 1) zodSchema = z$1.record(z$1.union(patternPropertyValues));
|
|
369
|
-
else zodSchema = z$1.record(patternPropertyValues[0]);
|
|
370
|
-
const objectPropertyKeys = new Set(Object.keys(normalizedSchema.properties ?? {}));
|
|
371
|
-
zodSchema = zodSchema.superRefine((value, ctx) => {
|
|
372
|
-
for (const key in value) {
|
|
373
|
-
let wasMatched = objectPropertyKeys.has(key);
|
|
374
|
-
for (const patternPropertyKey in normalizedSchema.patternProperties) {
|
|
375
|
-
const regex = new RegExp(patternPropertyKey);
|
|
376
|
-
if (key.match(regex)) {
|
|
377
|
-
wasMatched = true;
|
|
378
|
-
const result = parsedPatternProperties[patternPropertyKey].safeParse(value[key]);
|
|
379
|
-
if (!result.success) ctx.addIssue({
|
|
380
|
-
path: [...ctx.path, key],
|
|
381
|
-
code: "custom",
|
|
382
|
-
message: `Invalid input: Key matching regex /${key}/ must match schema`,
|
|
383
|
-
params: { issues: result.error.issues }
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
if (!wasMatched && additionalProperties) {
|
|
388
|
-
const result = additionalProperties.safeParse(value[key]);
|
|
389
|
-
if (!result.success) ctx.addIssue({
|
|
390
|
-
path: [...ctx.path, key],
|
|
391
|
-
code: "custom",
|
|
392
|
-
message: "Invalid input: must match catchall schema",
|
|
393
|
-
params: { issues: result.error.issues }
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
422
|
let output;
|
|
400
|
-
if (
|
|
401
|
-
else if (additionalProperties) if (additionalProperties instanceof z$1.ZodNever) output = propertiesSchema.strict();
|
|
423
|
+
if (hasPatternProperties) output = parseDynamicKeyObject(normalizedSchema, propertiesSchema, additionalProperties, refs);
|
|
424
|
+
else if (propertiesSchema) if (additionalProperties) if (additionalProperties instanceof z$1.ZodNever) output = propertiesSchema.strict();
|
|
402
425
|
else if (isAdditionalPropertiesTrue) output = propertiesSchema.passthrough();
|
|
403
426
|
else output = propertiesSchema.catchall(additionalProperties);
|
|
404
427
|
else output = propertiesSchema.strict();
|
|
405
|
-
else if (hasPatternProperties) output = zodSchema;
|
|
406
428
|
else if (additionalProperties) if (additionalProperties instanceof z$1.ZodNever) output = z$1.object({}).strict();
|
|
407
429
|
else if (isAdditionalPropertiesTrue) output = z$1.object({}).passthrough();
|
|
408
430
|
else output = z$1.record(additionalProperties);
|