microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/json-schema/src/lib.ts
95lines · modecode
| 1 | import { createTypeSpecLibrary, JSONSchemaType } from "@typespec/compiler"; |
| 2 | |
| 3 | export type FileType = "yaml" | "json"; |
| 4 | export type Int64Strategy = "string" | "number"; |
| 5 | |
| 6 | export interface JSONSchemaEmitterOptions { |
| 7 | /** |
| 8 | * Serialize the schema as either yaml or json. |
| 9 | * @default yaml, it not specified infer from the `output-file` extension |
| 10 | */ |
| 11 | "file-type"?: FileType; |
| 12 | |
| 13 | /** |
| 14 | * How to handle 64 bit integers on the wire. Options are: |
| 15 | * |
| 16 | * * string: serialize as a string (widely interoperable) |
| 17 | * * number: serialize as a number (not widely interoperable) |
| 18 | */ |
| 19 | "int64-strategy"?: Int64Strategy; |
| 20 | |
| 21 | /** |
| 22 | * When provided, bundle all the schemas into a single json schema document |
| 23 | * with schemas under $defs. The provided id is the id of the root document |
| 24 | * and is also used for the file name. |
| 25 | */ |
| 26 | bundleId?: string; |
| 27 | |
| 28 | /** |
| 29 | * When true, emit all model declarations to JSON Schema without requiring |
| 30 | * the @jsonSchema decorator. |
| 31 | */ |
| 32 | emitAllModels?: boolean; |
| 33 | |
| 34 | /** |
| 35 | * When true, emit all references as json schema files, even if the referenced |
| 36 | * type does not have the `@jsonSchema` decorator or is not within a namespace |
| 37 | * with the `@jsonSchema` decorator. |
| 38 | */ |
| 39 | emitAllRefs?: boolean; |
| 40 | } |
| 41 | |
| 42 | export const EmitterOptionsSchema: JSONSchemaType<JSONSchemaEmitterOptions> = { |
| 43 | type: "object", |
| 44 | additionalProperties: false, |
| 45 | properties: { |
| 46 | "file-type": { |
| 47 | type: "string", |
| 48 | enum: ["yaml", "json"], |
| 49 | nullable: true, |
| 50 | description: "Serialize the schema as either yaml or json.", |
| 51 | }, |
| 52 | "int64-strategy": { |
| 53 | type: "string", |
| 54 | enum: ["string", "number"], |
| 55 | nullable: true, |
| 56 | description: `How to handle 64 bit integers on the wire. Options are: |
| 57 | |
| 58 | * string: serialize as a string (widely interoperable) |
| 59 | * number: serialize as a number (not widely interoperable)`, |
| 60 | }, |
| 61 | bundleId: { |
| 62 | type: "string", |
| 63 | nullable: true, |
| 64 | description: |
| 65 | "When provided, bundle all the schemas into a single json schema document with schemas under $defs. The provided id is the id of the root document and is also used for the file name.", |
| 66 | }, |
| 67 | emitAllModels: { |
| 68 | type: "boolean", |
| 69 | nullable: true, |
| 70 | description: |
| 71 | "When true, emit all model declarations to JSON Schema without requiring the @jsonSchema decorator.", |
| 72 | }, |
| 73 | emitAllRefs: { |
| 74 | type: "boolean", |
| 75 | nullable: true, |
| 76 | description: |
| 77 | "When true, emit all references as json schema files, even if the referenced type does not have the `@jsonSchema` decorator or is not within a namespace with the `@jsonSchema` decorator.", |
| 78 | }, |
| 79 | }, |
| 80 | required: [], |
| 81 | }; |
| 82 | |
| 83 | export const libDef = { |
| 84 | name: "@typespec/json-schema", |
| 85 | diagnostics: {}, |
| 86 | emitter: { |
| 87 | options: EmitterOptionsSchema as JSONSchemaType<JSONSchemaEmitterOptions>, |
| 88 | }, |
| 89 | } as const; |
| 90 | |
| 91 | export const $lib = createTypeSpecLibrary(libDef); |
| 92 | |
| 93 | export const { reportDiagnostic, createStateSymbol } = $lib; |
| 94 | |
| 95 | export type JsonSchemaLibrary = typeof $lib; |
| 96 | |