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

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.


Solution

  • 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": "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

    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.