TypeSpec emitter for models to create JSON example objects.
To install the package, use npm:
npm install @wictorwilen/typespec-emit-json-samplesThis package is designed to work with the TypeSpec compiler. After installing, you can use it to generate JSON sample objects.
TypeSpec document:
namespace Samples;
@example(#{kind: "ev", brand: "Audi", year: 2020})
model Car {
kind: "ev" | "ice";
brand: string;
@minValue(1900) year: int32;
}Emitted JSON sample:
{
"kind": "ev",
"brand": "Audi",
"year": "2020"
}- Uses the
@exampledecorator to generate JSON samples - For multiple
@exampledecorators a random example will be chosen - If no examples are found on a property the emitter tries to retrieve examples through inheritance
- Populate arrays based on models and derived models, to create a great sample
- Randomization if multiple examples exists for a model
Use the following configuration in the tspconfig.yaml
- namespace (string or array(string), required) - the namespacse where the models are found
- models (string or array(string), required) - the name of the models to create samples from
- outDir: (string) - the output directory
- setUndefinedToNull: (boolean, default=true) - if set to false all non-defined properties in examples will be omitted, otherwise set to
null - fillArrays: (boolean, default=false) - tries to populate the array even if the examples specifies an empty array
- arraySize: (number, default=1) - the number of items to populate an array with
- randomize: (boolean, default=false) - randomize the example selection if there are multiple choices.
emit:
- "@wictorwilen/typespec-emit-json-samples"
options:
"@wictorwilen/typespec-emit-json-samples":
"namespace": "Samples"
"models":
- Car
- Engine
"outDir": "{cwd}/tsp-output"
"setUndefinedToNull": true
"fillArray": true
"arraySize": 3
"randomize": true
To contribute or modify this package, clone the repository and install the dependencies:
git clone https://github.com/wictorwilen/typespec-emit-json-samples.git
cd typespec-emit-json-samples
npm installRun the following command to build the project:
npm run buildThis project is licensed under the MIT License.
Created by Wictor Wilén. For inquiries, contact [email protected].