TypeSpec library for emitting TypeSpec to JSON Schema and converting JSON Schema to TypeSpec
npm install @typespec/json-schema
Add the @jsonSchema
decorator to any types or namespaces you want to emit as JSON Schema.
import "@typespec/json-schema";
using TypeSpec.JsonSchema;
@jsonSchema
namespace Example;
model Car {
make: string;
modelName: string;
}
- Via the command line
tsp compile . --emit=@typespec/json-schema
- Via the config
emit:
- "@typespec/json-schema"
The config can be extended with options as follows:
emit:
- "@typespec/json-schema"
options:
"@typespec/json-schema":
option: value
Type: "yaml" | "json"
Serialize the schema as either yaml or json.
Type: "string" | "number"
How to handle 64 bit integers on the wire. Options are:
- string: serialize as a string (widely interoperable)
- number: serialize as a number (not widely interoperable)
Type: string
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.
Type: boolean
When true, emit all model declarations to JSON Schema without requiring the @jsonSchema decorator.
Type: boolean
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.
Type: boolean
If true, then for models emitted as object schemas we default unevaluatedProperties
to { not: {} }
,
if not explicitly specified elsewhere.
Default: false
@baseUri
@contains
@contentEncoding
@contentMediaType
@contentSchema
@extension
@id
@jsonSchema
@maxContains
@maxProperties
@minContains
@minProperties
@multipleOf
@oneOf
@prefixItems
@uniqueItems
Set the base URI for any schemas emitted from types within this namespace.
@TypeSpec.JsonSchema.baseUri(baseUri: valueof string)
Namespace
Name | Type | Description |
---|---|---|
baseUri | valueof string |
the base URI. Schema IDs inside this namespace are relative to this URI. |
Specify that the array must contain at least one instance of the provided type.
Use @minContains
and @maxContains
to customize how many instances to expect.
@TypeSpec.JsonSchema.contains(value: unknown)
unknown[] | ModelProperty
Name | Type | Description |
---|---|---|
value | unknown |
The type the array must contain. |
Specify the encoding used for the contents of a string.
@TypeSpec.JsonSchema.contentEncoding(value: valueof string)
string | ModelProperty
Name | Type | Description |
---|---|---|
value | valueof string |
Specify the content type of content stored in a string.
@TypeSpec.JsonSchema.contentMediaType(value: valueof string)
string | ModelProperty
Name | Type | Description |
---|---|---|
value | valueof string |
the media type of the string contents |
Specify the schema for the contents of a string when interpreted according to the content's media type and encoding.
@TypeSpec.JsonSchema.contentSchema(value: unknown)
string | ModelProperty
Name | Type | Description |
---|---|---|
value | unknown |
the schema of the string contents |
Specify a custom property to add to the emitted schema. Useful for adding custom keywords
and other vendor-specific extensions. Scalar values need to be specified using typeof
to be converted to a schema.
For example, @extension("x-schema", typeof "foo")
will emit a JSON schema value for x-schema
,
whereas @extension("x-schema", "foo")
will emit the raw code "foo"
.
The value will be treated as a raw value if any of the following are true:
- The value is a scalar value (e.g. string, number, boolean, etc.)
- The value is wrapped in the
Json<Data>
template - The value is provided using the value syntax (e.g.
#{}
,#[]
)
For example, @extension("x-schema", { x: "value" })
will emit a JSON schema value for x-schema
,
whereas @extension("x-schema", #{x: "value"})
and @extension("x-schema", Json<{x: "value"}>)
will emit the raw JSON code {x: "value"}
.
@TypeSpec.JsonSchema.extension(key: valueof string, value: unknown | valueof unknown)
unknown
Name | Type | Description |
---|---|---|
key | valueof string |
the name of the keyword of vendor extension, e.g. x-custom . |
value | unknown | valueof unknown |
the value of the keyword. |
Specify the JSON Schema id. If this model or a parent namespace has a base URI, the provided ID will be relative to that base URI.
By default, the id will be constructed based on the declaration's name.
@TypeSpec.JsonSchema.id(id: valueof string)
unknown
Name | Type | Description |
---|---|---|
id | valueof string |
the id of the JSON schema for this declaration. |
Add to namespaces to emit models within that namespace to JSON schema. Add to another declaration to emit that declaration to JSON schema.
Optionally, for namespaces, you can provide a baseUri, and for other declarations, you can provide the id.
@TypeSpec.JsonSchema.jsonSchema(baseUri?: valueof string)
unknown
Name | Type | Description |
---|---|---|
baseUri | valueof string |
Schema IDs are interpreted as relative to this URI. |
Specify that the array must contain at most some number of the types provided by the contains decorator.
@TypeSpec.JsonSchema.maxContains(value: valueof int32)
unknown[] | ModelProperty
Name | Type | Description |
---|---|---|
value | valueof int32 |
The maximum number of instances the array must contain |
Specify the maximum number of properties this object can have.
@TypeSpec.JsonSchema.maxProperties(value: valueof int32)
Record<unknown> | ModelProperty
Name | Type | Description |
---|---|---|
value | valueof int32 |
The maximum number of properties this object can have. |
Specify that the array must contain at least some number of the types provided by the contains decorator.
@TypeSpec.JsonSchema.minContains(value: valueof int32)
unknown[] | ModelProperty
Name | Type | Description |
---|---|---|
value | valueof int32 |
The minimum number of instances the array must contain |
Specify the minimum number of properties this object can have.
@TypeSpec.JsonSchema.minProperties(value: valueof int32)
Record<unknown> | ModelProperty
Name | Type | Description |
---|---|---|
value | valueof int32 |
The minimum number of properties this object can have. |
Specify that the numeric type must be a multiple of some numeric value.
@TypeSpec.JsonSchema.multipleOf(value: valueof numeric)
numeric | ModelProperty
Name | Type | Description |
---|---|---|
value | valueof numeric |
The numeric type must be a multiple of this value. |
Specify that oneOf
should be used instead of anyOf
for that union.
@TypeSpec.JsonSchema.oneOf
Union | ModelProperty
None
Specify that the target array must begin with the provided types.
@TypeSpec.JsonSchema.prefixItems(value: unknown[])
unknown[] | ModelProperty
Name | Type | Description |
---|---|---|
value | unknown[] |
a tuple containing the types that must be present at the start of the array |
Specify that every item in the array must be unique.
@TypeSpec.JsonSchema.uniqueItems
unknown[] | ModelProperty
None