( ast: S.AST.AST )
| 421 | return info as any |
| 422 | } |
| 423 | |
| 424 | /** @deprecated Use OmegaForm instead */ |
| 425 | export function getMetadataFromSchema( |
| 426 | ast: S.AST.AST |
| 427 | ): { |
| 428 | type: "int" | "float" | "text" |
| 429 | minimum?: number |
| 430 | maximum?: number |
| 431 | exclusiveMinimum?: number |
| 432 | exclusiveMaximum?: number |
| 433 | minLength?: number |
| 434 | maxLength?: number |
| 435 | required: boolean |
| 436 | description?: string |
| 437 | } { |
| 438 | const findJsonSchemaType = ( |
| 439 | schema: any, |
| 440 | target: "number" | "integer" |
| 441 | ): boolean => { |
| 442 | if (!schema || typeof schema !== "object") { |
| 443 | return false |
| 444 | } |
| 445 | |
| 446 | if (schema.type === target) { |
| 447 | return true |
| 448 | } |
| 449 | |
| 450 | if (Array.isArray(schema.type) && schema.type.includes(target)) { |
| 451 | return true |
| 452 | } |
| 453 | |
| 454 | return ["anyOf", "oneOf", "allOf"].some((key) => |
| 455 | Array.isArray(schema[key]) && schema[key].some((member: any) => findJsonSchemaType(member, target)) |
| 456 | ) |
| 457 | } |
| 458 | |
| 459 | const nullable = S.AST.isUnion(ast) && ast.types.includes(S.Null.ast) |
| 460 | const realSelf = nullable && S.AST.isUnion(ast) |
| 461 | ? ast.types.find((_) => _ !== S.Null.ast)! |
| 462 | : ast |
| 463 | |
| 464 | let jschema: any |
| 465 | try { |
| 466 | const doc = S.toJsonSchemaDocument(S.make<S.Codec<unknown>>(realSelf)) |
| 467 | jschema = doc.schema as any |
| 468 | const defs = doc.definitions |
| 469 | // resolve $ref against definitions |
| 470 | while (jschema["$ref"] && jschema["$ref"].startsWith("#/$defs/")) { |
| 471 | const { $ref: _, ...rest } = jschema |
| 472 | jschema = { ...defs[jschema["$ref"].replace("#/$defs/", "")], ...rest } |
| 473 | } |
| 474 | } catch { |
| 475 | jschema = {} |
| 476 | } |
| 477 | // or we need to add these info directly in the refinement like the minimum |
| 478 | // or find a jsonschema parser whojoins all of them |
| 479 | // todo, we have to use $ref: "#/$defs/Int" |
| 480 | // and look up |
no test coverage detected
searching dependent graphs…