Just a small open-source script to create fake data given a simple JSON model.
This is a tiny package motivated by the need of generating certain amount of fake data to populate backend fixtures. We started implementing and editing a single .js
file with specific characteristics of some backend models and the desired amount we wanted to generate until we ended up with something like this. We personally decided to use the output files in the API endpoints of a test server but you could use them any way you like, they're just .json
files.
There are a few ways you can get this library installed:
# clone our project or fork your own
git clone https://github.com/Cambalab/fake-data-generator.git
# install dependencies
npm install
# install it as a dependency or dev-dependency of our own project
npm install --save-dev fake-data-generator
# install it globally
npm install -g fake-data-generator
Write a .json
model in the models
directory. Article Example
Run the generate
script from a terminal
The following command writes a .json file with an array of 50 elements to the output directory, where:
example
: is the name of your model.json
file.10
: the numbers of models to generate.example.json
: the name of the output file.npm run generate example 50 example.json
.json
file or a javascript Object
amountArg:
Number
modelArg:
Object | Json file
json
, modelArg behaves as a file path to that json file. For object
inputType values, modelArg behaves like a javascript object, where the model should be defined.fileName:
String
json
fileName will describe the output path where the file will be writen to.inputType:
String
object | json
outputType:
String
object | json
// Requires the package
const { generateModel } = require('fake-data-generator')
// Requires a model
const model = require('./models/example.json')
// Generate the model
const amountArg = 50
const modelArg = model
const inputType = 'object'
const outputType = 'object'
const generatedModel = generateModel({ amountArg, modelArg, inputType, outputType })
Note that when using
required
orimport
on a.json
file the returned value behaves like a javascript Object.
Create a models
directory, an output
directory and write a .json
model as explained before.
mkdir models
mkdir output
Run the global npm bin script.
fake-data-generator example 10 example.json
Type: (optional) Object
Details: general configuration.
Properties:
faker.locale
.
Type: (optional) Number
Details: an amount of objects to generate.
When this value is present, the amount value given from a cli or the generateModel function from the npm package is overwritten.
Type: Object
Details: A declaration of your object model
Properties:
id
faker
, randomNumberBetween
, Object
, Array
.A valid format would be an object with the following keys:
Currently the script supports faker methods that return Date
, String
or Number
data only. It's not ready to handle faker methods that receive arguments yet.
If you're not familiar with faker
, take a look at their docs, it's really simple to use.
Any other faker method can be used in the value attribute like this:
suppose we want to generate a company attribute with faker, then we would declare in the model:
{
"company": {
"type": "faker",
"value": "company.companyName"
}
}
This is simply a pass-through for those occasions when a known value is desired.
value: any
Case with a String
{
"operating_system": {
"type": "Literal",
"value": "Linux"
}
}
Case using an Array
of elements
{
"resources": {
"type": "Literal",
"value": ["memory", "disk", "network", "cpu"]
}
}
This is how the script knows we want to nest objects
say we want to declare a more complex company model:
value: Object
an object with a type, value, options structure
{
"company": {
"type": "Object",
"value": {
"name": {
"type": "faker",
"value": "company.companyName"
},
"address": {
"type": "Object",
"value": {
"street": {
"type": "faker",
"value": "address.streetAddress"
},
"city": {
"type": "faker",
"value": "address.city"
},
"state": {
"type": "faker",
"value": "address.state"
}
}
}
}
}
}
The script provides a simple way to get a random number between a range of numbers
value: Array<Number>
a range of values to compute the random number
{
"timesIWatchedNicolasCageMovies": {
"type": "randomNumberBetween",
"value": [150, 2587655]
}
}
The script provides a simple way to get a random element from an array of options.
value: Array
a list of options to pick from.
{
"whichMovieToWatchTonight": {
"type": "randomElementInArray",
"value": ["Frozen", "Mulan", "The Lion King", "Aladdin", "Pulp Fiction"]
}
}
output
{
"whichMovieToWatchTonight": "Pulp Fiction"
}
This one returns a random group of elements from an array of options.
value: Array
a list of options to pick from.
{
"whichMoviesToWatchTonight": {
"type": "randomElementsInArray",
"value": ["Frozen", "Mulan", "The Lion King", "Aladdin", "Pulp Fiction"]
}
}
output
{
"whichMoviesToWatchTonight": ["Pulp Fiction", "Aladdin"]
}
Just another version of randomNumberBetween that accepts a range of numbers, a prefix as a string and a suffix as a string
options:
prefix: String
a value to be interpolated as the number prefixsuffix: String
a value to be interpolated as the number suffix{
"publication": {
"type": "randomNumberBetweenWithString",
"value": [1, 2500000],
"options": {
"prefix": "#",
"suffix": "*"
}
}
}
You can get incremental numbers based on the given amount for a model
The
value
attribute is ignored
options:
from: Number
starts incrementing from a given number{
"brownies": {
"type": "incrementNumber",
"options": {
"from": 420
}
}
}
Output using an amount of 3:
[
{
"brownies": 420
},
{
"brownies": 421
},
{
"brownies": 422
},
]
Defines an Array
of elements to be created with the same type.
options
size: Number
How many objects to create. Required, is mutually exclusive with size: Array
size: Array
A two value array where the first value is the minimum number of entries and the second is the maximum. Required, is mutually exclusive with size: Number
Extending the company model a little further:
as a Number
{
"company": {
"type": "Object",
"value": {
"name": {
"type": "faker",
"value": "company.companyName"
},
"addresses": {
"type": "Array",
"options": {
"size": 10
},
"value": {
"type": "Object",
"value": {
"street": {
"type": "faker",
"value": "address.streetAddress"
},
"city": {
"type": "faker",
"value": "address.city"
},
"state": {
"type": "faker",
"value": "address.state"
}
}
}
}
}
}
}
as an Array
{
"company": {
"type": "Object",
"value": {
"name": {
"type": "faker",
"value": "company.companyName"
},
"addresses": {
"type": "Array",
"options": {
"size": [5, 20]
},
"value": {
"type": "Object",
"value": {
"street": {
"type": "faker",
"value": "address.streetAddress"
},
"city": {
"type": "faker",
"value": "address.city"
},
"state": {
"type": "faker",
"value": "address.state"
}
}
}
}
}
}
}
Adds a fixed String
in front of another dynamic value generated by one of the other datatypes.
options
text: String
The text to be prepended. required
{
"issue": {
"type": "prepend",
"options": {"text": "#"},
"value": {
"type": "randomNumberBetween",
"value": [1, 2500]
}
}
}
Adds a fixed String
at the back of another dynamic value generated by one of the other datatypes.
options
text: String
The text to be appended. required
{
"fileName": {
"type": "append",
"options": {"text": ".pdf"},
"value": {
"type": "faker",
"value": "random.words"
}
}
}
Please make sure to read the Contributing Guide before submitting pull requests. There you'll find development environment instructions, common scripts and the project structure summary.
Feel free to open an issue if any faker method is not working as expcected or if you would like support for another data generator module.
GNU General Public License version 3
👩💻 With :green_heart: :purple_heart: :heart: by Cambá Coop :earth_americas: Buenos Aires, Argentina