@typespec/json-schema 0.47.0-dev.4 → 0.47.0-dev.5

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.
Files changed (2) hide show
  1. package/README.md +317 -21
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # TypeSpec JSON Schema Emitter
1
+ # @typespec/json-schema
2
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.
3
+ TypeSpec library for emitting TypeSpec to JSON Schema and converting JSON Schema to TypeSpec
4
4
 
5
- ## Installation
5
+ ## Install
6
6
 
7
7
  ```bash
8
8
  npm install @typespec/json-schema
@@ -26,34 +26,32 @@ model Car {
26
26
  }
27
27
  ```
28
28
 
29
- To emit JSON Schema, use either of the following:
29
+ ## Emitter
30
+
31
+ ### Usage
30
32
 
31
33
  1. Via the command line
32
34
 
33
35
  ```bash
34
- tsp compile . --emit @typespec/json-schema
36
+ tsp compile . --emit=@typespec/json-schema
35
37
  ```
36
38
 
37
39
  2. Via the config
38
40
 
39
- Add the following to the `tspconfig.yaml` file.
40
-
41
41
  ```yaml
42
- emitters:
43
- @typespec/json-schema: true
42
+ emit:
43
+ - "@typespec/json-schema"
44
44
  ```
45
45
 
46
- For more information, consult the [JSON Schema documentation](https://microsoft.github.io/typespec/standard-library/json-schema/reference).
46
+ ### Emitter options
47
47
 
48
- ## Emitter options
49
-
50
- ### `file-type`
48
+ #### `file-type`
51
49
 
52
50
  **Type:** `"yaml" | "json"`
53
51
 
54
52
  Serialize the schema as either yaml or json.
55
53
 
56
- ### `int64-strategy`
54
+ #### `int64-strategy`
57
55
 
58
56
  **Type:** `"string" | "number"`
59
57
 
@@ -62,26 +60,324 @@ How to handle 64 bit integers on the wire. Options are:
62
60
  - string: serialize as a string (widely interoperable)
63
61
  - number: serialize as a number (not widely interoperable)
64
62
 
65
- ### `bundleId`
63
+ #### `bundleId`
66
64
 
67
65
  **Type:** `string`
68
66
 
69
67
  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
68
 
71
- ### `emitAllModels`
69
+ #### `emitAllModels`
72
70
 
73
71
  **Type:** `boolean`
74
72
 
75
73
  When true, emit all model declarations to JSON Schema without requiring the @jsonSchema decorator.
76
74
 
77
- ### `emitAllRefs`
75
+ #### `emitAllRefs`
78
76
 
79
77
  **Type:** `boolean`
80
78
 
81
79
  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
80
 
83
- ## See also
81
+ ## Decorators
82
+
83
+ ### TypeSpec.JsonSchema
84
+
85
+ - [`@baseUri`](#@baseuri)
86
+ - [`@contains`](#@contains)
87
+ - [`@contentEncoding`](#@contentencoding)
88
+ - [`@contentMediaType`](#@contentmediatype)
89
+ - [`@contentSchema`](#@contentschema)
90
+ - [`@extension`](#@extension)
91
+ - [`@id`](#@id)
92
+ - [`@jsonSchema`](#@jsonschema)
93
+ - [`@maxContains`](#@maxcontains)
94
+ - [`@maxProperties`](#@maxproperties)
95
+ - [`@minContains`](#@mincontains)
96
+ - [`@minProperties`](#@minproperties)
97
+ - [`@multipleOf`](#@multipleof)
98
+ - [`@prefixItems`](#@prefixitems)
99
+ - [`@uniqueItems`](#@uniqueitems)
100
+
101
+ #### `@baseUri`
102
+
103
+ Set the base URI for any schemas emitted from types within this namespace.
104
+
105
+ ```typespec
106
+ @TypeSpec.JsonSchema.baseUri(baseUri: valueof string)
107
+ ```
108
+
109
+ ##### Target
110
+
111
+ `Namespace`
112
+
113
+ ##### Parameters
114
+
115
+ | Name | Type | Description |
116
+ | ------- | ----------------------- | ------------------------------------------------------------------------ |
117
+ | baseUri | `valueof scalar string` | the base URI. Schema IDs inside this namespace are relative to this URI. |
118
+
119
+ #### `@contains`
120
+
121
+ Specify that the array must contain at least one instance of the provided type.
122
+ Use `@minContains` and `@maxContains` to customize how many instances to expect.
123
+
124
+ ```typespec
125
+ @TypeSpec.JsonSchema.contains(value: unknown)
126
+ ```
127
+
128
+ ##### Target
129
+
130
+ `union unknown[] | ModelProperty`
131
+
132
+ ##### Parameters
133
+
134
+ | Name | Type | Description |
135
+ | ----- | --------------------- | -------------------------------- |
136
+ | value | `(intrinsic) unknown` | The type the array must contain. |
137
+
138
+ #### `@contentEncoding`
139
+
140
+ Specify the encoding used for the contents of a string.
141
+
142
+ ```typespec
143
+ @TypeSpec.JsonSchema.contentEncoding(value: valueof string)
144
+ ```
145
+
146
+ ##### Target
147
+
148
+ `union string | ModelProperty`
149
+
150
+ ##### Parameters
151
+
152
+ | Name | Type | Description |
153
+ | ----- | ----------------------- | ----------- |
154
+ | value | `valueof scalar string` | <br /> |
155
+
156
+ #### `@contentMediaType`
157
+
158
+ Specify the content type of content stored in a string.
159
+
160
+ ```typespec
161
+ @TypeSpec.JsonSchema.contentMediaType(value: valueof string)
162
+ ```
163
+
164
+ ##### Target
165
+
166
+ `union string | ModelProperty`
167
+
168
+ ##### Parameters
169
+
170
+ | Name | Type | Description |
171
+ | ----- | ----------------------- | ------------------------------------- |
172
+ | value | `valueof scalar string` | the media type of the string contents |
173
+
174
+ #### `@contentSchema`
175
+
176
+ Specify the schema for the contents of a string when interpreted according to the content's
177
+ media type and encoding.
178
+
179
+ ```typespec
180
+ @TypeSpec.JsonSchema.contentSchema(value: unknown)
181
+ ```
182
+
183
+ ##### Target
184
+
185
+ `union string | ModelProperty`
186
+
187
+ ##### Parameters
188
+
189
+ | Name | Type | Description |
190
+ | ----- | --------------------- | --------------------------------- |
191
+ | value | `(intrinsic) unknown` | the schema of the string contents |
192
+
193
+ #### `@extension`
194
+
195
+ Specify a custom property to add to the emitted schema. Useful for adding custom keywords
196
+ and other vendor-specific extensions. The value will be converted to a schema unless the parameter
197
+ is wrapped in the `Json<T>` template. For example, `@extension("x-schema", { x: "value" })` will
198
+ emit a JSON schema value for `x-schema`, whereas `@extension("x-schema", Json<{x: "value"}>)` will
199
+ emit the raw JSON code `{x: "value"}`.
200
+
201
+ ```typespec
202
+ @TypeSpec.JsonSchema.extension(key: valueof string, value: unknown)
203
+ ```
204
+
205
+ ##### Target
206
+
207
+ `(intrinsic) unknown`
208
+
209
+ ##### Parameters
210
+
211
+ | Name | Type | Description |
212
+ | ----- | ----------------------- | ------------------------------------------------------------------------------------ |
213
+ | key | `valueof scalar string` | the name of the keyword of vendor extension, e.g. `x-custom`. |
214
+ | value | `(intrinsic) unknown` | the value of the keyword. Will be converted to a schema unless wrapped in `Json<T>`. |
215
+
216
+ #### `@id`
217
+
218
+ Specify the JSON Schema id. If this model or a parent namespace has a base URI,
219
+ the provided ID will be relative to that base URI.
220
+
221
+ By default, the id will be constructed based on the declaration's name.
222
+
223
+ ```typespec
224
+ @TypeSpec.JsonSchema.id(id: valueof string)
225
+ ```
226
+
227
+ ##### Target
228
+
229
+ `(intrinsic) unknown`
230
+
231
+ ##### Parameters
232
+
233
+ | Name | Type | Description |
234
+ | ---- | ----------------------- | ----------------------------------------------- |
235
+ | id | `valueof scalar string` | the id of the JSON schema for this declaration. |
236
+
237
+ #### `@jsonSchema`
238
+
239
+ Add to namespaces to emit models within that namespace to JSON schema.
240
+ Add to another declaration to emit that declaration to JSON schema.
241
+
242
+ Optionally, for namespaces, you can provide a baseUri, and for other declarations,
243
+ you can provide the id.
244
+
245
+ ```typespec
246
+ @TypeSpec.JsonSchema.jsonSchema(baseUri?: valueof string)
247
+ ```
248
+
249
+ ##### Target
250
+
251
+ `(intrinsic) unknown`
252
+
253
+ ##### Parameters
254
+
255
+ | Name | Type | Description |
256
+ | ------- | ----------------------- | --------------------------------------------------- |
257
+ | baseUri | `valueof scalar string` | Schema IDs are interpreted as relative to this URI. |
258
+
259
+ #### `@maxContains`
260
+
261
+ Specify that the array must contain at most some number of the types provided
262
+ by the contains decorator.
263
+
264
+ ```typespec
265
+ @TypeSpec.JsonSchema.maxContains(value: valueof int32)
266
+ ```
267
+
268
+ ##### Target
269
+
270
+ `union unknown[] | ModelProperty`
271
+
272
+ ##### Parameters
273
+
274
+ | Name | Type | Description |
275
+ | ----- | ---------------------- | ------------------------------------------------------ |
276
+ | value | `valueof scalar int32` | The maximum number of instances the array must contain |
277
+
278
+ #### `@maxProperties`
279
+
280
+ Specify the maximum number of properties this object can have.
281
+
282
+ ```typespec
283
+ @TypeSpec.JsonSchema.maxProperties(value: valueof int32)
284
+ ```
285
+
286
+ ##### Target
287
+
288
+ `union Record<unknown> | ModelProperty`
289
+
290
+ ##### Parameters
291
+
292
+ | Name | Type | Description |
293
+ | ----- | ---------------------- | ------------------------------------------------------ |
294
+ | value | `valueof scalar int32` | The maximum number of properties this object can have. |
295
+
296
+ #### `@minContains`
297
+
298
+ Specify that the array must contain at least some number of the types provided
299
+ by the contains decorator.
300
+
301
+ ```typespec
302
+ @TypeSpec.JsonSchema.minContains(value: valueof int32)
303
+ ```
304
+
305
+ ##### Target
306
+
307
+ `union unknown[] | ModelProperty`
308
+
309
+ ##### Parameters
310
+
311
+ | Name | Type | Description |
312
+ | ----- | ---------------------- | ------------------------------------------------------ |
313
+ | value | `valueof scalar int32` | The minimum number of instances the array must contain |
314
+
315
+ #### `@minProperties`
316
+
317
+ Specify the minimum number of properties this object can have.
318
+
319
+ ```typespec
320
+ @TypeSpec.JsonSchema.minProperties(value: valueof int32)
321
+ ```
322
+
323
+ ##### Target
324
+
325
+ `union Record<unknown> | ModelProperty`
326
+
327
+ ##### Parameters
328
+
329
+ | Name | Type | Description |
330
+ | ----- | ---------------------- | ------------------------------------------------------ |
331
+ | value | `valueof scalar int32` | The minimum number of properties this object can have. |
332
+
333
+ #### `@multipleOf`
334
+
335
+ Specify that the numeric type must be a multiple of some numeric value.
336
+
337
+ ```typespec
338
+ @TypeSpec.JsonSchema.multipleOf(value: valueof numeric)
339
+ ```
340
+
341
+ ##### Target
342
+
343
+ `union numeric | ModelProperty`
344
+
345
+ ##### Parameters
346
+
347
+ | Name | Type | Description |
348
+ | ----- | ------------------------ | -------------------------------------------------- |
349
+ | value | `valueof scalar numeric` | The numeric type must be a multiple of this value. |
350
+
351
+ #### `@prefixItems`
352
+
353
+ Specify that the target array must begin with the provided types.
354
+
355
+ ```typespec
356
+ @TypeSpec.JsonSchema.prefixItems(value: unknown[])
357
+ ```
358
+
359
+ ##### Target
360
+
361
+ `union unknown[] | ModelProperty`
362
+
363
+ ##### Parameters
364
+
365
+ | Name | Type | Description |
366
+ | ----- | ----------------- | --------------------------------------------------------------------------- |
367
+ | value | `model unknown[]` | a tuple containing the types that must be present at the start of the array |
368
+
369
+ #### `@uniqueItems`
370
+
371
+ Specify that every item in the array must be unique.
372
+
373
+ ```typespec
374
+ @TypeSpec.JsonSchema.uniqueItems
375
+ ```
376
+
377
+ ##### Target
378
+
379
+ `union unknown[] | ModelProperty`
380
+
381
+ ##### Parameters
84
382
 
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)
383
+ None
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typespec/json-schema",
3
- "version": "0.47.0-dev.4",
3
+ "version": "0.47.0-dev.5",
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",