Moshe Shmukler
Moshe Shmukler

Reputation: 1300

Generating test data with Faker

I am working on a script to generate test data using faker and JSON-Schema-Faker packages.

Looking for examples with "schema inheritance" and optional fields. For example, I have a 'user' object with mandatory fields: '_id', 'firstName', 'lastName', 'username', 'email' and one [and only one of]: customerProfile, partnerProfile and adminProfile. Each of optional fields, when present: keeps a non-empty array of values which correspond to another schema.

Please point me to relevant examples.

Upvotes: 0

Views: 528

Answers (1)

Moshe Shmukler
Moshe Shmukler

Reputation: 1300

Ended-up with:

admin: function(first, last){
  return {
    "displayUsername": first + " " + last,
    "active": true
  };
},
...

var profileIndex = Math.round(Math.random() * 2);
var profileTypes = ["admin", "customer", "partner"];
var currentProfileType = profileTypes[profileIndex];
var userRecord = {
  "username": username, 
  "firstName": firstName, 
  "lastName": lastName, 
  "email": email, 
  "_id": _id
  //(ES6 Syntax) ,[currentProfileType + "Profile"]: profileBuilders[currentProfileType](firstName, lastName);
};
userRecord[currentProfileType + "Profile"] = profileBuilders[currentProfileType](firstName, lastName);

return userRecord;

Upvotes: 1

Related Questions