Skip to content

Instantly share code, notes, and snippets.

@coryhouse
Last active April 15, 2023 15:09
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save coryhouse/300ed803148caaf9d4f3f45d1a03874d to your computer and use it in GitHub Desktop.
Save coryhouse/300ed803148caaf9d4f3f45d1a03874d to your computer and use it in GitHub Desktop.
Mock Data Schema for "Building a JavaScript Development Environment" on Pluralsight
export const schema = {
"type": "object",
"properties": {
"users": {
"type": "array",
"minItems": 3,
"maxItems": 5,
"items": {
"type": "object",
"properties": {
"id": {
"type": "number",
"unique": true,
"minimum": 1
},
"firstName": {
"type": "string",
"faker": "name.firstName"
},
"lastName": {
"type": "string",
"faker": "name.lastName"
},
"email": {
"type": "string",
"faker": "internet.email"
}
},
"required": ["id", "firstName", "lastName", "email"]
}
}
},
"required": ["users"]
};
@gahabeen
Copy link

gahabeen commented Oct 12, 2017

If anybody comes here using a version from v0.5.x for json-schema-faker you would need to add a piece of code to instanciate faker and maybe set the id parameter type to an integer (instead of number) ;D

In the generateMockData.js file:

import faker from "faker"

jsf.extend("faker", function() {
  return faker
})

@heypano
Copy link

heypano commented Feb 8, 2018

@gahabeen Thank you this was very helpful

The rest of generateMockData.js also needed re-writing based on the spec

jsf.resolve(schema).then(function(result) {
    fs.writeFile(outputFile, JSON.stringify(result, null, 2), function(err){
        if (err) {
            return (console.log(r(err)));
        } else {
            console.log(g(`Mock Data Generated Here: ${outputFile}`));
        }
    });
});

@abhi-jha
Copy link

abhi-jha commented Apr 12, 2018

There's no data coming out from the json file to the webpage on http://localhost:3000/. Any fixes?

http://localhost:3001/users is working fine. Its just that no data is rendered in the table from the db.json file.

@CianMoynihan
Copy link

Working generateMockData.js file below: I also had to run 'npm install faker'

/* This script generates mock data for local development.
*/

/* eslint-disable no-console */
import jsf from 'json-schema-faker';
import {schema} from './mockDataSchema';
import fs from 'fs';
import chalk from 'chalk';
import faker from "faker"

jsf.extend("faker", function() {
return faker
})

var outputFile = './src/api/db.json';

//const json = JSON.stringify(jsf(schema));

jsf.resolve(schema).then(function(result) {
fs.writeFile(outputFile, JSON.stringify(result, null, 2), function(err){
if (err) {
return (console.log(r(err)));
} else {
console.log(Mock Data Generated Here: ${outputFile});
}
});
});

@oscarigarcia
Copy link

I uninstalled gel pack (--save) and it worked!

@aliceridgway
Copy link

aliceridgway commented Apr 16, 2020

Check out the updated branch of Cory's repository:
https://github.com/coryhouse/javascript-development-environment/blob/update-dependencies/buildScripts/generateMockData.js

To get it to work, make sure Faker is installed and included in the package.json.

@AxelPotato
Copy link

For me it kept creating floats instead of whole numbers.
This fix to the number generating part seemed to do the trick.

      id: {
        type: 'number',
        faker: 'random.number',
        unique: true,
        minimum: 1
      },

@oscarigarcia
Copy link

oscarigarcia commented Apr 26, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment