如何使用/引用在相同json模式中生成的字段值 [英] How to use / reference field values generated in the same json schema

查看:74
本文介绍了如何使用/引用在相同json模式中生成的字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过结合使用json-serverjson-schema-faker来创建模拟数据.

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

我试图使用$ref属性,但是我知道这仅引用类型,而不是确切的值.

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?

我在mockDataSchema.js文件中拥有的架构是:

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;

在此模式下,我希望在所有用注释// here指示的三个位置中,id都相同.

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

请注意,由于要多次出现tests,所以不能使用enumconst.

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

test2_ids将是一个数组,所以我想将第一个ID的特定ID以及相同类型的其他值包括在内..

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..

test3id中,我只想要与testid完全相同的值.

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?

或者是否有办法更改generateMockData.js文件中的这些数据,而不是更改包含此模式的mockDataSchema.js文件中的数据?

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

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

});

推荐答案

我想在这里分享我找到的解决方案.就我而言,我需要使用json-schema-faker为字段passwordpassword_confirmation生成相同的值,而对我有用的是,在faker库中分配一个新属性,并将新属性名称放入伪造的架构.这里的代码:

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'
}
*/

这篇关于如何使用/引用在相同json模式中生成的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆