# @stoplight/json-schema-sampler

> Tool for generation samples based on JSON Schema Draft 7

Latest version **0.3.0** (published 2023-07-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @stoplight/json-schema-sampler
pnpm add @stoplight/json-schema-sampler
yarn add @stoplight/json-schema-sampler
bun add @stoplight/json-schema-sampler
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2023-07-17 |
| First published | 2021-04-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 55.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Stoplight |
| Maintainers | damhale, troydm, naumanali__, mmurphy_stoplight, luke.baker-stoplight, treyturner, clanzo, dotslashderek, kaylachun, brendarearden, danielwhite-stoplight, ed-stoplight, vazha, chohmann, wmhilton, mallachari, p0lip, paulatulis, mmiask, falsaffa_stoplight, jharmn, amjcraft, ksoviero-stoplight, mmarti21, matty-v, stoplight-devops |
| Keywords | OpenAPI, JSON Schema, Swagger, instantiator, sampler, faker |

## Links

- npm: https://www.npmjs.com/package/@stoplight/json-schema-sampler
- Repository: https://github.com/stoplightio/json-schema-sampler
- Homepage: https://github.com/stoplightio/json-schema-sampler/
- Issues: https://github.com/stoplightio/json-schema-sampler/issues
- npm.io page: https://npm.io/package/@stoplight/json-schema-sampler

## Dependencies (2)

- [json-pointer](https://npm.io/package/json-pointer.md) ^0.6.1
- [@types/json-schema](https://npm.io/package/@types/json-schema.md) ^7.0.7

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.3.0 (latest) — 2023-07-17
- 0.2.3 — 2022-11-17
- 0.2.2 — 2021-10-13
- 0.2.1 — 2021-09-21
- 0.2.0 — 2021-04-26
- 0.1.0 — 2021-04-21
- 0.0.0 — 2021-04-15

## README

# @stoplight/json-schema-sampler

It's a fork of [openapi-sampler](https://github.com/Redocly/openapi-sampler) by Redocly, with focus on supporting JSON Schema Draft 7.

Tool for generation samples based on JSON Schema Draft 7.

## Features

- Deterministic (given a particular input, will always produce the same output)
- Supports compound keywords: `allOf`, `oneOf`, `anyOf`, `if/then/else`
- Supports `additionalProperties`
- Uses `default`, `const`, `enum` and `examples` where possible
- Good array support: supports `contains`, `minItems`, `maxItems`, and tuples (`items` as an array)
- Supports `minLength`, `maxLength`, `min`, `max`, `exclusiveMinimum`, `exclusiveMaximum`
- Supports the following `string` formats:
  - email
  - 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](https://www.npmjs.com/package/json-schema-faker#inferred-types)
- Support for `$ref` resolving

## Installation

Install using [npm](https://docs.npmjs.com/getting-started/what-is-npm)

    npm install @stoplight/json-schema-sampler --save

or using [yarn](https://yarnpkg.com)

    yarn add @stoplight/json-schema-sampler

Then require it in your code:

```js
const JSONSchemaSampler = require('@stoplight/json-schema-sampler');
```

## Usage
#### `JSONSchemaSampler.sample(schema, [options], [spec])`
- **schema** (_required_) - `object`
A JSON Schema Draft 7 document.
- **options** (_optional_) - `object`
Available options:
  - **skipNonRequired** - `boolean`
  Don't include non-required object properties not specified in [`required` property of the schema object](https://swagger.io/docs/specification/data-models/data-types/#required)
  - **skipReadOnly** - `boolean`
  Don't include `readOnly` object properties
  - **skipWriteOnly** - `boolean`
  Don't include `writeOnly` object properties
  - **quiet** - `boolean`
  Don't log console warning messages
  - **maxSampleDepth** - `number`
    Max depth sampler should traverse
- **doc** - the whole schema where the schema is taken from. Might be useful when the `schema` passed is only a portion of the whole schema and $refs aren't resected. **doc** must not contain any external references

## Example
```js
const JSONSchemaSampler = require('@stoplight/json-schema-sampler');
JSONSchemaSampler.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' }
```

---
_Source: https://npm.io/package/@stoplight/json-schema-sampler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
