11var mongoose = require ( 'mongoose' ) ;
22var Schema = mongoose . Schema ;
33
4- function IdValidator ( ) {
4+ function IdValidator ( ) {
55 this . enabled = true ;
66}
77
8- IdValidator . prototype . enable = function ( ) {
8+ IdValidator . prototype . enable = function ( ) {
99 this . enabled = true ;
1010}
1111
12- IdValidator . prototype . disable = function ( ) {
12+ IdValidator . prototype . disable = function ( ) {
1313 this . enabled = false ;
1414}
1515
16- IdValidator . prototype . validate = function ( schema , options ) {
16+ IdValidator . prototype . validate = function ( schema , options ) {
1717 var self = this ;
1818 options = options || { } ;
1919 var message = options . message || "{PATH} references a non existing ID" ;
2020 var connection = options . connection || mongoose ;
21+ var allowDuplicates = options . allowDuplicates || false ;
2122
2223 var caller = ( self instanceof IdValidator ) ? self : IdValidator . prototype ;
2324
24- return caller . validateSchema ( schema , message , connection ) ;
25+ return caller . validateSchema ( schema , message , connection , allowDuplicates ) ;
2526}
2627
27- IdValidator . prototype . validateSchema = function ( schema , message , connection ) {
28+ IdValidator . prototype . validateSchema = function ( schema , message , connection , allowDuplicates ) {
2829 var self = this ;
2930 var caller = ( self instanceof IdValidator ) ? self : IdValidator . prototype ;
3031 schema . eachPath ( function ( path , schemaType ) {
@@ -54,15 +55,15 @@ IdValidator.prototype.validateSchema = function(schema, message, connection) {
5455
5556 if ( validateFunction ) {
5657 schema . path ( path ) . validate ( function ( value , respond ) {
57- if ( ! ( self instanceof IdValidator ) || self . enabled ) {
58- return validateFunction ( this , connection , refModelName , value , conditions , respond ) ;
58+ if ( ! ( self instanceof IdValidator ) || self . enabled ) {
59+ return validateFunction ( this , connection , refModelName , value , conditions , respond , allowDuplicates ) ;
5960 }
6061 return respond ( true ) ;
6162 } , message ) ;
6263 }
6364
6465 } ) ;
65- }
66+ } ;
6667
6768function executeQuery ( query , conditions , validateValue , respond ) {
6869 for ( var fieldName in conditions ) {
@@ -85,13 +86,22 @@ function validateId(doc, connection, refModelName, value, conditions, respond) {
8586 executeQuery ( query , conditions , 1 , respond ) ;
8687}
8788
88- function validateIdArray ( doc , connection , refModelName , values , conditions , respond ) {
89+ function validateIdArray ( doc , connection , refModelName , values , conditions , respond , allowDuplicates ) {
8990 if ( values == null || values . length == 0 ) {
9091 return respond ( true ) ;
9192 }
93+
94+ var checkValues = values ;
95+ if ( allowDuplicates ) {
96+ //Extract unique values only
97+ checkValues = values . filter ( function ( v , i ) {
98+ return values . indexOf ( v ) === i
99+ } ) ;
100+ }
101+
92102 var refModel = connection . model ( refModelName ) ;
93- var query = refModel . count ( ) . where ( '_id' ) [ 'in' ] ( values ) ;
94- executeQuery ( query , conditions , values . length , respond ) ;
103+ var query = refModel . count ( ) . where ( '_id' ) [ 'in' ] ( checkValues ) ;
104+ executeQuery ( query , conditions , checkValues . length , respond ) ;
95105}
96106
97107module . exports = IdValidator . prototype . validate ;
0 commit comments