Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
json-schema-faker
Advanced tools
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.
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);
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.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 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 fake data for your system. Note that json-schema-faker
supports (currently) the JSON-Schema specification draft-04 only.
See online demo.
Install json-schema-faker
with npm:
npm install json-schema-faker --save-dev
JSON-schema-faker (or jsf
for short) combines two things:
var jsf = require('json-schema-faker');
var schema = {
type: 'object',
properties: {
user: {
type: 'object',
properties: {
id: {
$ref: '#/definitions/positiveInt'
},
name: {
type: 'string',
faker: 'name.findName'
},
email: {
type: 'string',
format: 'email',
faker: 'internet.email'
}
},
required: ['id', 'name', 'email']
}
},
required: ['user'],
definitions: {
positiveInt: {
type: 'integer',
minimum: 0,
exclusiveMinimum: true
}
}
};
var sample = jsf(schema);
console.log(sample.user.name);
// output: John Doe
Inline references are fully supported (json-pointers) but external can't be resolved by json-schema-faker
.
In order to achieve that you can use refaker and then use the resolved schemas:
var schema = {
type: 'object',
properties: {
someValue: {
$ref: 'otherSchema'
}
}
};
var refs = [
{
id: 'otherSchema',
type: 'string'
}
];
var sample = jsf(schema, refs);
console.log(sample.someValue);
// output: voluptatem
json-schema-faker
has built-in generators for core-formats, Faker.js and Chance.js are also supported.
You can use faker or chance properties but they are optional:
{
"type": "string",
"faker": "internet.email"
}
The above schema will invoke:
require('faker').internet.email();
Another example is passing arguments to the generator:
{
"type": "string",
"chance": {
"email": {
"domain": "fake.com"
}
}
}
And will invoke:
var Chance = require('chance'),
chance = new Chance();
chance.email({ "domain": "fake.com" });
If you pass an array, they will be used as raw arguments.
Note that both generators has higher precedence than format.
You can also use standard JSON Schema keywords, e.g. pattern
:
{
"type": "string",
"pattern": "yes|no|maybe|i don't know"
}
Additionally, you can add custom generators for those:
jsf.formats('semver', function(gen, schema) {
return gen.randexp('^\\d\\.\\d\\.\\d{1,2}$');
});
Now that format can be generated:
{
"type": "string",
"format": "semver"
}
Usage:
Callback:
Note that custom generators has lower precedence than core ones.
You may extend Faker.js:
var jsf = require('json-schema-faker');
jsf.extend('faker', function(faker){
faker.locale = "de"; // or any other language
faker.custom = {
statement: function(length) {
return faker.name.firstName() + " has " + faker.finance.amount() + " on " + faker.finance.account(length) + ".";
}
};
return faker;
});
var schema = {
"type": "string",
"faker": {
"custom.statement": [19]
}
}
var sample = jsf(schema);
or if you want to use faker's individual localization packages, simply do the following:
jsf.extend('faker', function() {
// just ignore the passed faker instance
var faker = require('faker/locale/de');
// do other stuff
return faker;
});
You can also extend Chance.js, using built-in chance.mixin function:
var jsf = require('json-schema-faker');
jsf.extend('chance', function(chance){
chance.mixin({
'user': function() {
return {
first: chance.first(),
last: chance.last(),
email: chance.email()
};
}
});
return chance;
});
var schema = {
"type": "string",
"chance": "user"
}
var sample = jsf(schema);
The first parameter of extend
function is the generator name (faker
or chance
). The second one is the function that accepts the dependency library; the function alters the library and returns it.
JSON Schema does not require you to provide the type
property for your JSON Schema documents and document fragments.
But since jsf
uses the type
property to create the proper fake data, we attempt to infer the type whenever it is not provided. We do this based on the JSON Schema validation properties you use.
Now this means that if you do not use any of the JSON Schema validation properties, json-schema-faker will not be able to infer the type for you and you will need to explicitly set your
type
manually.)
Below is the list of JSON Schema validation properties and the inferred type based on the property:
array
additionalItems
items
maxItems
minItems
uniqueItems
integer (Number uses the same properties so if you need number
, set your type
explicitly)
exclusiveMaximum
exclusiveMinimum
maximum
minimum
multipleOf
object
additionalProperties
dependencies
maxProperties
minProperties
patternProperties
properties
required
string
maxLength
minLength
pattern
Use grunt-jsonschema-faker
to automate running json-schema-faker
against your JSON schemas.
jsf
Actually, I've found some projects or services:
Many of they are incomplete (?), so I decided to code this library.
Any contribution is well received, please see contribution guide.
FAQs
JSON-Schema + fake data generators
The npm package json-schema-faker receives a total of 195,616 weekly downloads. As such, json-schema-faker popularity was classified as popular.
We found that json-schema-faker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.