How to use / reference field values generated in t

2019-08-18 23:53发布

问题:

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.");
  }

});