How to use / reference field values generated in the same json schema

℡╲_俬逩灬. 提交于 2019-12-25 01:01:45

问题


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

});

来源:https://stackoverflow.com/questions/54940463/how-to-use-reference-field-values-generated-in-the-same-json-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!