2

I am trying to create mock data by using the json-server in combination with the json-schema-faker.

I was trying to use the $ref property but I understood that this only references the type and not the exact value.

Is there a way to reuse the exact same value instead of just its type?

The schema I have in mockDataSchema.js file is:

var schema =
{
    "title": "tests",
    "type": "object",
    "required": [
        "test"
    ],
    "properties": {
        "test": {
            "type": "object",
            "required": [
                "id",
                "test2_ids",
                "test3"
            ],
            "properties": {
                "id": {
                    "type": "string",
                    "faker": "random.uuid" // here
                },
                "test2_ids": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "faker": "random.uuid" // here
                    }
                },
                "test3": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "id": {
                                "type": "string",
                                "faker": "random.uuid" // here
                            }
                        }
                    }
                }
            }
        }
    }
};

module.exports = schema;

From this schema I want the id to be the same in all three locations which i have indicated with the comment // here.

Note that I can't use an enum or const as I want to have multiple tests occurrences.

test2_ids will be an array so i would like to include that specific id for the first id and other values of the same type as well..

In the id of test3 i just want the exact same value as the id of test.

Is it feasible what I am trying to achieve?

Or is there a way to change these data in the generateMockData.js file instead of the mockDataSchema.js which includes this schema ?

My generateMockData.js :

var jsf = require('json-schema-faker');
var mockDataSchema = require('./mockDataSchema');
var fs = require('fs');

var json = JSON.stringify(jsf(mockDataSchema));

fs.writeFile("./src/api/db.json", json, function (err) {
  if (err) {
    return console.log(err);
  } else {
    console.log("Mock data generated.");
  }

});
Marialena
  • 817
  • 8
  • 31

1 Answers1

1

I'd like to share here a solution that I found. In my case, I required to generate the same value for the fields password and password_confirmation using json-schema-faker, and something that worked for me was, assign a new property in the faker library and put the new property name inside the faker schema. Here the code:

import faker from 'faker';
import jsf from 'json-schema-faker';

// here we generate a random password using faker
const password = faker.internet.password();
// we assign the password generated to a non-existent property, basically here you create your own property inside faker to save the constant value that you want to use.
faker.internet.samePassword = () => password;
// now we specify that we want to use faker in jsf
jsf.extend('faker', () => faker);
// we create the schema specifying the property that we have created above
const fakerSchema = {
   type: 'object',
   properties: {
      password: {
         faker: 'internet.samePassword',
         type: 'string'
      },
      password_confirmation: {
         faker: 'internet.samePassword',
         type: 'string'
      }
   }
};
// We use the schema defined and voilá!
let dataToSave = await jsf.resolve(fakerSchema);


/*
This is the data generated
{
   password: 'zajkBxZcV0CwrFs',
   password_confirmation: 'zajkBxZcV0CwrFs'
}
*/