Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@gahabeen 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

This comment has been minimized.

Copy link

@heypano 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

This comment has been minimized.

Copy link

@abhi-jha 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

This comment has been minimized.

Copy link

@CianMoynihan CianMoynihan commented Apr 20, 2019

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

This comment has been minimized.

Copy link

@oscarigarcia oscarigarcia commented Apr 14, 2020

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

@alicecampkin

This comment has been minimized.

Copy link

@alicecampkin alicecampkin 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

This comment has been minimized.

Copy link

@AxelPotato AxelPotato commented Apr 26, 2020

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

This comment has been minimized.

Copy link

@oscarigarcia oscarigarcia commented Apr 26, 2020

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