Mock JSON Schema

1.1.2 · active · verified Sun Apr 19

"mock-json-schema" is a JavaScript utility designed to generate deterministic example objects based on JSON Schema definitions. It offers a predictable, non-randomized approach to data generation, making it suitable for testing, documentation, and mock API development. The current stable version is 1.1.2. The library supports various JSON Schema keywords, including `example`, `default`, `anyOf`, `allOf`, and `oneOf`, and provides built-in examples for common string formats like `email`, `uuid`, and `date-time`. It differentiates itself by its minimal API, deterministic output, and comprehensive test coverage. While it includes TypeScript types, a current limitation is its lack of support for `$ref` pointers, which can affect its utility for highly interconnected schemas. Release cadence appears to follow semantic versioning, with minor updates for new features and bug fixes, typically for patches and minor feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to generate a mock object for a complex product schema, leveraging `example`, `default`, and built-in format types for deterministic output.

import { mock } from 'mock-json-schema';

const productSchema = {
  type: 'object',
  properties: {
    id: { type: 'string', format: 'uuid', example: 'a1b2c3d4-e5f6-7890-1234-567890abcdef' },
    name: { type: 'string', default: 'Example Product' },
    price: { type: 'number', minimum: 0, default: 99.99 },
    tags: {
      type: 'array',
      items: { type: 'string' },
      default: ['electronics', 'gadget']
    },
    releaseDate: { type: 'string', format: 'date-time' },
    isAvailable: { type: 'boolean', default: true }
  },
  required: ['id', 'name', 'price']
};

const exampleProduct = mock(productSchema);

console.log(exampleProduct);
/*
Expected output (or similar deterministic structure):
{
  id: 'a1b2c3d4-e5f6-7890-1234-567890abcdef',
  name: 'Example Product',
  price: 99.99,
  tags: [ 'electronics', 'gadget' ],
  releaseDate: '1970-01-01T00:00:00.000Z', // Default for date-time if no example provided
  isAvailable: true
}
*/

view raw JSON →