Skip to content

Commit ef642db

Browse files
authored
Merge pull request #38 from makinde/dynamic_refs
Adding support for dynamic refs
2 parents 905cccb + 49fa236 commit ef642db

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

lib/id-validator.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ IdValidator.prototype.validateSchema = function (
4141

4242
var validateFunction = null
4343
var refModelName = null
44+
var refModelPath = null
4445
var conditions = {}
4546
var optionsSource = null
4647

@@ -49,6 +50,11 @@ IdValidator.prototype.validateSchema = function (
4950
if (schemaType.options.refConditions) {
5051
conditions = schemaType.options.refConditions
5152
}
53+
} else if (schemaType.options && schemaType.options.refPath) {
54+
refModelPath = schemaType.options.refPath
55+
if (schemaType.options.refConditions) {
56+
conditions = schemaType.options.refConditions
57+
}
5258
} else if (schemaType.caster && schemaType.caster.instance &&
5359
schemaType.caster.options && schemaType.caster.options.ref) {
5460
refModelName = schemaType.caster.options.ref
@@ -63,7 +69,7 @@ IdValidator.prototype.validateSchema = function (
6369
(schemaType['$isMongooseArray'] === true)
6470
validateFunction = isArraySchemaType ? validateIdArray : validateId
6571

66-
if (refModelName) {
72+
if (refModelName || refModelPath) {
6773
schema.path(path).validate({
6874
validator: function (value) {
6975
return new Promise(function (resolve, reject) {
@@ -84,8 +90,12 @@ IdValidator.prototype.validateSchema = function (
8490
}
8591
})
8692
}
93+
var localRefModelName = refModelName
94+
if (refModelPath) {
95+
localRefModelName = this[refModelPath]
96+
}
8797

88-
return validateFunction(this, connection, refModelName,
98+
return validateFunction(this, connection, localRefModelName,
8999
value, conditionsCopy, resolve, reject, allowDuplicates)
90100
}
91101
resolve(true)

0 commit comments

Comments
 (0)