{"version":3,"file":"export-json-schema.mjs","names":[],"sources":["../../../src/common/schema/export-json-schema.ts"],"sourcesContent":["import type { Type } from './schema'\nimport { isEmpty } from '../data'\nimport { objectMap } from '../data/object'\n\nconst _mapJsonSchemaType: Record<string, string> = {\n  string: 'string',\n  number: 'number',\n  boolean: 'boolean',\n  int: 'integer',\n}\n\nexport function schemaExportJsonSchema<T>(schema: Type<T>): Record<string, any> {\n  // assert(isSchemaObjectFlat(schema), 'schema should be a flat object')\n\n  function transformSchema(schema: Type<any>): any {\n    const type = _mapJsonSchemaType[schema.type] ?? schema.type ?? 'object'\n    const properties: Record<string, any> = {}\n    const required: string[] = []\n\n    objectMap(schema._object!, (key, schema: any) => {\n      const type = _mapJsonSchemaType[schema.type] ?? schema.type\n      properties[key] = { type }\n      const enumValues = (schema as any)._enumValues\n      if (enumValues) {\n        properties[key].enum = enumValues\n      }\n      if (schema._default !== undefined) {\n        properties[key].default = schema._default\n      }\n      if (schema._meta?.desc) {\n        properties[key].description = schema._meta.desc\n      }\n      if (schema._optional !== true) {\n        required.push(key)\n      }\n      if (schema.type === 'array' && schema._type) {\n        properties[key].items = transformSchema(schema._type)\n      }\n      else if (schema.type === 'object' && schema._object) {\n        Object.assign(properties[key], transformSchema(schema))\n        properties[key].additionalProperties = false\n      }\n      else if (schema.type === 'record' && schema._type) {\n        properties[key].type = 'object'\n        properties[key].additionalProperties = transformSchema(schema._type)\n      }\n      // Handle union types (e.g., z.union)\n      else if (schema.type === 'union' && Array.isArray(schema._union)) {\n        properties[key].type = schema._union.map((s: any) => _mapJsonSchemaType[s.type] ?? s.type) // todo complex types\n      }\n    })\n\n    if (!isEmpty(properties)) {\n      return {\n        type,\n        properties,\n        additionalProperties: false,\n        ...(required.length > 0 ? { required } : {}),\n      }\n    }\n\n    return {\n      type,\n    }\n  }\n\n  return {\n    $schema: 'http://json-schema.org/draft-07/schema#',\n    additionalProperties: false,\n    ...transformSchema(schema),\n  }\n}\n\nexport function schemaExportJsonSchemaString<T>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