Registry / testing / mock-json-schema

mock-json-schema

JSON →
library1.1.2jsnpmunverified

"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.

npm install mock-json-schema
INSTALL
IMPORT
SIG · MOCK-JSON-SCHEMA
M
mock-json-schema
testingjavascriptv1.1.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

mock
import { mock } from 'mock-json-schema';
import mock from 'mock-json-schema'; const mock = require('mock-json-schema').default;
The primary mocking function, `mock`, is a named export. Ensure you destructure it when importing, especially in ESM environments, to avoid undefined errors.
mock
const { mock } = require('mock-json-schema');
const mock = require('mock-json-schema');
For CommonJS environments, access the `mock` function by destructuring it from the module's exports object, as it is not a default export.
JSONSchema
import type { JSONSchema } from 'mock-json-schema';
import { JSONSchema } from 'mock-json-schema';
When using TypeScript, import `JSONSchema` as a type-only import (`import type`) to ensure it's removed during compilation, optimizing bundle size and preventing accidental runtime imports.

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 } */
Debug
Known issues
gotchaThe `mock-json-schema` library currently does not support `$ref` pointers within JSON Schemas. If your schema relies on external or internal references for definitions, these will not be resolved, potentially leading to incomplete or incorrect mock objects.
fix
Manually inline referenced schemas or preprocess your schema to resolve `$ref` pointers (e.g., using `json-schema-ref-parser`) before passing it to `mock-json-schema`. This limitation applies to all versions up to 1.1.2.
affects: >=1.0.0
gotchaWhile `mock-json-schema` supports many JSON Schema keywords, it prioritizes `example` and `default` values. For properties where neither is provided, it generates basic type-specific defaults (e.g., `1` for number, `""` for string, `true` for boolean, `1970-01-01T00:00:00.000Z` for `date-time`). This can result in generic or less realistic data if explicit examples or defaults are not defined.
fix
Always provide `example` or `default` values in your JSON Schema for any properties where specific mock data is desired. Review the generated output carefully, especially for fields without explicit definitions, to ensure it meets expectations.
affects: >=1.0.0
gotchaThis library is explicitly designed for *deterministic* output. Unlike some other mocking libraries, it does not introduce any randomness. For a given schema, `mock()` will always produce the identical object. If your use case requires varied or randomized data, `mock-json-schema` alone will not suffice.
fix
Use `mock-json-schema` for establishing a consistent baseline of mock data. If randomized values are needed for specific fields (e.g., in a test suite requiring unique IDs), combine its output with other data generation libraries like `faker.js` for those particular fields.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: mock is not a function
Attempting to import `mock` as a default export when it is a named export, or using incorrect destructuring syntax in CommonJS.
fix
For ES Modules, use `import { mock } from 'mock-json-schema';`. For CommonJS, use `const { mock } = require('mock-json-schema');`. Ensure you're not trying to `import mock from 'mock-json-schema';`.
Generated object does not match schema requirements / Property '...' is missing in mock output
The generated mock data might be too generic due to missing `example` or `default` values in the schema, or the schema itself might be malformed or relying on unsupported features like `$ref` pointers.
fix
Review your JSON Schema to ensure all desired properties have explicit `example` or `default` values. Verify the schema's validity and ensure it doesn't contain `$ref` pointers, as they are not resolved by this library. For `required` fields, provide an `example` or `default` to guarantee their presence.
Type 'number' is not assignable to type 'string' (TypeScript error)
A common type mismatch when the JSON Schema defines a type (e.g., `"type": "string"`) but the provided `example` or `default` value in the schema, or a value from a generated mock, does not conform to that type.
fix
Ensure that the `example` or `default` values within your JSON Schema strictly adhere to the `type` specified for that property. For instance, if `"type": "string"`, `"example": 123` would cause a type error. Correct the schema's example/default values to match the declared type.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
mock-json-schema — npm install mock-json-schema · libregistry