|
1 | | -var mongoose = require('mongoose'); |
2 | | -var traverse = require('traverse'); |
3 | | -var clone = require('clone'); |
4 | | -var Schema = mongoose.Schema; |
| 1 | +var mongoose = require('mongoose') |
| 2 | +var traverse = require('traverse') |
| 3 | +var clone = require('clone') |
| 4 | +var Schema = mongoose.Schema |
5 | 5 |
|
6 | | -function IdValidator() { |
7 | | - this.enabled = true; |
| 6 | +function IdValidator () { |
| 7 | + this.enabled = true |
8 | 8 | } |
9 | 9 |
|
10 | 10 | IdValidator.prototype.enable = function () { |
11 | | - this.enabled = true; |
12 | | -}; |
| 11 | + this.enabled = true |
| 12 | +} |
13 | 13 |
|
14 | 14 | IdValidator.prototype.disable = function () { |
15 | | - this.enabled = false; |
16 | | -}; |
| 15 | + this.enabled = false |
| 16 | +} |
17 | 17 |
|
18 | 18 | IdValidator.prototype.validate = function (schema, options) { |
19 | | - var self = this; |
20 | | - options = options || {}; |
21 | | - var message = options.message || "{PATH} references a non existing ID"; |
22 | | - var connection = options.connection || mongoose; |
23 | | - var allowDuplicates = options.allowDuplicates || false; |
| 19 | + var self = this |
| 20 | + options = options || {} |
| 21 | + var message = options.message || '{PATH} references a non existing ID' |
| 22 | + var connection = options.connection || mongoose |
| 23 | + var allowDuplicates = options.allowDuplicates || false |
24 | 24 |
|
25 | | - var caller = (self instanceof IdValidator) ? self : IdValidator.prototype; |
| 25 | + var caller = (self instanceof IdValidator) ? self : IdValidator.prototype |
26 | 26 |
|
27 | | - return caller.validateSchema(schema, message, connection, allowDuplicates); |
28 | | -}; |
| 27 | + return caller.validateSchema(schema, message, connection, allowDuplicates) |
| 28 | +} |
29 | 29 |
|
30 | | -IdValidator.prototype.validateSchema = function (schema, message, connection, allowDuplicates) { |
31 | | - var self = this; |
32 | | - var caller = (self instanceof IdValidator) ? self : IdValidator.prototype; |
| 30 | +IdValidator.prototype.validateSchema = function ( |
| 31 | + schema, message, connection, allowDuplicates) { |
| 32 | + var self = this |
| 33 | + var caller = (self instanceof IdValidator) ? self : IdValidator.prototype |
33 | 34 | schema.eachPath(function (path, schemaType) { |
34 | 35 |
|
35 | 36 | //Apply validation recursively to sub-schemas |
36 | 37 | if (schemaType.schema) { |
37 | | - return caller.validateSchema(schemaType.schema, message, connection); |
| 38 | + return caller.validateSchema(schemaType.schema, message, |
| 39 | + connection) |
38 | 40 | } |
39 | 41 |
|
40 | | - var validateFunction = null; |
41 | | - var refModelName = null; |
42 | | - var conditions = {}; |
| 42 | + var validateFunction = null |
| 43 | + var refModelName = null |
| 44 | + var conditions = {} |
43 | 45 |
|
44 | 46 | if (schemaType.options && schemaType.options.ref) { |
45 | | - validateFunction = validateId; |
46 | | - refModelName = schemaType.options.ref; |
| 47 | + validateFunction = validateId |
| 48 | + refModelName = schemaType.options.ref |
47 | 49 | if (schemaType.options.refConditions) { |
48 | | - conditions = schemaType.options.refConditions; |
| 50 | + conditions = schemaType.options.refConditions |
49 | 51 | } |
50 | | - } else if (schemaType.caster && schemaType.caster.instance && schemaType.caster.options && schemaType.caster.options.ref) { |
51 | | - validateFunction = validateIdArray; |
52 | | - refModelName = schemaType.caster.options.ref; |
| 52 | + } else if (schemaType.caster && schemaType.caster.instance && |
| 53 | + schemaType.caster.options && schemaType.caster.options.ref) { |
| 54 | + validateFunction = validateIdArray |
| 55 | + refModelName = schemaType.caster.options.ref |
53 | 56 | if (schemaType.caster.options.refConditions) { |
54 | | - conditions = schemaType.caster.options.refConditions; |
| 57 | + conditions = schemaType.caster.options.refConditions |
55 | 58 | } |
56 | 59 | } |
57 | 60 |
|
58 | 61 | if (validateFunction) { |
59 | 62 | schema.path(path).validate({ |
60 | 63 | validator: function (value, respond) { |
61 | | - var conditionsCopy = conditions; |
| 64 | + var conditionsCopy = conditions |
62 | 65 | //A query may not implement an isModified function. |
63 | 66 | if (!!this.isModified && !this.isModified(path)) { |
64 | | - return respond(true); |
| 67 | + return respond(true) |
65 | 68 | } |
66 | 69 | if (!(self instanceof IdValidator) || self.enabled) { |
67 | 70 | if (Object.keys(conditionsCopy).length > 0) { |
68 | | - var instance = this; |
| 71 | + var instance = this |
69 | 72 |
|
70 | | - conditionsCopy = clone(conditions); |
| 73 | + conditionsCopy = clone(conditions) |
71 | 74 | traverse(conditionsCopy).forEach(function (value) { |
72 | 75 | if (typeof value === 'function') { |
73 | | - this.update(value.call(instance)); |
| 76 | + this.update(value.call(instance)) |
74 | 77 | } |
75 | | - }); |
| 78 | + }) |
76 | 79 | } |
77 | 80 |
|
78 | | - return validateFunction(this, connection, refModelName, value, conditionsCopy, respond, allowDuplicates); |
| 81 | + return validateFunction(this, connection, refModelName, |
| 82 | + value, conditionsCopy, respond, allowDuplicates) |
79 | 83 | } |
80 | | - return respond(true); |
| 84 | + return respond(true) |
81 | 85 | }, |
82 | 86 | isAsync: true, |
83 | 87 | message: message |
84 | | - }); |
| 88 | + }) |
85 | 89 | } |
86 | | - }); |
87 | | -}; |
| 90 | + }) |
| 91 | +} |
88 | 92 |
|
89 | | -function executeQuery(query, conditions, validateValue, respond) { |
| 93 | +function executeQuery (query, conditions, validateValue, respond) { |
90 | 94 | for (var fieldName in conditions) { |
91 | | - query.where(fieldName, conditions[fieldName]); |
| 95 | + query.where(fieldName, conditions[fieldName]) |
92 | 96 | } |
93 | 97 | query.exec(function (err, count) { |
94 | 98 | if (err) { |
95 | | - return respond(err); |
| 99 | + return respond(err) |
96 | 100 | } |
97 | | - respond(count === validateValue); |
98 | | - }); |
| 101 | + respond(count === validateValue) |
| 102 | + }) |
99 | 103 | } |
100 | 104 |
|
101 | | -function validateId(doc, connection, refModelName, value, conditions, respond) { |
| 105 | +function validateId ( |
| 106 | + doc, connection, refModelName, value, conditions, respond) { |
102 | 107 | if (value == null) { |
103 | | - return respond(true); |
| 108 | + return respond(true) |
104 | 109 | } |
105 | | - var refModel = connection.model(refModelName); |
106 | | - var query = refModel.count({_id: value}); |
107 | | - executeQuery(query, conditions, 1, respond); |
| 110 | + var refModel = connection.model(refModelName) |
| 111 | + var query = refModel.count({_id: value}) |
| 112 | + executeQuery(query, conditions, 1, respond) |
108 | 113 | } |
109 | 114 |
|
110 | | -function validateIdArray(doc, connection, refModelName, values, conditions, respond, allowDuplicates) { |
| 115 | +function validateIdArray ( |
| 116 | + doc, connection, refModelName, values, conditions, respond, |
| 117 | + allowDuplicates) { |
111 | 118 | if (values == null || values.length == 0) { |
112 | | - return respond(true); |
| 119 | + return respond(true) |
113 | 120 | } |
114 | 121 |
|
115 | | - var checkValues = values; |
| 122 | + var checkValues = values |
116 | 123 | if (allowDuplicates) { |
117 | 124 | //Extract unique values only |
118 | 125 | checkValues = values.filter(function (v, i) { |
119 | 126 | return values.indexOf(v) === i |
120 | | - }); |
| 127 | + }) |
121 | 128 | } |
122 | 129 |
|
123 | | - var refModel = connection.model(refModelName); |
124 | | - var query = refModel.count().where('_id')['in'](checkValues); |
| 130 | + var refModel = connection.model(refModelName) |
| 131 | + var query = refModel.count().where('_id')['in'](checkValues) |
125 | 132 |
|
126 | | - executeQuery(query, conditions, checkValues.length, respond); |
| 133 | + executeQuery(query, conditions, checkValues.length, respond) |
127 | 134 | } |
128 | 135 |
|
129 | | -module.exports = IdValidator.prototype.validate; |
130 | | -module.exports.getConstructor = IdValidator; |
| 136 | +module.exports = IdValidator.prototype.validate |
| 137 | +module.exports.getConstructor = IdValidator |
0 commit comments