-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathid-validator.js
More file actions
177 lines (155 loc) · 6.68 KB
/
id-validator.js
File metadata and controls
177 lines (155 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var mongoose = require('mongoose')
var traverse = require('traverse')
var clone = require('clone')
var Schema = mongoose.Schema
function IdValidator () {
this.enabled = true
}
IdValidator.prototype.enable = function () {
this.enabled = true
}
IdValidator.prototype.disable = function () {
this.enabled = false
}
IdValidator.prototype.validate = function (schema, options) {
var self = this
options = options || {}
var message = options.message || '{PATH} references a non existing ID'
var connection = options.connection || mongoose
var allowDuplicates = options.allowDuplicates || false
var caller = (self instanceof IdValidator) ? self : IdValidator.prototype
return caller.validateSchema(schema, message, connection, allowDuplicates)
}
IdValidator.prototype.validateSchema = function (
schema, message, connection, allowDuplicates) {
var self = this
var caller = (self instanceof IdValidator) ? self : IdValidator.prototype
schema.eachPath(function (path, schemaType) {
// Apply validation recursively to sub-schemas (but not ourself if we
// are referenced recursively)
if (schemaType.schema && schemaType.schema !== schema) {
return caller.validateSchema(schemaType.schema, message,
connection)
}
var validateFunction = null
var refModelName = null
var refModelPath = null
var conditions = {}
var optionsSource = null
if (schemaType.options && schemaType.options.ref) {
refModelName = schemaType.options.ref
if (schemaType.options.refConditions) {
conditions = schemaType.options.refConditions
}
} else if (schemaType.options && schemaType.options.refPath) {
refModelPath = schemaType.options.refPath
console.log("[mongoose-id-validator] schemaType.options: ", schemaType.options)
console.log("[mongoose-id-validator] schemaType: refPath", 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
if (schemaType.caster.options.refConditions) {
conditions = schemaType.caster.options.refConditions
}
}
var isArraySchemaType =
(schemaType.caster && schemaType.caster.instance) ||
(schemaType.instance === 'Array') ||
(schemaType['$isMongooseArray'] === true)
validateFunction = isArraySchemaType ? validateIdArray : validateId
if (refModelName || refModelPath) {
schema.path(path).validate({
validator: function (value) {
return new Promise(function (resolve, reject) {
var conditionsCopy = conditions
//A query may not implement an isModified function.
if (this && !!this.isModified && !this.isModified(path)) {
resolve(true)
return
}
if (!(self instanceof IdValidator) || self.enabled) {
if (Object.keys(conditionsCopy).length > 0) {
var instance = this
conditionsCopy = clone(conditions)
traverse(conditionsCopy).forEach(function (value) {
if (typeof value === 'function') {
this.update(value.call(instance))
}
})
}
var localRefModelName = refModelName
/*console.log("[mongoose-id-validator] REF_PATH_NAME: ", refModelPath)
console.log("[mongoose-id-validator] THIS: ", this)
console.log("[mongoose-id-validator] SCHEMA: ", schemaType)
console.log("[mongoose-id-validator] PATH: ", path)*/
if (refModelPath) {
const refModelPathSplits = refModelPath.split('.')
if(refModelPathSplits.length > 1) localRefModelName = this[refModelPathSplits[refModelPathSplits.length-1]]
else localRefModelName = this[refModelPath]
}
// console.log("[mongoose-id-validator] REF_Model_NAME: ", localRefModelName)
return validateFunction(this, connection, localRefModelName,
value, conditionsCopy, resolve, reject, allowDuplicates)
}
resolve(true)
return
}.bind(this));
},
message: message
})
}
})
}
function executeQuery (query, conditions, validateValue, resolve, reject) {
for (var fieldName in conditions) {
query.where(fieldName, conditions[fieldName])
}
query.exec(function (err, count) {
if (err) {
reject(err)
return
}
return count === validateValue ? resolve(true) : resolve(false)
})
}
function validateId (
doc, connection, refModelName, value, conditions, resolve, reject) {
if (value == null) {
resolve(true)
return
}
var refModel = connection.model(refModelName)
var query = refModel.countDocuments({_id: value})
var session = doc.$session && doc.$session()
if (session) {
query.session(session)
}
executeQuery(query, conditions, 1, resolve, reject)
}
function validateIdArray (
doc, connection, refModelName, values, conditions, resolve, reject,
allowDuplicates) {
if (values == null || values.length == 0) {
resolve(true)
return
}
var checkValues = values
if (allowDuplicates) {
//Extract unique values only
checkValues = values.filter(function (v, i) {
return values.indexOf(v) === i
})
}
var refModel = connection.model(refModelName)
var query = refModel.countDocuments().where('_id')['in'](checkValues)
var session = doc.$session && doc.$session()
if (session) {
query.session(session)
}
executeQuery(query, conditions, checkValues.length, resolve, reject)
}
module.exports = IdValidator.prototype.validate
module.exports.getConstructor = IdValidator