|
| 1 | +/** |
| 2 | + * Source of truth for the `examples.yaml` unified examples format emitted/consumed by |
| 3 | + * Azure tooling. The JSON Schema generated from this file is published for editor and CI |
| 4 | + * validation and is used by the `examples-validate` tool (via ajv). |
| 5 | + * |
| 6 | + * See the RFC: Unified Examples Format (§3–§4). |
| 7 | + * |
| 8 | + * Note: because YAML permits arbitrary string keys, status codes, headers, query params, and |
| 9 | + * map-shaped bodies are modeled as `Record<...>`. Rules that JSON Schema cannot express |
| 10 | + * (integer-only status codes, quoted `since`, `since` ∈ `service.yaml`, per-lineage |
| 11 | + * uniqueness, file placement, `{api-version}`-only placeholder) are enforced by the |
| 12 | + * `examples-validate` semantic rules. |
| 13 | + */ |
| 14 | +import "@typespec/json-schema"; |
| 15 | + |
| 16 | +using JsonSchema; |
| 17 | + |
| 18 | +/** |
| 19 | + * Top-level `examples.yaml` file: `$schema`/`$namespace` metadata plus one entry per |
| 20 | + * operation, keyed by the interface-relative operation name (e.g. `CaCertificates.get`). |
| 21 | + * Each operation maps to a list of example variants. |
| 22 | + * |
| 23 | + * The indexer is widened to `Example[] | string` so the `$`-prefixed string metadata keys |
| 24 | + * are structurally valid. The rules that a bare (non-`$`) key must be a list of examples and |
| 25 | + * that the only allowed metadata keys are `$schema`/`$namespace` are enforced by the |
| 26 | + * `examples-validate` semantic rules. |
| 27 | + */ |
| 28 | +@jsonSchema("examples.yaml") |
| 29 | +model ExamplesYaml is Record<Example[] | string> { |
| 30 | + /** Schema URL. Gives editors autocomplete and inline validation while authoring. */ |
| 31 | + $schema?: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * Service namespace (e.g. `Microsoft.EventGrid`), prepended to each operation key to form |
| 35 | + * the fully-qualified operation identity used for Swagger linkage (`x-id`). |
| 36 | + */ |
| 37 | + $namespace?: string; |
| 38 | +} |
| 39 | + |
| 40 | +/** A single example variant representing one complete API interaction. */ |
| 41 | +model Example { |
| 42 | + /** |
| 43 | + * Optional human-readable title. Omit for the single-example case; provide it only to |
| 44 | + * disambiguate multiple distinct examples on the same operation. Entries sharing a `title` |
| 45 | + * form one lineage; untitled entries all belong to the single default lineage. |
| 46 | + */ |
| 47 | + title?: string; |
| 48 | + |
| 49 | + /** Longer description of what this example demonstrates. */ |
| 50 | + description?: string; |
| 51 | + |
| 52 | + /** |
| 53 | + * Quoted version from which this variant applies (must be a version listed in the service's |
| 54 | + * `service.yaml`). Omit to apply from the earliest version. |
| 55 | + */ |
| 56 | + since?: string; |
| 57 | + |
| 58 | + /** The request of the API interaction. */ |
| 59 | + request: ExampleRequest; |
| 60 | + |
| 61 | + /** Responses keyed by integer status code (e.g. `200`, `404`). */ |
| 62 | + responses: Record<ExampleResponse>; |
| 63 | +} |
| 64 | + |
| 65 | +/** The request portion of an example, split by parameter location. */ |
| 66 | +model ExampleRequest { |
| 67 | + /** Path parameters. `api-version` is implicit and MUST NOT be included. */ |
| 68 | + path?: Record<unknown>; |
| 69 | + |
| 70 | + /** Query parameters (e.g. `$top`, `$filter`). */ |
| 71 | + query?: Record<unknown>; |
| 72 | + |
| 73 | + /** Request headers (e.g. `If-Match`). */ |
| 74 | + headers?: Record<unknown>; |
| 75 | + |
| 76 | + /** Request body. Structure matches the operation's request schema. */ |
| 77 | + body?: unknown; |
| 78 | +} |
| 79 | + |
| 80 | +/** A single response of an example, keyed in the parent map by integer status code. */ |
| 81 | +model ExampleResponse { |
| 82 | + /** Response headers (e.g. `Location`, `Azure-AsyncOperation`). */ |
| 83 | + headers?: Record<unknown>; |
| 84 | + |
| 85 | + /** Response body. Structure matches the operation's response schema. */ |
| 86 | + body?: unknown; |
| 87 | +} |
0 commit comments