| 80 | * In redacting mode, rejects annotations that could expose redacted values. |
| 81 | */ |
| 82 | const exampleOf = ( |
| 83 | root: Json, |
| 84 | definitions: Record<string, Json>, |
| 85 | redacting = false |
| 86 | ): Json | undefined => { |
| 87 | const open = new Set<string>() |
| 88 | let unmasked = false |
| 89 | const masks = (node: Json | undefined, seen: Set<Json>): boolean => { |
| 90 | const schema = resolve(node, definitions) |
| 91 | if (typeof schema !== "object" || schema === null || seen.has(schema)) { |
| 92 | return false |
| 93 | } |
| 94 | seen.add(schema) |
| 95 | return isPlaceholderSchema(schema) || |
| 96 | Object.values(schema).some((child) => masks(child, seen)) |
| 97 | } |
| 98 | const walk = (schema: Json): Json | undefined => { |
| 99 | if (!isObject(schema)) return undefined |
| 100 | if (typeof schema.$ref === "string") { |
| 101 | const target = resolve(schema, definitions) |
| 102 | if (open.has(schema.$ref) || target === undefined) return undefined |
| 103 | open.add(schema.$ref) |
| 104 | const out = walk(target) |
| 105 | open.delete(schema.$ref) |
| 106 | return out |
| 107 | } |
| 108 | if ("const" in schema) return schema.const |
| 109 | if (Array.isArray(schema.enum)) return schema.enum[0] |
| 110 | const annotated = Array.isArray(schema.examples) && |
| 111 | schema.examples.length > 0 |
| 112 | if (annotated || "default" in schema) { |
| 113 | if (redacting && masks(schema, new Set())) unmasked = true |
| 114 | return annotated ? (schema.examples as Array<Json>)[0] : schema.default |
| 115 | } |
| 116 | const members = schema.anyOf ?? schema.oneOf |
| 117 | if (Array.isArray(members)) { |
| 118 | for (const member of members) { |
| 119 | const out = walk(member) |
| 120 | if (out !== undefined) return out |
| 121 | } |
| 122 | return undefined |
| 123 | } |
| 124 | if (Array.isArray(schema.allOf)) { |
| 125 | const { allOf, ...rest } = schema |
| 126 | return walk(Object.assign(rest, ...allOf)) |
| 127 | } |
| 128 | if (isObject(schema.not)) return undefined |
| 129 | const number = (key: string) => typeof schema[key] === "number" ? schema[key] : undefined |
| 130 | switch (schema.type) { |
| 131 | case "string": |
| 132 | return "a".repeat(Math.min(number("minLength") ?? 0, most)) |
| 133 | case "number": |
| 134 | case "integer": { |
| 135 | let out = Math.max(0, number("minimum") ?? -Infinity) |
| 136 | const above = number("exclusiveMinimum") |
| 137 | if (above !== undefined && out <= above) out = above + 1 |
| 138 | out = Math.min(out, number("maximum") ?? Infinity) |
| 139 | const below = number("exclusiveMaximum") |