(js: JsonSchema.JsonSchema, seen = new Set<string>())
| 408 | // Prove nullability through type constraints, references, allOf and anyOf. |
| 409 | // Other constraints (notably enum and oneOf) can exclude null despite a nullable type. |
| 410 | const acceptsNull = (js: JsonSchema.JsonSchema, seen = new Set<string>()): boolean => { |
| 411 | if (Predicate.isString(js.$ref)) { |
| 412 | if (!Object.keys(js).every((key) => key === "$ref" || isAnnotation(key))) return false |
| 413 | const key = definitionKey(js.$ref) |
| 414 | if (key === undefined || seen.has(key)) return false |
| 415 | const definition = resolve(key) |
| 416 | if (definition === undefined) return false |
| 417 | seen.add(key) |
| 418 | const out = acceptsNull(definition, seen) |
| 419 | seen.delete(key) |
| 420 | return out |
| 421 | } |
| 422 | if ( |
| 423 | !Object.keys(js).every((key) => objectKeys.has(key) || key === "allOf" || key === "anyOf" || isAnnotation(key)) |
| 424 | ) { |
| 425 | return false |
| 426 | } |
| 427 | return (js.type === undefined || js.type === "null" || isStringArray(js.type) && js.type.includes("null")) && |
| 428 | (!Array.isArray(js.allOf) || |
| 429 | js.allOf.every((member) => Predicate.isObject(member) && acceptsNull(member, seen))) && |
| 430 | (!Array.isArray(js.anyOf) || js.anyOf.some((member) => Predicate.isObject(member) && acceptsNull(member, seen))) |
| 431 | } |
| 432 | |
| 433 | const prepare = (js: JsonSchema.JsonSchema): JsonSchema.JsonSchema => |
| 434 | rewriteNot(rewriteConditional(rewriteNullable(js, acceptsNull))) |
no test coverage detected