"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-schemaVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to generate a mock object for a complex product schema, leveraging `example`, `default`, and built-in format types for deterministic output.
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.
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.
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.
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';`.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.
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.
No dependency data recorded yet.