使用以下架构:
和一个非常简单的package.json
,唯一的依赖项是json-schema-faker
(0.5.0.rc16),当我运行以下代码时,我看到输出显示在底部(示例运行)< / p>
jsf = require('json-schema-faker');
var schema = {
"type": "object",
"properties": {
"users": {
"type": "array",
"minItems": 3,
"maxItems": 5,
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"unique": true,
"minimum": 1
},
"firstName": {
"type": "string",
"faker": "name.findName"
},
"lastName": {
"type": "string",
"faker": "name.lastName"
},
"email": {
"type": "string",
"faker": "internet.email"
}
},
"required": ["id", "firstName", "lastName", "email"]
}
}
},
"required": ["users"]
};
var mylist = jsf.generate(schema);
console.log("mylist: ", mylist);
输出
mylist: { users:
[ { id: 46919647,
firstName: 'commodo ut deserunt',
lastName: 'magna',
email: 'ex minim irure' },
{ id: 36864773,
firstName: 'aliquip elit laborum',
lastName: 'co',
email: 'nisi Ut laboris dolore' },
{ id: 62231151,
firstName: 'adipisicing id reprehenderit exercitation',
lastName: 'tempor culpa deserunt Excepteur nisi',
email: 'est enim' },
{ id: 57427341,
firstName: 'eu ullamco reprehenderit mollit',
lastName: 'cupidatat ut non',
email: 'id dolore sed et' } ] }
为什么所有拉丁语都用?我在这里做错什么了。
答案 0 :(得分:4)
完全相同的事情发生在我身上。我跟随Cory House的“ PositiveSight”上的“构建Java脚本开发环境”课程。为了与所有依赖关系保持最新,我更新到了最新的json-schema-faker版本0.5.0-rc16。
这打破了json一代,我在所有事情上都变得拉丁了。当我恢复到0.3.6版时,则正确生成了名字,姓氏和电子邮件。
这是我使用的架构:
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"]
};
这是相应的Java脚本:
import jsf from 'json-schema-faker';
import {schema} from './mockDataSchema';
import fs from 'fs';
import chalk from 'chalk';
const json = JSON.stringify(jsf(schema));
fs.writeFile("./src/api/db.json", json, function (err) {
if (err) {
return console.log(chalk.red(err));
} else {
console.log(chalk.green("Mock data generated."));
}
});
输出
{
"users": [{
"id": 49569377,
"firstName": "Gerald",
"lastName": "Turcotte",
"email": "Eda_Lemke66@hotmail.com"
},
{
"id": 84739169,
"firstName": "Jerad",
"lastName": "Gerhold",
"email": "Reynold.Ryan@yahoo.com"
},
{
"id": 78507259,
"firstName": "Hayden",
"lastName": "Schultz",
"email": "Kassandra64@yahoo.com"
}
]
}
但是说了这么多就可以开始工作了,经过一番谷歌搜索,我发现了这个问题
0.5.0-RC2 possible bug with faker 'date.past' #275
所以我对package.json进行了这些更改:
"json-schema-faker": "^0.5.0-rc16",
"faker": "^4.1.0",
并清除了我的node_modules文件夹和package-lock.json文件,并进行了干净的npm安装。
我将上面的代码更改为此,然后重新运行脚本并获得成功的结果。
jsf.extend('faker', () => require('faker'));
const json = JSON.stringify(jsf.generate(schema));
错误报告指出
您好,由于0.5.x并不是所有外部生成器(机会,伪造者等)都是内置的,因此您需要注册为文档
希望这对您有用。
答案 1 :(得分:0)
添加到@joe的答案中。我采取了以下步骤
npm install --save-dev faker json-schema-faker
json-schema-faker
import faker from 'faker'
jsf.extend('faker', () => {return faker});
const json = JSON.stringify(jsf.generate(mockUserSchema));