@typespec/json-schema 0.46.0-dev.10 → 0.46.0-dev.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -0
- package/lib/main.tsp +7 -2
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# TypeSpec JSON Schema Emitter
|
|
2
|
+
|
|
3
|
+
This package provides [TypeSpec](https://github.com/microsoft/TypeSpec) support for emitting JSON Schema version `2020-12` from TypeSpec definitions. It also provides [decorators for adding JSON Schema constraints](https://microsoft.github.io/typespec/standard-library/json-schema/reference/decorators). The emitter supports either YAML or JSON output and can be configured to emit one file per schema or bundle all schemas in to a single file.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @typespec/json-schema
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Add the `@jsonSchema` decorator to any types or namespaces you want to emit as JSON Schema.
|
|
14
|
+
|
|
15
|
+
```TypeSpec
|
|
16
|
+
import "@typespec/json-schema";
|
|
17
|
+
|
|
18
|
+
using TypeSpec.JsonSchema;
|
|
19
|
+
|
|
20
|
+
@jsonSchema
|
|
21
|
+
namespace Example;
|
|
22
|
+
|
|
23
|
+
model Car {
|
|
24
|
+
make: string;
|
|
25
|
+
model: string;
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
To emit JSON Schema, use either of the following:
|
|
30
|
+
|
|
31
|
+
1. Via the command line
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
tsp compile . --emit @typespec/json-schema
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
2. Via the config
|
|
38
|
+
|
|
39
|
+
Add the following to the `tspconfig.yaml` file.
|
|
40
|
+
|
|
41
|
+
```yaml
|
|
42
|
+
emitters:
|
|
43
|
+
@typespec/json-schema: true
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For more information, consult the [JSON Schema documentation](https://microsoft.github.io/typespec/standard-library/json-schema/reference).
|
|
47
|
+
|
|
48
|
+
## Emitter options
|
|
49
|
+
|
|
50
|
+
### `file-type`
|
|
51
|
+
|
|
52
|
+
**Type:** `"yaml" | "json"`
|
|
53
|
+
|
|
54
|
+
Serialize the schema as either yaml or json.
|
|
55
|
+
|
|
56
|
+
### `int64-strategy`
|
|
57
|
+
|
|
58
|
+
**Type:** `"string" | "number"`
|
|
59
|
+
|
|
60
|
+
How to handle 64 bit integers on the wire. Options are:
|
|
61
|
+
|
|
62
|
+
- string: serialize as a string (widely interoperable)
|
|
63
|
+
- number: serialize as a number (not widely interoperable)
|
|
64
|
+
|
|
65
|
+
### `bundleId`
|
|
66
|
+
|
|
67
|
+
**Type:** `string`
|
|
68
|
+
|
|
69
|
+
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.
|
|
70
|
+
|
|
71
|
+
### `emitAllModels`
|
|
72
|
+
|
|
73
|
+
**Type:** `boolean`
|
|
74
|
+
|
|
75
|
+
When true, emit all model declarations to JSON Schema without requiring the @jsonSchema decorator.
|
|
76
|
+
|
|
77
|
+
### `emitAllRefs`
|
|
78
|
+
|
|
79
|
+
**Type:** `boolean`
|
|
80
|
+
|
|
81
|
+
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.
|
|
82
|
+
|
|
83
|
+
## See also
|
|
84
|
+
|
|
85
|
+
- [Json Schema Emitter Documentation](https://microsoft.github.io/typespec/standard-library/json-schema/reference)
|
|
86
|
+
- [TypeSpec Getting Started](https://github.com/microsoft/typespec#getting-started)
|
|
87
|
+
- [TypeSpec Website](https://microsoft.github.io/typespec)
|
package/lib/main.tsp
CHANGED
|
@@ -96,7 +96,7 @@ extern dec prefixItems(target: unknown[] | Reflection.ModelProperty, value: unkn
|
|
|
96
96
|
/**
|
|
97
97
|
* Specify the content type of content stored in a string.
|
|
98
98
|
*
|
|
99
|
-
* @param the media type of the string contents
|
|
99
|
+
* @param value the media type of the string contents
|
|
100
100
|
*
|
|
101
101
|
*/
|
|
102
102
|
extern dec contentMediaType(target: string | Reflection.ModelProperty, value: valueof string);
|
|
@@ -117,10 +117,13 @@ extern dec contentSchema(target: string | Reflection.ModelProperty, value: unkno
|
|
|
117
117
|
* emit the raw JSON code `{x: "value"}`.
|
|
118
118
|
*
|
|
119
119
|
* @param key the name of the keyword of vendor extension, e.g. `x-custom`.
|
|
120
|
-
* @param value the value of the keyword. Will be converted to a schema unless wrapped in Json<T
|
|
120
|
+
* @param value the value of the keyword. Will be converted to a schema unless wrapped in `Json<T>`.
|
|
121
121
|
*/
|
|
122
122
|
extern dec extension(target: unknown, key: valueof string, value: unknown);
|
|
123
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Well-known JSON Schema formats.
|
|
126
|
+
*/
|
|
124
127
|
enum Format {
|
|
125
128
|
dateTime: "date-time",
|
|
126
129
|
date: "date",
|
|
@@ -148,6 +151,8 @@ enum Format {
|
|
|
148
151
|
* `@extension("x-schema", { x: "value" })` will emit a JSON schema value for `x-schema`,
|
|
149
152
|
* whereas `@extension("x-schema", Json<{x: "value"}>)` will emit the raw JSON code
|
|
150
153
|
* `{x: "value"}`.
|
|
154
|
+
*
|
|
155
|
+
* @template T the type to convert to raw JSON
|
|
151
156
|
*/
|
|
152
157
|
@Private.validatesRawJson(T)
|
|
153
158
|
model Json<T> {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/json-schema",
|
|
3
|
-
"version": "0.46.0-dev.
|
|
3
|
+
"version": "0.46.0-dev.12",
|
|
4
4
|
"author": "Microsoft Corporation",
|
|
5
5
|
"description": "TypeSpec library for emitting TypeSpec to JSON Schema and converting JSON Schema to TypeSpec",
|
|
6
6
|
"homepage": "https://github.com/microsoft/typespec",
|