What is json-schema-faker?
The json-schema-faker npm package is a tool that generates fake data based on JSON Schema definitions. It is useful for testing, prototyping, and mocking APIs.
What are json-schema-faker's main functionalities?
Basic JSON Schema Support
Generates fake data based on a simple JSON Schema. The schema defines an object with 'name' and 'age' properties, and json-schema-faker generates a corresponding fake object.
const jsf = require('json-schema-faker');
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' }
},
required: ['name', 'age']
};
const fakeData = jsf.generate(schema);
console.log(fakeData);
Custom Formats
Allows the creation of custom formats for generating specific types of fake data. In this example, a custom format 'customFormat' is defined to always return 'customValue'.
const jsf = require('json-schema-faker');
jsf.format('customFormat', () => 'customValue');
const schema = {
type: 'object',
properties: {
customField: { type: 'string', format: 'customFormat' }
}
};
const fakeData = jsf.generate(schema);
console.log(fakeData);
Extending with Faker.js
Integrates with Faker.js to use its extensive library of fake data generators. In this example, the schema uses Faker.js to generate a fake email address.
const jsf = require('json-schema-faker');
const faker = require('faker');
jsf.extend('faker', () => faker);
const schema = {
type: 'object',
properties: {
email: { type: 'string', faker: 'internet.email' }
}
};
const fakeData = jsf.generate(schema);
console.log(fakeData);
Other packages similar to json-schema-faker
faker
Faker.js is a popular library for generating fake data. It provides a wide range of data types and is highly customizable. Unlike json-schema-faker, it does not use JSON Schema definitions but offers a rich API for generating fake data directly.
chance
Chance.js is another library for generating random data. It is lightweight and provides a simple API for generating various types of random data. Like Faker.js, it does not use JSON Schema definitions but offers a straightforward way to generate random data.
mockaroo
Mockaroo is an online tool and API for generating fake data. It supports a wide range of data types and allows users to define schemas using a web interface. It is more user-friendly for non-developers compared to json-schema-faker.
Use JSON Schema along with fake generators to provide consistent and meaningful fake data for your system.
What's next?
Breaking-changes towards v0.5.x
API:
- deprecated — You will not longer be able to call
jsf()
and get a fully-dereferenced result. It will just generate given refs and inline ones, nothing else.
jsf.generate()
is the sync-version, with partial dereferencing through given refs, etc.jsf.resolve()
is the async-version, with full dereferencing, given refs are also supported.
- deprecated — TypeScript sources are not longer used, however
d.ts
definitions will be updated on time.
Usage info from README.md was moved to docs/
, read more.
Contributors
— Artwork by Ajay Karat
We are more than happy to welcome new contributors, our project is still being developed, but we need more feedback!
Please see our contribution guide to learn how.
We are looking for your help!
We have a gitter room for this project, if you want to contribute, talk about specific issues from the library, or you need help on json-schema topics just reach out to us!
Please take a look at the technical documentation page.
If you think json-schema-faker
is adding value on what you're doing please consider buying me a coffee.
Also, you can support the project via Open Collective.