Tool for generation samples based on OpenAPI payload/response schema
- Deterministic (given a particular input, will always produce the same output)
- Supports compound keywords:
allOf,oneOf,anyOf,if/then/else - Supports
additionalPropertieswithx-additionalPropertiesName - Uses
const,examples,enumanddefaultwhere possible - in this order - Good array support: supports
contains,minItems,maxItems, and tuples (itemsas an array) - Supports
minLength,maxLength,min,max,exclusiveMinimum,exclusiveMaximum, (limited)pattern - Supports the following
stringformats:- idn-email
- password
- date-time
- date
- time
- ipv4
- ipv6
- hostname
- idn-hostname
- uri
- uri-reference
- uri-template
- iri
- iri-reference
- uuid
- json-pointer
- relative-json-pointer
- regex
- Infers schema type automatically following same rules as json-schema-faker
- Support for
$refresolving - Has basic supports for JSON Schema draft 7 (thanks to @P0lip from @stoplightio for contributing)
Install using npm
npm install openapi-sampler
or using yarn
yarn add openapi-sampler
Then require it in your code:
var OpenAPISampler = require('openapi-sampler');- schema (required) -
objectAn OpenAPI Schema Object or a JSON Schema Draft 7 document. - options (optional) -
objectAvailable options:- skipNonRequired -
booleanDon't include non-required object properties not specified inrequiredproperty of the schema object - skipReadOnly -
booleanDon't includereadOnlyobject properties - skipWriteOnly -
booleanDon't includewriteOnlyobject properties - quiet -
booleanDon't log console warning messages
- skipNonRequired -
- spec - whole specification where the schema is taken from. Required only when schema may contain
$ref. spec must not contain any external references
const OpenAPISampler = require('.');
OpenAPISampler.sample({
type: 'object',
properties: {
a: {type: 'integer', minimum: 10},
b: {type: 'string', format: 'password', minLength: 10},
c: {type: 'boolean', readOnly: true}
}
}, {skipReadOnly: true});
// { a: 10, b: 'pa$$word_q' }