microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/openapi3/src/lib.ts
94lines · modecode
| 1 | import { createCadlLibrary, paramMessage } from "@cadl-lang/compiler"; |
| 2 | |
| 3 | export const libDef = { |
| 4 | name: "@cadl-lang/openapi3", |
| 5 | diagnostics: { |
| 6 | "security-service-namespace": { |
| 7 | severity: "error", |
| 8 | messages: { |
| 9 | default: "Cannot add security details to a namespace other than the service namespace.", |
| 10 | }, |
| 11 | }, |
| 12 | "resource-namespace": { |
| 13 | severity: "error", |
| 14 | messages: { |
| 15 | default: "Resource goes on namespace", |
| 16 | }, |
| 17 | }, |
| 18 | "path-query": { |
| 19 | severity: "error", |
| 20 | messages: { |
| 21 | default: `OpenAPI does not allow paths containing a query string.`, |
| 22 | }, |
| 23 | }, |
| 24 | "duplicate-body": { |
| 25 | severity: "error", |
| 26 | messages: { |
| 27 | default: "Duplicate @body declarations on response type", |
| 28 | }, |
| 29 | }, |
| 30 | "duplicate-header": { |
| 31 | severity: "error", |
| 32 | messages: { |
| 33 | default: paramMessage`The header ${"header"} is defined across multiple content types`, |
| 34 | }, |
| 35 | }, |
| 36 | "status-code-in-default-response": { |
| 37 | severity: "error", |
| 38 | messages: { |
| 39 | default: "a default response should not have an explicit status code", |
| 40 | }, |
| 41 | }, |
| 42 | "invalid-schema": { |
| 43 | severity: "error", |
| 44 | messages: { |
| 45 | default: paramMessage`Couldn't get schema for type ${"type"}`, |
| 46 | }, |
| 47 | }, |
| 48 | "union-null": { |
| 49 | severity: "error", |
| 50 | messages: { |
| 51 | default: "Cannot have a union containing only null types.", |
| 52 | }, |
| 53 | }, |
| 54 | "union-unsupported": { |
| 55 | severity: "error", |
| 56 | messages: { |
| 57 | default: "Unions are not supported unless all options are literals of the same type.", |
| 58 | type: paramMessage`Type "${"kind"}" cannot be used in unions`, |
| 59 | empty: |
| 60 | "Empty unions are not supported for OpenAPI v3 - enums must have at least one value.", |
| 61 | null: "Unions containing multiple model types cannot be emitted to OpenAPI v2 unless the union is between one model type and 'null'.", |
| 62 | }, |
| 63 | }, |
| 64 | discriminator: { |
| 65 | severity: "error", |
| 66 | messages: { |
| 67 | duplicate: paramMessage`Discriminator value "${"val"}" defined in two different variants: ${"model1"} and ${"model2"}`, |
| 68 | missing: "The discriminator property is not defined in a variant of a discriminated union.", |
| 69 | required: "The discriminator property must be a required property.", |
| 70 | type: "The discriminator property must be type 'string'.", |
| 71 | }, |
| 72 | }, |
| 73 | "discriminator-value": { |
| 74 | severity: "warning", |
| 75 | messages: { |
| 76 | literal: |
| 77 | "Each variant of a discriminated union should define the discriminator property with a string literal value.", |
| 78 | }, |
| 79 | }, |
| 80 | "invalid-default": { |
| 81 | severity: "error", |
| 82 | messages: { |
| 83 | default: paramMessage`Invalid type '${"type"}' for a default value`, |
| 84 | }, |
| 85 | }, |
| 86 | }, |
| 87 | emitter: { |
| 88 | names: ["openapi3"], |
| 89 | }, |
| 90 | } as const; |
| 91 | const lib = createCadlLibrary(libDef); |
| 92 | export const { reportDiagnostic } = lib; |
| 93 | |
| 94 | export type OpenAPILibrary = typeof lib; |
| 95 | |