Gathering detailed insights and metrics for @har-sdk/openapi-sampler
Gathering detailed insights and metrics for @har-sdk/openapi-sampler
Gathering detailed insights and metrics for @har-sdk/openapi-sampler
Gathering detailed insights and metrics for @har-sdk/openapi-sampler
HAR SDK enables Node.js developers to easily work with HAR, convert it from OAS/Swagger and Postman collection, and more.
npm install @har-sdk/openapi-sampler
Typescript
Module System
Node Version
NPM Version
83
Supply Chain
97
Quality
81.8
Maintenance
100
Vulnerability
99.6
License
@har-sdk/oas@2.12.2
Updated on Apr 03, 2025
@har-sdk/openapi-sampler@2.4.2
Updated on Apr 03, 2025
@har-sdk/editor@1.5.14
Updated on Mar 25, 2025
@har-sdk/validator@2.6.1
Updated on Mar 25, 2025
@har-sdk/types@1.1.10
Updated on Mar 25, 2025
@har-sdk/postman@2.4.7
Updated on Mar 25, 2025
TypeScript (98.24%)
JavaScript (1.72%)
Shell (0.04%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
11 Stars
157 Commits
5 Forks
15 Watchers
7 Branches
26 Contributors
Updated on Apr 03, 2025
Latest Version
2.4.2
Package Id
@har-sdk/openapi-sampler@2.4.2
Unpacked Size
125.38 kB
Size
30.26 kB
File Count
54
NPM Version
10.8.2
Node Version
18.20.7
Published on
Apr 03, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
1
This is a tool that generates samples based on OpenAPI payload/response schema.
allOf
, additionalProperties
, and common string formats such as email
, password
, date-time
, and more.$ref
resolving.Using the @har-sdk/openapi-sampler
package can save you a lot of time when you need to generate sample data for your OpenAPI schemas. With just a few lines of code, you can create realistic data objects that you can use for testing or documentation purposes.
To install the package using npm, run the following command:
1$ npm i --save @har-sdk/openapi-sampler
If you prefer using yarn, you can use the following command instead:
1$ yarn add @har-sdk/openapi-sampler
After installing, you can import the package in your code using the following import statement:
1import { sample } from '@har-sdk/openapi-sampler';
The sample()
function is the main API provided by the package, and it takes three parameters:
Option | Description |
---|---|
schema | OpenAPI Schema Object that specifies the structure of the data you want to generate samples for. |
options | Provides additional options for the sampler, such as skipping non-required properties, read-only properties, or write-only properties. You can find a full list of available options in the code example below. |
spec | Entire OpenAPI specification that the schema is taken from. This parameter is only required if the schema contains $ref references to external schemas. |
Here's an example of how to use the sample()
function:
1import { sample } from '@har-sdk/openapi-sampler'; 2 3sample({ 4 type: 'object', 5 properties: { 6 a: { type: 'integer', minimum: 10 }, 7 b: { type: 'string', format: 'password', minLength: 10 }, 8 c: { type: 'boolean' } 9 } 10}); 11// { a: 10, b: 'pa$$word_q', c: true }
In this example, it is generating a sample data object based on a schema that contains three properties (a
, b
, and c
).
Note, when $ref
is used, the spec
object must be provided so that the sampler can resolve the reference:
1import { sample } from '@har-sdk/openapi-sampler'; 2 3const spec = { 4 // ... 5 components: { 6 schemas: { 7 Pet: { 8 type: 'object', 9 properties: { 10 id: { 11 type: 'integer', 12 format: 'int64' 13 }, 14 name: { 15 type: 'string' 16 } 17 } 18 } 19 } 20 } 21}; 22 23sample({ $ref: '#/components/schemas/Pet' }, {}, spec); 24// { id: 42, name: 'lorem' }
By default, the sampler generates values for all properties defined in a schema, regardless of whether they are required or not. If you want to exclude non-required object properties, you can use the skipNonRequired
option as follows:
1sample( 2 { 3 type: 'object', 4 properties: { 5 a: { type: 'string' }, 6 b: { type: 'string' } 7 }, 8 required: ['b'] 9 }, 10 { skipNonRequired: true } 11); 12// { b: 'lorem' }
By default, all properties, including those marked as read or write-only, are included in the generated sample. However, if you want to exclude readOnly
or writeOnly
properties, you can use the skipReadOnly
or skipWriteOnly
options, respectively, as shown below:
1sample( 2 { 3 type: 'object', 4 properties: { 5 a: { type: 'string' }, 6 b: { type: 'string', readOnly: true } 7 } 8 }, 9 { skipReadOnly: true } 10); 11// { a: 'lorem' }
Note, the library recursively generates the entire sample object tree up to a maximum depth of 2 level to prevent infinite recursion. If you want to increase or decrease this maximum depth, you can use the maxSampleDepth
option as follows:
1sample(schema, { maxSampleDepth: 20 });
Also, the library logs console warning messages when it encounters an unsupported schema. If you want to suppress these warning messages, you can use the quiet option as follows:
1sample(schema, { quiet: true });
When the schema comes from the specification which does not allow the example
node to exist e.g. OAS 2.0 parameter definition, some vendors may provide such schema example value in OAS vendor extension nodes namely x-example
or x-examples
. If you want to include such kind of example values into the output, you can use the includeVendorExamples
as shown below:
1sample( 2 { 3 'type': 'string', 4 'x-example': 'some_value' 5 }, 6 { includeVendorExamples: true } 7); 8// some_value
Copyright © 2023 Bright Security.
This project is licensed under the MIT License - see the LICENSE file for details.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
all changesets reviewed
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-14
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More