(schema: JSONSchema7)
| 195 | * Converts boolean const values to enum. |
| 196 | */ |
| 197 | export function postProcessSchema(schema: JSONSchema7): JSONSchema7 { |
| 198 | if (typeof schema !== "object" || schema === null) return schema; |
| 199 | |
| 200 | const processed = { ...schema } as JSONSchema7WithDefs; |
| 201 | |
| 202 | if ("const" in processed && typeof processed.const === "boolean") { |
| 203 | processed.enum = [processed.const]; |
| 204 | delete processed.const; |
| 205 | } |
| 206 | |
| 207 | if (processed.properties) { |
| 208 | const newProps: Record<string, JSONSchema7Definition> = {}; |
| 209 | for (const [key, value] of Object.entries(processed.properties).sort(([a], [b]) => a.localeCompare(b))) { |
| 210 | newProps[key] = typeof value === "object" ? postProcessSchema(value as JSONSchema7) : value; |
| 211 | } |
| 212 | processed.properties = newProps; |
| 213 | } |
| 214 | |
| 215 | if (processed.items) { |
| 216 | if (typeof processed.items === "object" && !Array.isArray(processed.items)) { |
| 217 | processed.items = postProcessSchema(processed.items as JSONSchema7); |
| 218 | } else if (Array.isArray(processed.items)) { |
| 219 | processed.items = processed.items.map((item) => |
| 220 | typeof item === "object" ? postProcessSchema(item as JSONSchema7) : item |
| 221 | ) as JSONSchema7Definition[]; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | for (const combiner of ["anyOf", "allOf", "oneOf"] as const) { |
| 226 | if (processed[combiner]) { |
| 227 | processed[combiner] = processed[combiner]!.map((item) => |
| 228 | typeof item === "object" ? postProcessSchema(item as JSONSchema7) : item |
| 229 | ) as JSONSchema7Definition[]; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | const { definitions, $defs } = collectDefinitionCollections(processed as Record<string, unknown>); |
| 234 | let newDefs: Record<string, JSONSchema7Definition> | undefined; |
| 235 | if (Object.keys(definitions).length > 0) { |
| 236 | newDefs = {}; |
| 237 | for (const [key, value] of Object.entries(definitions)) { |
| 238 | newDefs[key] = typeof value === "object" ? postProcessSchema(value as JSONSchema7) : value; |
| 239 | } |
| 240 | processed.definitions = newDefs; |
| 241 | } |
| 242 | let newDraftDefs: Record<string, JSONSchema7Definition> | undefined; |
| 243 | if (Object.keys($defs).length > 0) { |
| 244 | newDraftDefs = {}; |
| 245 | for (const [key, value] of Object.entries($defs)) { |
| 246 | newDraftDefs[key] = typeof value === "object" ? postProcessSchema(value as JSONSchema7) : value; |
| 247 | } |
| 248 | processed.$defs = newDraftDefs; |
| 249 | } |
| 250 | if (processed.definitions && !processed.$defs) { |
| 251 | processed.$defs = { ...(newDefs ?? processed.definitions) }; |
| 252 | } else if (processed.$defs && !processed.definitions) { |
| 253 | processed.definitions = { ...processed.$defs }; |
| 254 | } |
no test coverage detected
searching dependent graphs…