# mock-json-schema

> Simple utility to mock example objects based on JSON schema definitions

Latest version **1.1.2** (published 2025-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install mock-json-schema
pnpm add mock-json-schema
yarn add mock-json-schema
bun add mock-json-schema
```

## Health

**Score 55/100 (C)** — status: stable.

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2025-09-12 |
| First published | 2018-11-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | Viljami Kuosmanen |
| Maintainers | anttiviljami |
| Keywords | mock, json schema, json, schema, example, instantiate, swagger, openapi, typescript |

## Links

- npm: https://www.npmjs.com/package/mock-json-schema
- Repository: https://github.com/anttiviljami/mock-json-schema
- Issues: https://github.com/anttiviljami/mock-json-schema/issues
- npm.io page: https://npm.io/package/mock-json-schema

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## 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

- 1.1.2 (latest) — 2025-09-12
- 1.1.1 — 2021-06-10
- 1.1.0 — 2021-06-10
- 1.0.8 — 2020-04-17
- 1.0.7 — 2020-04-17
- 1.0.6 — 2020-04-17
- 1.0.5 — 2018-12-04
- 1.0.4 — 2018-11-18
- 1.0.3 — 2018-11-17
- 1.0.2 — 2018-11-17
- 1.0.1 — 2018-11-17
- 1.0.0 — 2018-11-17

## README

# mock-json-schema
[![CI](https://github.com/anttiviljami/mock-json-schema/workflows/CI/badge.svg)](https://github.com/anttiviljami/mock-json-schema/actions?query=workflow%3ACI)
[![npm version](https://img.shields.io/npm/v/mock-json-schema.svg)](https://www.npmjs.com/package/mock-json-schema)
[![npm downloads](https://img.shields.io/npm/dw/mock-json-schema)](https://www.npmjs.com/package/mock-json-schema)
[![License](http://img.shields.io/:license-mit-blue.svg)](https://github.com/anttiviljami/mock-json-schema/blob/master/LICENSE)
[![Buy me a coffee](https://img.shields.io/badge/donate-buy%20me%20a%20coffee-orange)](https://buymeacoff.ee/anttiviljami)

Simple utility to mock example objects based on JSON schema definitions

## Features

- [x] Minimal & deterministic. Predictable single example with no randomisation involved
- [x] Thoroughly [tested](https://github.com/anttiviljami/mock-json-schema/blob/master/src/mock.test.ts) feature set
- [x] Supports `example`, `default`
- [x] Supports `anyOf`, `allOf`, `oneOf`
- [x] Built-in examples for following string formats:
	- `email`
	- `hostname`
	- `ipv4`
	- `ipv6`
	- `uri`
	- `uri-reference`
	- `uri-template`
	- `json-pointer`
	- `date-time`
	- `uuid`
- [x] TypeScript types included
- [ ] Supports $ref pointers

## Usage

```javascript
const { mock } = require('mock-json-schema');
const assert = require('assert');

const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      id: {
        type: 'integer',
        minimum: 1,
      },
      name: {
        type: 'string',
        example: 'John Doe',
      },
      email: {
        type: 'string',
        format: 'email',
      },
    },
  },
};

assert.deepEqual(mock(schema), [{ id: 1, name: 'John Doe', email: 'user@example.com' }]);
```

View more [examples](https://github.com/anttiviljami/mock-json-schema/blob/master/src/mock.test.ts)

## Contributing

mock-json-schema is Free and Open Source Software. Issues and pull requests are more than welcome!

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