-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Hi!
First of all, thank you for this great library.
I am using additional custom metadata fields in my JSON Schemas for purposes such as documentation generation, form rendering, and other tooling. Zod currently supports storing custom metadata via metadata registries (see: Zod - Metadata and registries).
Problem
When converting JSON Schema → Zod, the generated Zod schema only preserves the description field.
All other non-standard JSON Schema keys—such as custom documentation fields, UI hints, or domain-specific metadata—are currently discarded.
Example
{ "type": "string", "description": "User name", "x-ui-label": "Name", "x-form-position": 1}Expected Zod output:
z.string().meta({ "description": "User name", "x-ui-label": "Name", "x-form-position": 1})Suggestion
Adapt MetadataHandler:
const JSON_SCHEMA_KEYS = [
'$schema',
'$id',
'$anchor',
'$ref',
'$dynamicRef',
'$dynamicAnchor',
'$vocabulary',
'$comment',
'$defs',
'type',
'additionalItems',
'unevaluatedItems',
'prefixItems',
'items',
'contains',
'additionalProperties',
'unevaluatedProperties',
'properties',
'patternProperties',
'dependentSchemas',
'propertyNames',
'if',
'then',
'else',
'allOf',
'anyOf',
'oneOf',
'not',
'multipleOf',
'maximum',
'exclusiveMaximum',
'minimum',
'exclusiveMinimum',
'maxLength',
'minLength',
'pattern',
'maxItems',
'minItems',
'uniqueItems',
'maxContains',
'minContains',
'maxProperties',
'minProperties',
'required',
'dependentRequired',
'enum',
'const'
];
const JSON_SCHEMA_KEY_SET = new Set(JSON_SCHEMA_KEYS);
function isStandardKey(key) {
return JSON_SCHEMA_KEY_SET.has(key);
}
var MetadataHandler = class {
apply(zodSchema, schema) {
const meta = {};
for (const [k, v] of Object.entries(schema)) {
if (!isStandardKey(k)) {
meta[k] = v;
}
}
if (Object.keys(meta).length > 0) {
zodSchema = zodSchema.meta(meta);
}
return zodSchema;
}
};Alternatively, you could provide a way to pass custom refinement handlers, allowing me to implement this logic myself.
Thanks!
Thanks in advance for considering this enhancement. It would significantly improve interoperability for projects working with richer JSON Schemas.