Skip to content

Commit

Permalink
Adding support for dynamic refs
Browse files Browse the repository at this point in the history
  • Loading branch information
makinde committed Apr 24, 2020
1 parent 36e63b3 commit 49fa236
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/id-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ IdValidator.prototype.validateSchema = function (

var validateFunction = null
var refModelName = null
var refModelPath = null
var conditions = {}
var optionsSource = null

Expand All @@ -49,6 +50,11 @@ IdValidator.prototype.validateSchema = function (
if (schemaType.options.refConditions) {
conditions = schemaType.options.refConditions
}
} else if (schemaType.options && schemaType.options.refPath) {
refModelPath = schemaType.options.refPath
if (schemaType.options.refConditions) {
conditions = schemaType.options.refConditions
}
} else if (schemaType.caster && schemaType.caster.instance &&
schemaType.caster.options && schemaType.caster.options.ref) {
refModelName = schemaType.caster.options.ref
Expand All @@ -63,7 +69,7 @@ IdValidator.prototype.validateSchema = function (
(schemaType['$isMongooseArray'] === true)
validateFunction = isArraySchemaType ? validateIdArray : validateId

if (refModelName) {
if (refModelName || refModelPath) {
schema.path(path).validate({
validator: function (value) {
return new Promise(function (resolve, reject) {
Expand All @@ -84,8 +90,12 @@ IdValidator.prototype.validateSchema = function (
}
})
}
var localRefModelName = refModelName
if (refModelPath) {
localRefModelName = this[refModelPath]
}

return validateFunction(this, connection, refModelName,
return validateFunction(this, connection, localRefModelName,
value, conditionsCopy, resolve, reject, allowDuplicates)
}
resolve(true)
Expand Down

0 comments on commit 49fa236

Please sign in to comment.