0

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.

Moshe Shmukler
  • 1,270
  • 2
  • 21
  • 43
  • If they're all "profiles" and always contain a single entity, why not reference as a `profile` property rather than each in their own property? Would simplify your schema in my opinion and mean less checking for nulls/empties. This way, the schema contained within would tell you what type of profile it is. Just a thought, I may have missed something though. – timothyclifford Dec 08 '15 at 15:40

1 Answers1

1

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;
Moshe Shmukler
  • 1,270
  • 2
  • 21
  • 43