{"id":11344,"library":"mock-json-schema","title":"Mock JSON Schema","description":"\"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.","status":"active","version":"1.1.2","language":"javascript","source_language":"en","source_url":"https://github.com/anttiviljami/mock-json-schema","tags":["javascript","mock","json schema","json","schema","example","instantiate","swagger","openapi","typescript"],"install":[{"cmd":"npm install mock-json-schema","lang":"bash","label":"npm"},{"cmd":"yarn add mock-json-schema","lang":"bash","label":"yarn"},{"cmd":"pnpm add mock-json-schema","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary mocking function, `mock`, is a named export. Ensure you destructure it when importing, especially in ESM environments, to avoid undefined errors.","wrong":"import mock from 'mock-json-schema';\nconst mock = require('mock-json-schema').default;","symbol":"mock","correct":"import { mock } from 'mock-json-schema';"},{"note":"For CommonJS environments, access the `mock` function by destructuring it from the module's exports object, as it is not a default export.","wrong":"const mock = require('mock-json-schema');","symbol":"mock","correct":"const { mock } = require('mock-json-schema');"},{"note":"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.","wrong":"import { JSONSchema } from 'mock-json-schema';","symbol":"JSONSchema","correct":"import type { JSONSchema } from 'mock-json-schema';"}],"quickstart":{"code":"import { mock } from 'mock-json-schema';\n\nconst productSchema = {\n  type: 'object',\n  properties: {\n    id: { type: 'string', format: 'uuid', example: 'a1b2c3d4-e5f6-7890-1234-567890abcdef' },\n    name: { type: 'string', default: 'Example Product' },\n    price: { type: 'number', minimum: 0, default: 99.99 },\n    tags: {\n      type: 'array',\n      items: { type: 'string' },\n      default: ['electronics', 'gadget']\n    },\n    releaseDate: { type: 'string', format: 'date-time' },\n    isAvailable: { type: 'boolean', default: true }\n  },\n  required: ['id', 'name', 'price']\n};\n\nconst exampleProduct = mock(productSchema);\n\nconsole.log(exampleProduct);\n/*\nExpected output (or similar deterministic structure):\n{\n  id: 'a1b2c3d4-e5f6-7890-1234-567890abcdef',\n  name: 'Example Product',\n  price: 99.99,\n  tags: [ 'electronics', 'gadget' ],\n  releaseDate: '1970-01-01T00:00:00.000Z', // Default for date-time if no example provided\n  isAvailable: true\n}\n*/","lang":"typescript","description":"Demonstrates how to generate a mock object for a complex product schema, leveraging `example`, `default`, and built-in format types for deterministic output."},"warnings":[{"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.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"While `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"This 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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';`.","cause":"Attempting to import `mock` as a default export when it is a named export, or using incorrect destructuring syntax in CommonJS.","error":"TypeError: mock is not a function"},{"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.","cause":"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.","error":"Generated object does not match schema requirements / Property '...' is missing in mock output"},{"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.","cause":"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.","error":"Type 'number' is not assignable to type 'string' (TypeScript error)"}],"ecosystem":"npm"}