{"version":3,"file":"json-schema.mjs","names":[],"sources":["../../../../../../../@warlock.js/seal/src/standard-schema/json-schema.ts"],"sourcesContent":["import type { StandardJSONSchemaV1 } from \"./types\";\n\n/**\n * Supported JSON Schema generation targets.\n */\nexport type JsonSchemaTarget = StandardJSONSchemaV1.Target;\n\n/**\n * The result shape for a generated JSON Schema.\n */\nexport type JsonSchemaResult = Record<string, unknown>;\n\n/**\n * Apply nullable to a JSON Schema object based on the target dialect.\n *\n * - draft-2020-12  : `type` becomes an array: `[\"string\", \"null\"]`\n * - openai-strict  : same as draft-2020-12 (type array form)\n * - draft-07       : wraps in `oneOf: [{ ...schema }, { type: \"null\" }]`\n * - openapi-3.0    : adds `nullable: true` alongside the existing type\n *\n * Mutates the schema in-place.\n *\n * @example\n * ```ts\n * const schema = { type: \"string\" };\n * applyNullable(schema, \"draft-2020-12\");\n * // → { type: [\"string\", \"null\"] }\n * ```\n */\nexport function applyNullable(schema: JsonSchemaResult, target: JsonSchemaTarget): void {\n  if (target === \"openapi-3.0\") {\n    schema.nullable = true;\n    return;\n  }\n\n  if (target === \"draft-2020-12\" || target === \"openai-strict\") {\n    const baseType = schema.type as string | string[] | undefined;\n    schema.type = Array.isArray(baseType)\n      ? [...baseType, \"null\"]\n      : [baseType as string, \"null\"];\n    return;\n  }\n\n  // draft-07: oneOf wrapping\n  const copy = { ...schema };\n  for (const key of Object.keys(schema)) {\n    delete schema[key];\n  }\n  schema.oneOf = [copy, { type: \"null\" }];\n}\n\n/**\n * Wrap a field schema as nullable for OpenAI strict mode.\n *\n * OpenAI strict requires optional fields to be expressed as a nullable type\n * rather than being omitted from the `required` array. This helper wraps a\n * given schema into its nullable equivalent using the type-array form.\n *\n * Unlike `applyNullable()` (which mutates in-place), this returns a new\n * schema object to avoid side effects when wrapping child schemas.\n *\n * @example\n * ```ts\n * wrapNullableStrict({ type: \"string\", minLength: 3 })\n * // → { type: [\"string\", \"null\"], minLength: 3 }\n *\n * wrapNullableStrict({ type: \"object\", properties: {...} })\n * // → { type: [\"object\", \"null\"], properties: {...} }\n *\n * wrapNullableStrict({ oneOf: [...] })\n * // → { oneOf: [..., { type: \"null\" }] }\n * ```\n */\nexport function wrapNullableStrict(schema: JsonSchemaResult): JsonSchemaResult {\n  // If the schema uses oneOf/anyOf (e.g. union), append null to the list\n  if (schema.oneOf) {\n    return { ...schema, oneOf: [...(schema.oneOf as unknown[]), { type: \"null\" }] };\n  }\n\n  if (schema.anyOf) {\n    return { ...schema, anyOf: [...(schema.anyOf as unknown[]), { type: \"null\" }] };\n  }\n\n  // If there's no type at all (permissive `{}`), stay permissive\n  if (schema.type === undefined) {\n    return schema;\n  }\n\n  // Standard case: wrap type into array with null\n  const baseType = schema.type as string | string[];\n  return {\n    ...schema,\n    type: Array.isArray(baseType) ? [...baseType, \"null\"] : [baseType, \"null\"],\n  };\n}\n\n/**\n * Find the first rule matching the given name in a rules array,\n * and return its options bag.\n */\nexport function getRuleOptions(\n  rules: Array<{ name: string; context: { options: Record<string, unknown> } }>,\n  ruleName: string,\n): Record<string, unknown> | undefined {\n  const rule = rules.find(r => r.name === ruleName);\n  return rule?.context?.options as Record<string, unknown> | undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA6BA,SAAgB,cAAc,QAA0B,QAAgC;CACtF,IAAI,WAAW,eAAe;EAC5B,OAAO,WAAW;EAClB;CACF;CAEA,IAAI,WAAW,mBAAmB,WAAW,iBAAiB;EAC5D,MAAM,WAAW,OAAO;EACxB,OAAO,OAAO,MAAM,QAAQ,QAAQ,IAChC,CAAC,GAAG,UAAU,MAAM,IACpB,CAAC,UAAoB,MAAM;EAC/B;CACF;CAGA,MAAM,OAAO,EAAE,GAAG,OAAO;CACzB,KAAK,MAAM,OAAO,OAAO,KAAK,MAAM,GAClC,OAAO,OAAO;CAEhB,OAAO,QAAQ,CAAC,MAAM,EAAE,MAAM,OAAO,CAAC;AACxC;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,mBAAmB,QAA4C;CAE7E,IAAI,OAAO,OACT,OAAO;EAAE,GAAG;EAAQ,OAAO,CAAC,GAAI,OAAO,OAAqB,EAAE,MAAM,OAAO,CAAC;CAAE;CAGhF,IAAI,OAAO,OACT,OAAO;EAAE,GAAG;EAAQ,OAAO,CAAC,GAAI,OAAO,OAAqB,EAAE,MAAM,OAAO,CAAC;CAAE;CAIhF,IAAI,OAAO,SAAS,QAClB,OAAO;CAIT,MAAM,WAAW,OAAO;CACxB,OAAO;EACL,GAAG;EACH,MAAM,MAAM,QAAQ,QAAQ,IAAI,CAAC,GAAG,UAAU,MAAM,IAAI,CAAC,UAAU,MAAM;CAC3E;AACF;;;;;AAMA,SAAgB,eACd,OACA,UACqC;CAErC,OADa,MAAM,MAAK,MAAK,EAAE,SAAS,QAC9B,CAAC,EAAE,SAAS;AACxB"}