-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathid-validator.js
More file actions
148 lines (126 loc) · 5.41 KB
/
id-validator.js
File metadata and controls
148 lines (126 loc) · 5.41 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
var mongoose = require('mongoose')
var traverse = require('traverse')
var clone = require('clone')
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 ref = null
var refPath = null
var conditions = {}
if (schemaType.options && schemaType.options.ref) {
ref = schemaType.options.ref
if (schemaType.options.refConditions) {
conditions = schemaType.options.refConditions
}
} else if (schemaType.options && schemaType.options.refPath) {
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) {
ref = 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 (ref || refPath) {
schema.path(path).validate({
validator: function (value) {
var conditionsCopy = conditions
//A query may not implement an isModified function.
if (this && !!this.isModified && !this.isModified(path)) {
return true
}
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 =
typeof ref === 'function' ? ref.call(this, value) : ref
if (refPath) {
const refModelPath =
typeof refPath === 'function' ? refPath.call(this, value) : refPath
localRefModelName = this[refModelPath]
}
return validateFunction(this, connection, localRefModelName,
value, conditionsCopy, allowDuplicates)
}
return true
},
message: message
})
}
})
}
async function executeQuery (query, conditions, validateValue) {
for (var fieldName in conditions) {
query.where(fieldName, conditions[fieldName])
}
query.setOptions({ readPreference: 'primary' })
const count = await query.exec()
return count === validateValue
}
function validateId (doc, connection, refModelName, value, conditions) {
if (value == null) return true
var refModel = connection.model(refModelName)
var query = refModel.countDocuments({_id: value})
var session = doc.$session && doc.$session()
if (session) query.session(session)
return executeQuery(query, conditions, 1)
}
function validateIdArray (doc, connection, refModelName, values, conditions, allowDuplicates) {
if (values == null || values.length == 0) return true
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)
return executeQuery(query, conditions, checkValues.length)
}
module.exports = IdValidator.prototype.validate
module.exports.getConstructor = IdValidator