简体   繁体   English

为什么json-schema-faker会为所有内容返回拉丁语?

[英]Why does json-schema-faker return Latin for everything?

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) 和一个非常简单的package.json ,唯一的依赖是json-schema-faker (0.5.0.rc16),当我运行以下代码时,我看到底部显示的输出(示例运行)

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 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. 我在这做错了什么。

The exact same thing was happening to me. 发生在我身上的事情完全一样。 I was following along with Cory House's "Building a java script development environment" course on pluralSight. 我跟随Cory House的关于pluralSight的“构建java脚本开发环境”课程。 To stay current with all the dependencies I updated to the latest json-schema-faker version 0.5.0-rc16. 为了与所有依赖关系保持同步,我更新了最新的json-schema-faker版本0.5.0-rc16。

This broke the json generation and I was getting latin for everything. 这打破了json一代,我为所有事情做了拉丁。 When I reverted back to version 0.3.6, then I was generating first name, last name and email correctly. 当我恢复到0.3.6版本时,我正在生成名字,姓氏和电子邮件。

Here is the schema I used : 这是我使用的架构:

  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"]
};

and here is the corresponding java script : 这里是相应的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."));
  }
});

OUTPUT OUTPUT

{
    "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"
        }
    ]
}

But having said all that and getting to work now, and after a bit of googling I found this 但是说了所有这些并现在开始工作,经过一段谷歌搜索后我发现了这一点

0.5.0-RC2 possible bug with faker 'date.past' #275 faker'date.past'#275可能存在0.5.0-RC2错误

So I made these changes to package.json: 所以我对package.json进行了以下更改:

"json-schema-faker": "^0.5.0-rc16",
"faker": "^4.1.0",

and wiped out my node_modules folder and package-lock.json file and did a clean npm install. 并清除我的node_modules文件夹和package-lock.json文件,并进行了干净的npm安装。

I changed the code above to this and re-ran the script with successful results. 我将上面的代码更改为此并重新运行脚本并获得成功结果。

jsf.extend('faker', () => require('faker'));
const json = JSON.stringify(jsf.generate(schema));

The bug report states that 错误报告指出

Hi, since 0.5.x all external generators (chance, faker, etc.) are not built-in, so you need to register as the docs 嗨,因为0.5.x所有外部生成器(机会,faker等)都不是内置的,所以你需要注册为docs

Hope this works for you. 希望这对你有用。

To add to @joe's answer. 添加到@ joe的答案。 I took the following steps 我采取了以下步骤

  1. npm install --save-dev faker json-schema-faker
  2. In data generator file I extended json-schema-faker like so 在数据生成器文件中,我像这样扩展了json-schema-faker
import faker from 'faker'

jsf.extend('faker', () => {return faker});
const json = JSON.stringify(jsf.generate(mockUserSchema));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM