最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does json-schema-faker return Latin for everything? - Stack Overflow

programmeradmin0浏览0评论

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: 'modo 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.

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: 'modo 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.

Share Improve this question asked Oct 21, 2018 at 23:21 Paul JoiremanPaul Joireman 2,8355 gold badges27 silver badges35 bronze badges 1
  • Using the web based tool json-schema-faker.js suggests your schema is as expected, and the example code is the same as yours as far as I can see. Have you considered logging an issue on the github repo? – Relequestual Commented Oct 22, 2018 at 9:01
Add a ment  | 

2 Answers 2

Reset to default 9

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. To stay current with all the dependencies I updated to the latest json-schema-faker version 0.5.0-rc16.

This broke the json generation and I was getting latin for everything. When I reverted back to version 0.3.6, then I was generating first name, last name and email correctly.

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 :

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

{
    "users": [{
            "id": 49569377,
            "firstName": "Gerald",
            "lastName": "Turcotte",
            "email": "[email protected]"
        },

        {
            "id": 84739169,
            "firstName": "Jerad",
            "lastName": "Gerhold",
            "email": "[email protected]"
        },

        {
            "id": 78507259,
            "firstName": "Hayden",
            "lastName": "Schultz",
            "email": "[email protected]"
        }
    ]
}

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

So I made these changes to 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.

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

Hope this works for you.

To add to @joe's answer. 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
import faker from 'faker'

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

评论列表(0)

  1. 暂无评论