Skip to content

Commit

Permalink
Add an option resolveRef to support ref
Browse files Browse the repository at this point in the history
Add an option to import ref schema in order to fully support
linked schema.
  • Loading branch information
BenjaminVanRyseghem committed Oct 2, 2019
1 parent 58e2931 commit ab884b2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Generates dummy object from `model`
autoDetect: Boolean,
applyFilter: Boolean,
returnDate: Boolean,
resolveRef: Function,
custom: {
email: [String, Array, Object],
phone: [String, Array, Object],
Expand All @@ -33,6 +34,7 @@ Generates dummy object from `model`
| autoDetect | Boolean | Attempt to detect e-mail, phone, or password and generate corresponding random data, defaults to true |
| applyFilter | Boolean | Apply lowercase, uppercase, and trim filters on generated object if defined in the path |
| returnDate | Boolean | Weather to return dates as Date or String |
| resolveRef | Function | Function taking a schema ref as arguments, and returning the *absolute* path to its schema file |
| custom | Object | Special generator for specified fields |
| custom.email | String, Array, or Object | String (field to generate a random e-mails), Array of Strings (fields to generate a random e-mail), or Object `{field: String or Array of String, value: Function (custom generator function)}`
| custom.phone | String, Array, or Object | String (field to generate a random phones), Array of Strings (fields to generate a random phone), or Object `{field: String or Array of String, value: Function (custom generator function)}`
Expand Down
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,9 @@ let generateRandomModel = (paths, opts) => {
generated[field][firstCity] = faker.helpers.createCard();
generated[field][secondCity] = faker.helpers.createCard();
} else if (type === "objectid") { /* Handle ObjectId*/
if (desc.ref) {
generated[field] = mongoose.Types.ObjectId().toString()
if (desc.ref && opts.resolveRef) {
let subSchema = require(opts.resolveRef(desc.ref));
generated[field] = getPathsAndGenerate(subSchema, opts);
} else {
generated[field] = mongoose.Types.ObjectId().toString()
}
Expand Down Expand Up @@ -352,7 +353,7 @@ let generateRandomModel = (paths, opts) => {
*
* Returns the generated dummy object
*/
let getPathsAndGenerate = (model, opts) => {
function getPathsAndGenerate(model, opts) {
let paths = getPaths(model);
return generateRandomModel(paths, opts);
}
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ describe('mongoose-dummy', () => {
parent: {
type: mongoose.Schema.Types.ObjectId
},
school: {
type: mongoose.Schema.Types.ObjectId,
ref: "School"
},
detail: {
main_info: String,
some_info: String,
Expand All @@ -65,6 +69,7 @@ describe('mongoose-dummy', () => {
let randomObject = dummy(model, {
ignore: ignoredFields,
returnDate: true,
resolveRef: ref => `${__dirname}/${ref.toLowerCase()}.js`,
force: {
parent: '5af8a4f33f56930349d8f45b'
}
Expand All @@ -81,6 +86,8 @@ describe('mongoose-dummy', () => {
randomObject.results[0].should.have.property('score');
randomObject.is_student.should.be.a('boolean');
randomObject.parent.should.equal('5af8a4f33f56930349d8f45b')
randomObject.school.name.should.be.a('string');
randomObject.school.description.should.be.a('string');
isObjectId(randomObject.parent).should.be.true;

// Check ignore fields
Expand Down
15 changes: 15 additions & 0 deletions test/school.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require("mongoose");
const { Schema } = mongoose;

const schema = new Schema({
name: {
type: String,
required: true
},
description: {
type: String,
required: true
}
});

module.exports = schema;

0 comments on commit ab884b2

Please sign in to comment.