2

Using the following schema:

and a very simple package.json with the only dependency being json-schema-faker (0.5.0.rc16), when I run the following code I see the output shown at the bottom (an example run)

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);

OUTPUT

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' } ] }

Why is everything in Latin? What am I doing wrong here.

4

2 回答 2

9

同样的事情也发生在我身上。我跟随 Cory House 在pluralSight 上的“构建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 可能带有 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 所有外部生成器(机会、伪造者等)都不是内置的,因此您需要注册为文档

希望这对你有用。

于 2018-11-09T19:40:05.017 回答
0

添加到@joe的答案。我采取了以下步骤

  1. npm install --save-dev faker json-schema-faker
  2. json-schema-faker在我这样扩展的数据生成器文件中
import faker from 'faker'

jsf.extend('faker', () => {return faker});
const json = JSON.stringify(jsf.generate(mockUserSchema));
于 2019-06-27T23:37:41.827 回答