Registry / testing / openapi-sampler

openapi-sampler

JSON →
library1.7.4jsnpmunverified

Tool for generation samples based on OpenAPI payload/response schema. Current stable version is 1.7.4. Released as needed with moderate cadence. Differentiators: deterministic output, supports compound keywords (allOf, oneOf, anyOf, if/then/else), uses const, examples, enum, and default in priority order, and supports $ref resolving.

npm install openapi-sampler
INSTALL
IMPORT
SIG · OPENAPI-SAMPLER
O
openapi-sampler
testingjavascriptv1.7.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sample
import { sample } from 'openapi-sampler'
const sample = require('openapi-sampler')
ESM default export is an object with sample function; named import is correct for ESM. CJS require returns an object, so sample is a property.
OpenAPISampler
import OpenAPISampler from 'openapi-sampler'
const { OpenAPISampler } = require('openapi-sampler')
If using default import, OpenAPISampler is the default export (object). In CJS, require returns that object; destructuring 'OpenAPISampler' is wrong because property name is lowercase 'sample'.
sample
const { sample } = require('openapi-sampler')
const sample = require('openapi-sampler')
In CJS, require returns the module object; sample is a property of that object.

Generate a sample object from an OpenAPI schema object using default options.

import { sample } from 'openapi-sampler'; const schema = { type: 'object', properties: { id: { type: 'integer', minimum: 1 }, name: { type: 'string', minLength: 3 }, tags: { type: 'array', items: { type: 'string' }, minItems: 1 } }, required: ['id', 'name'] }; const result = sample(schema, { skipNonRequired: true }); console.log(result); // { id: 1, name: 'name_2' }
Debug
Known issues
gotchaThe `spec` parameter is required when the schema contains $ref, but it must not contain external references. Failing to provide it will cause $ref to not be resolved and the sample may be incomplete.
fix
Pass the full OpenAPI spec object as third argument to `sample()`.
affects: >=1.0.0
gotchaThe library is deterministic, so subsequent calls with the same inputs produce identical output. This may lead to misleading samples if randomness is expected.
fix
If you need non-deterministic output, consider using another library or manually post-processing the sample.
affects: >=1.0.0
gotchaInferring schema type follows same rules as json-schema-faker, which may not match OpenAPI specification exactly. For example, an empty object {} is treated as type 'object'.
fix
Be explicit with `type` in schema to avoid unexpected inferences.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'properties')
The schema object is not provided or is undefined.
fix
Ensure the first argument to `sample()` is a valid schema object.
Error: Could not resolve $ref: #/components/schemas/Pet
The spec parameter was omitted or the reference cannot be resolved in the provided spec.
fix
Pass the full OpenAPI spec object as third argument to `sample()`.
Upgrade
Version history
1.7.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Amazon
1
Resources
openapi-sampler — npm install openapi-sampler · libregistry