Skip to content

Commit 57c0fc2

Browse files
committed
Updated recursion code to prevent validating sub-schema when implementing self recursive schemas.
This fixes #19.
1 parent fca3dcd commit 57c0fc2

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

lib/id-validator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ IdValidator.prototype.validateSchema = function (
3232
var self = this
3333
var caller = (self instanceof IdValidator) ? self : IdValidator.prototype
3434
schema.eachPath(function (path, schemaType) {
35-
36-
//Apply validation recursively to sub-schemas
37-
if (schemaType.schema) {
35+
// Apply validation recursively to sub-schemas (but not ourself if we
36+
// are referenced recursively)
37+
if (schemaType.schema && schemaType.schema !== schema) {
3838
return caller.validateSchema(schemaType.schema, message,
3939
connection)
4040
}

test/id-validator.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,29 @@ describe('mongoose-id-validator Integration Tests', function () {
608608
})
609609
})
610610

611+
describe('Self recursive schema', function () {
612+
var Tasks = new mongoose.Schema()
613+
Tasks.add({
614+
title: String,
615+
subtasks: [Tasks]
616+
})
617+
Tasks.plugin(validator)
618+
var Task = mongoose.model('Tasks', Tasks)
619+
620+
it('Should validate recursive task', function (done) {
621+
var t1 = new Task({title: 'Task 1'})
622+
var t2 = new Task({title: 'Task 2', subtasks: [t1]})
623+
async.series([
624+
function (cb) {
625+
t1.save(cb)
626+
},
627+
function (cb) {
628+
t2.save(cb)
629+
}
630+
], done)
631+
})
632+
})
633+
611634
describe('Connection tests', function () {
612635
it('Correct connection should be used when specified as option',
613636
function (done) {

0 commit comments

Comments
 (0)