(schema: JsonSchemaType)
| 235 | * @returns A normalized schema or the original schema |
| 236 | */ |
| 237 | export function normalizeUnionType(schema: JsonSchemaType): JsonSchemaType { |
| 238 | // Handle anyOf with exactly 2 items (type and null) - unified handling |
| 239 | // Preserves enum and other properties automatically |
| 240 | if ( |
| 241 | schema.anyOf && |
| 242 | schema.anyOf.length === 2 && |
| 243 | schema.anyOf.some((t) => (t as JsonSchemaType).type === "null") |
| 244 | ) { |
| 245 | const nonNullItem = schema.anyOf.find((t) => { |
| 246 | const item = t as JsonSchemaType; |
| 247 | return item?.type !== "null"; |
| 248 | }) as JsonSchemaType; |
| 249 | |
| 250 | // Only process if non-null item has type or enum |
| 251 | if (nonNullItem?.type || nonNullItem?.enum) { |
| 252 | return { |
| 253 | ...schema, |
| 254 | ...nonNullItem, |
| 255 | type: nonNullItem?.type || (nonNullItem?.enum ? "string" : undefined), |
| 256 | nullable: true, |
| 257 | anyOf: undefined, |
| 258 | }; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // Handle array type with exactly string and null |
| 263 | if ( |
| 264 | Array.isArray(schema.type) && |
| 265 | schema.type.length === 2 && |
| 266 | schema.type.includes("string") && |
| 267 | schema.type.includes("null") |
| 268 | ) { |
| 269 | return { ...schema, type: "string", nullable: true }; |
| 270 | } |
| 271 | |
| 272 | // Handle array type with exactly boolean and null |
| 273 | if ( |
| 274 | Array.isArray(schema.type) && |
| 275 | schema.type.length === 2 && |
| 276 | schema.type.includes("boolean") && |
| 277 | schema.type.includes("null") |
| 278 | ) { |
| 279 | return { ...schema, type: "boolean", nullable: true }; |
| 280 | } |
| 281 | |
| 282 | // Handle array type with exactly number and null |
| 283 | if ( |
| 284 | Array.isArray(schema.type) && |
| 285 | schema.type.length === 2 && |
| 286 | schema.type.includes("number") && |
| 287 | schema.type.includes("null") |
| 288 | ) { |
| 289 | return { ...schema, type: "number", nullable: true }; |
| 290 | } |
| 291 | |
| 292 | // Handle array type with exactly integer and null |
| 293 | if ( |
| 294 | Array.isArray(schema.type) && |
no outgoing calls
no test coverage detected