@wictorwilen/typespec-emit-json-samples
v1.1.0
Published
TypeSpec emitter for models to create JSON sample objects based on the TypeSpec model and examples.
Readme
TypeSpec Emit JSON samples
TypeSpec emitter for models to create JSON example objects.
Installation
To install the package, use npm:
npm install @wictorwilen/typespec-emit-json-samplesUsage
This package is designed to work with the TypeSpec compiler. After installing, you can use it to generate JSON sample objects.
Example
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"
}Features
- 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
Configuration
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
Development
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 installBuild the Project
Run the following command to build the project:
npm run buildLicense
This project is licensed under the MIT License.
Author
Created by Wictor Wilén. For inquiries, contact [email protected].
