-
-
Save coryhouse/300ed803148caaf9d4f3f45d1a03874d to your computer and use it in GitHub Desktop.
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 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}`));
}
});
});
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.
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}
);
}
});
});
I uninstalled gel pack (--save) and it worked!
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.
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
},
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: